1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
use reqwest::blocking;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::fmt::Display;
use reqwest::header::USER_AGENT;
macro_rules! arg_to_string {
($item:expr) => {{
trait PrintAsArg {
fn as_arg_str(&self) -> String{
String::new()
}
}
impl PrintAsArg for Vec<i64>{
fn as_arg_str(&self) -> String{
let mut tmp_string = String::new();
let len = self.len();
for (index,item) in self.iter().enumerate(){
tmp_string.push_str(&(item.to_string()));
if index != len - 1{
tmp_string.push(',');
}
}
format!("[{}]",tmp_string)
}
}
impl PrintAsArg for i64{
fn as_arg_str(&self) -> String{
self.to_string()
}
}
impl PrintAsArg for &str{
fn as_arg_str(&self) -> String{
self.to_string()
}
}
format!("{}={}",stringify!($item),$item.as_arg_str())
}};
}
macro_rules! args {
($($item:expr),+) => {{
let mut tmp_string = String::new();
$(
tmp_string.push_str(&arg_to_string!($item));
tmp_string.push_str(";");
)
+
tmp_string
}};
}
pub mod util;
#[derive(Debug)]
pub struct ApiError {
prompt: String,
}
impl ApiError {
pub fn new(prompt_str: &str) -> ApiError {
ApiError {
prompt: String::from(prompt_str),
}
}
pub fn ret_prompt(&self) -> String {
return self.prompt.clone();
}
}
impl Display for ApiError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.prompt)
}
}
enum YunNode {
GetUserInfo,
GetQuotaInfo,
GetFileList,
GetFileInfo,
}
fn get_node_addr(in_node: YunNode) -> String {
match in_node {
YunNode::GetUserInfo => {
String::from("https://pan.baidu.com/rest/2.0/xpan/nas?method=uinfo")
}
YunNode::GetQuotaInfo => {
String::from("https://pan.baidu.com/api/quota?checkfree=1&checkexpire=1")
}
YunNode::GetFileList => {
String::from("https://pan.baidu.com/rest/2.0/xpan/file?method=list")
}
YunNode::GetFileInfo => {
String::from("http://pan.baidu.com/rest/2.0/xpan/multimedia?method=filemetas")
}
}
}
#[derive(Serialize, Deserialize)]
pub struct UserInfo {
pub baidu_name: String,
pub netdisk_name: String,
pub avatar_url: String,
pub vip_type: i64,
pub uk: i64,
}
#[derive(Serialize, Deserialize)]
pub struct QuotaInfo {
pub total: i64,
pub expire: bool,
pub used: i64,
pub free: i64,
}
#[derive(Serialize, Deserialize,Debug)]
pub struct FilePtr {
pub path: String,
pub category: i64,
pub fs_id: i64,
pub isdir: i64,
pub local_ctime: i64,
pub local_mtime: i64,
pub server_ctime: i64,
pub server_mtime: i64,
pub server_filename: String,
pub md5: Option<String>,
pub size: i64,
pub thumbs: Option<String>,
pub dir_empty:Option<i64>,
}
pub struct YunApi {
access_token: String,
client: blocking::Client,
}
impl YunApi {
pub fn new(in_token: &str) -> YunApi {
YunApi {
access_token: String::from(in_token),
client: blocking::Client::new(),
}
}
fn get_addr(&self, in_node: YunNode, args: &str) -> String {
let arg_vec: Vec<&str> = args.split(';').filter(|s| *s !="").collect();
let node_addr = get_node_addr(in_node);
if node_addr.contains('?') {
let mut addr = format!("{}&access_token={}", node_addr, self.access_token);
for item in arg_vec {
addr.push_str(&format!("{}{}", '&', item));
}
println!("{}",addr);
addr
} else {
let mut addr = format!("{}?access_token={}", node_addr, self.access_token);
for item in arg_vec {
addr.push_str(&format!("{}{}", '&', item));
}
println!("{}",addr);
addr
}
}
fn reqest(&self, in_node: YunNode, args: &str) -> Result<Value, ApiError> {
if let Ok(send_result) = self.client.get(self.get_addr(in_node, args)).header(USER_AGENT, "pan.baidu.com").send() {
if let Ok(text) = send_result.text() {
let tmp = Ok(serde_json::from_str(&text).unwrap());
println!("{:?}",tmp);
tmp
} else {
return Err(ApiError::new("decode text error."));
}
} else {
return Err(ApiError::new("send request error."));
}
}
pub fn get_user_info(&self) -> Result<UserInfo, ApiError> {
let value = self.reqest(YunNode::GetUserInfo, "").unwrap();
if value["errno"].as_i64().unwrap() == 0 {
return Ok(serde_json::from_value(value).unwrap());
} else {
return Err(ApiError::new("Get User infomation error."));
}
}
pub fn get_quota_info(&self) -> Result<QuotaInfo, ApiError> {
let value = self.reqest(YunNode::GetQuotaInfo, "").unwrap();
if value["errno"].as_i64().unwrap() == 0 {
return Ok(serde_json::from_value(value).unwrap());
} else {
return Err(ApiError::new("Get quta infomation error."));
}
}
pub fn get_file_list(
&self,
dir: &str,
start: i64,
limit: i64,
) -> Result<Vec<FilePtr>, ApiError> {
if limit < 0 || limit > 10000 {
return Err(ApiError::new("limit arg error."));
}
if start < 0 {
return Err(ApiError::new("start arg error."));
}
let value = self.reqest(YunNode::GetFileList, &args!(dir,start,limit)).unwrap();
if value["errno"].as_i64().unwrap() == 0 {
let len = value["list"].as_array().unwrap().len();
let mut file_vec = Vec::new();
for index in 0..len {
let file_info: FilePtr =
serde_json::from_value(value["list"][index].clone()).unwrap();
file_vec.push(file_info);
}
return Ok(file_vec);
} else {
return Err(ApiError::new("Get files list error."));
}
}
pub fn get_files_dlink_vec(&self,files:Vec<FilePtr>)->Result<Vec<String>,ApiError>{
let mut array_string = String::new();
let len = files.len();
for (index,item) in files.iter().filter(|f| f.isdir == 0).enumerate(){
array_string.push_str(&item.fs_id.to_string());
if index != len - 1{
array_string.push(',');
}
}
let args = format!("fsids=[{}];dlink=1",array_string);
let value = self.reqest(YunNode::GetFileInfo, &args).unwrap();
if value["errno"].as_i64().unwrap() == 0 {
let mut dlink_vec:Vec<String> = Vec::new();
let len = value["list"].as_array().unwrap().len();
for index in 0..len{
let dlink = value["list"][index]["dlink"].as_str().unwrap().to_string();
dlink_vec.push(dlink);
}
Ok(dlink_vec)
}else{
return Err(ApiError::new("Get file dlink error."));
}
}
}