-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
289 lines (279 loc) · 13.7 KB
/
index.html
File metadata and controls
289 lines (279 loc) · 13.7 KB
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
<!doctype html>
<html lang="en">
<head>
<!-- version 2014.3.23 -->
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=no">
<title>百度帮我下!</title>
<link rel="stylesheet" href="css/uikit.almost-flat.min.css">
<link rel="stylesheet" href="http://cdn.staticfile.org/font-awesome/4.0.3/css/font-awesome.min.css">
</head>
<body>
<div class="uk-container-center uk-width-small-1-1 uk-width-medium-2-3 uk-clearfix">
<h1>百度帮我下</h1>
<form class="uk-form uk-float-left uk-width-1-1">
<fieldset data-uk-margin>
<legend>添加离线任务</legend>
<input id="source_path" type="text" class="uk-width-1-2" placeholder="在此输入任务地址">
<button id="add_task" class="uk-button uk-button-success">添加</button>
<button id="query_tasks" class="uk-button uk-button-primary">查询所有任务</button>
</fieldset>
</form>
<table class="uk-table uk-table-hover uk-float-left">
<caption>离线任务统计</caption>
<thead>
<tr>
<th>任务ID</th>
<th>源地址</th>
<th>存储地址</th>
<th>任务状态</th>
<th>文件大小</th>
</tr>
</thead>
<tbody id="task_list">
</tbody>
</table>
<div class="uk-panel-box uk-width-small-1-1 uk-width-medium-1-2 uk-float-left">
<h3 class="uk-panel-title">个人信息</h3>
<div class="uk-width-1-3 uk-float-left">
<img id="user_pic" src="http://placehold.it/100x100&text=no_user" alt="" />
</div>
<div class="uk-width-2-3 uk-float-right">
<p id="info"></p>
<button id="auth" class="uk-button" disabled>未授权</button>
<a id="auth_link" class="uk-button uk-button-danger" href="https://openapi.baidu.com/oauth/2.0/authorize?response_type=token&client_id=OuHPQzAlkVARw7PqcD64B1Av&redirect_uri=https%3A%2F%2Fgucheen.github.io%2FletBDdoit%2F&scope=basic+netdisk&display=popup">获取授权</a>
</div>
<div class="uk-float-left uk-width-1-1">
<h4>空间使用</h4>
<p id="space_info"></p>
<div class="uk-progress">
<div id="space_chart" class="uk-progress-bar uk-align-center" style="color:#34495e"></div>
</div>
</div>
</div>
<div class="uk-panel-box uk-width-small-1-1 uk-width-medium-1-2 uk-float-right">
<h3 class="uk-panel-title">使用说明</h3>
<p>1. 首次使用请戳左边的授权按钮登录,然后点击这个按钮
<button id="init" class="uk-button uk-button-primary">初始化</button>以生成一个目录用来存放下载文件。</p>
<p>2. 没有做后端,所以无法刷新access_token,每个月重新授权一次。</p>
<p>3. 查询所有任务的功能已经可以使用,但是只能获取在百度云盘添加的任务,在这里添加的任务只能通过单个查询获取。</p>
<p>4. 已经做了Cookie缓存,需要清理请点击
<button id="clean_cookies" class="uk-button uk-button-primary">清理Cookies</button>Cookies有效期为一个月。
</p>
</div>
</div>
<script src="js/jquery.min.js"></script>
<script src="js/uikit.min.js"></script>
<script>
$(function() {
function formatSize(bytes) {
var s = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB'];
var e = Math.floor(Math.log(bytes) / Math.log(1024));
return (bytes / Math.pow(1024, Math.floor(e))).toFixed(2) + ' ' + s[e];
}
function getQueryStringRegExp(name) {
var reg = new RegExp("(^|\\?|&)" + name + "=([^&]*)(\\s|&|$)", "i");
if (reg.test(location.href)) return unescape(RegExp.$2.replace(/\+/g, " "));
return null;
};
/* Cookies */
var docCookies = {
getItem: function(sKey) {
return decodeURIComponent(document.cookie.replace(new RegExp("(?:(?:^|.*;)\\s*" + encodeURIComponent(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*([^;]*).*$)|^.*$"), "$1")) || null;
},
setItem: function(sKey, sValue, vEnd, sPath, sDomain, bSecure) {
if (!sKey || /^(?:expires|max\-age|path|domain|secure)$/i.test(sKey)) {
return false;
}
var sExpires = "";
if (vEnd) {
switch (vEnd.constructor) {
case Number:
sExpires = vEnd === Infinity ? "; expires=Fri, 31 Dec 9999 23:59:59 GMT" : "; max-age=" + vEnd;
break;
case String:
sExpires = "; expires=" + vEnd;
break;
case Date:
sExpires = "; expires=" + vEnd.toUTCString();
break;
}
}
document.cookie = encodeURIComponent(sKey) + "=" + encodeURIComponent(sValue) + sExpires + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "") + (bSecure ? "; secure" : "");
return true;
},
removeItem: function(sKey, sPath, sDomain) {
if (!sKey || !this.hasItem(sKey)) {
return false;
}
document.cookie = encodeURIComponent(sKey) + "=; expires=Thu, 01 Jan 1970 00:00:00 GMT" + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "");
return true;
},
hasItem: function(sKey) {
return (new RegExp("(?:^|;\\s*)" + encodeURIComponent(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=")).test(document.cookie);
},
keys: /* optional method: you can safely remove it! */ function() {
var aKeys = document.cookie.replace(/((?:^|\s*;)[^\=]+)(?=;|$)|^\s*|\s*(?:\=[^;]*)?(?:\1|$)/g, "").split(/\s*(?:\=[^;]*)?;\s*/);
for (var nIdx = 0; nIdx < aKeys.length; nIdx++) {
aKeys[nIdx] = decodeURIComponent(aKeys[nIdx]);
}
return aKeys;
}
};
function addTaskInfo(task_id) {
$.post('https://pcs.baidu.com/rest/2.0/pcs/services/cloud_dl', {
method: 'query_task',
access_token: user_access_token,
task_ids: task_id,
op_type: 1
}).done(function(data) {
var res = data['task_info'][task_id];
var info_source = res['source_url'];
var info_path = res['save_path'];
var info_size = formatSize(res['finished_size']);
var info_status;
switch (res['status']) {
case '0':
info_status = '下载成功';
break;
case '1':
info_status = '下载进行中';
break;
case '2':
info_status = '系统错误';
break;
case '3':
info_status = '资源不存在';
break;
case '4':
info_status = '下载超时';
break;
case '5':
info_status = '资源存在但下载失败';
break;
case '6':
info_status = '存储空间不足';
break;
case '7':
info_status = '任务取消';
break;
}
var new_task_info = '<tr><td>' + task_id + '</td><td>' + info_source + '</td><td><a href="http://pan.baidu.com/disk/home#dir/path=%2Fapps%2Feasydl">' + info_path + '</a></td><td>' + info_status + '</td><td>' + info_size + '</td></tr>';
$('#task_list').append(new_task_info);
});
}
var accessToken = getQueryStringRegExp('access_token');
if (accessToken) {
docCookies.setItem('user_access_token', accessToken, 2592000);
}
var user_access_token = docCookies.getItem('user_access_token');
$('#clean_cookies').click(function(e) {
e.preventDefault();
docCookies.removeItem('user_access_token');
window.location.assign('/');
});
if (user_access_token) {
$.ajax({
url: 'https://openapi.baidu.com/rest/2.0/passport/users/isAppUser?access_token=' + user_access_token,
dataType: 'jsonp'
}).done(function(data) {
var isAuth;
if (data.result == 1) {
isAuth = '已授权';
$('#auth_link').hide();
$.ajax({
url: 'https://openapi.baidu.com/rest/2.0/passport/users/getLoggedInUser?access_token=' + user_access_token,
dataType: 'jsonp'
}).done(function(data) {
$('#info').text(data.uname);
$('#user_pic').attr('src', 'http://tb.himg.baidu.com/sys/portrait/item/' + data.portrait);
});
$.ajax({
url: 'https://pcs.baidu.com/rest/2.0/pcs/quota?method=info&access_token=' + user_access_token,
dataType: 'jsonp'
}).done(function(data) {
var space_quota = formatSize(data.quota);
var space_used = formatSize(data.used);
var use_per = ((data.used / data.quota) * 100).toFixed(2);
$('#space_chart').css('width', use_per + '%').text(use_per + '%');
$('#space_info').text(space_used + '/' + space_quota);
});
} else {
isAuth = '未授权';
}
$('#auth').text(isAuth);
});
$('#add_task').click(function(e) {
e.preventDefault();
var $this = $(this);
$this.html('<i class="uk-icon-spinner uk-icon-spin"></i> 提交中');
var source_path = $('#source_path').val();
$.post('https://pcs.baidu.com/rest/2.0/pcs/services/cloud_dl', {
method: 'add_task',
access_token: user_access_token,
save_path: '/apps/easydl',
source_url: source_path
}).done(function(data) {
$('#source_path').val('');
addTaskInfo(data.task_id);
$this.html('添加');
});
});
/* 查询任务已经可以使用 */
$('#query_tasks').click(function(e) {
e.preventDefault();
$.post('https://pcs.baidu.com/rest/2.0/pcs/services/cloud_dl', {
method: 'list_task',
access_token: user_access_token,
need_task_info: '1'
}).done(function(data) {
var res = data['task_info'];
for (var i = 0; i < res.length; i++) {
var task_id = res[i]['task_id'];
var info_source = res[i]['source_url'];
var info_path = res[i]['save_path'];
var info_status;
switch (res[i]['status']) {
case '0':
info_status = '下载成功';
break;
case '1':
info_status = '下载进行中';
break;
case '2':
info_status = '系统错误';
break;
case '3':
info_status = '资源不存在';
break;
case '4':
info_status = '下载超时';
break;
case '5':
info_status = '资源存在但下载失败';
break;
case '6':
info_status = '存储空间不足';
break;
case '7':
info_status = '任务取消';
break;
}
var new_task_info = '<tr><td>' + task_id + '</td><td>' + info_source + '</td><td><a href="http://pan.baidu.com/disk/home#dir/path=%2Fapps%2Feasydl">' + info_path + '</a></td><td>' + info_status + '</td><td><button class="uk-button">取消该任务</button></td></tr>';
$('#task_list').append(new_task_info);
}
});
});
$('#init').click(function(e) {
e.preventDefault();
$.get('https://pcs.baidu.com/rest/2.0/pcs/file', {
method: 'mkdir',
access_token: user_access_token,
path: '/apps/easydl'
})
});
}
})
</script>
</body>
</html>