forked from reruin/sharelist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
drive.od.js
195 lines (154 loc) · 4.67 KB
/
drive.od.js
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
/*
* One Drive
* od:OneDriveID
*/
const name = 'OneDrive'
const version = '1.0'
const protocols = ['od', 'onedrive']
const defaultProtocol = 'od'
module.exports = ({ request , cache , getConfig , querystring , datetime }) => {
var _authkey, _appid, _rootcid, _cookie , _authid
// get authkey
const getAuth = async () => {
if (!_authkey) {
await updateAuth()
}
return { authkey: _authkey, cookie: _cookie, appid: _appid }
}
const checkAuthId = (id) => {
if(!_authid){
if(/^s!/.test(id)){
_authid = id
}
}
}
const updateAuth = async () => {
let url = 'https://1drv.ms/f/' + _authid
let resp = await request.get(url, { followRedirect: false })
if (resp.headers && resp.headers.location) {
let params = querystring.parse(resp.headers.location.split('?')[1])
_authkey = params.authkey
let cid = params.resid.split('!')[0].toLowerCase()
resp = await request.get('https://onedrive.live.com/?authkey=' + params.authkey + '&id=' + params.resid + '&cid=' + cid, { followRedirect: false })
_cookie = resp.headers['set-cookie'].join('; ')
_appid = (resp.body.match(/"appId":"(\d+)"/) || ['', ''])[1]
console.log('******* update authkey:', _authkey)
}
}
// shareid => id
const conv = async (shareid) => {
if (cache.get('od:' + shareid)) {
return cache.get('od:' + shareid)
}
let url = 'https://1drv.ms/f/' + shareid.replace('od:', '')
let r = await request.get(url, { followRedirect: false })
if (r.headers && r.headers.location) {
let params = querystring.parse(r.headers.location.split('?')[1])
cache.set('od:' + shareid, params.resid)
return params.resid
} else {
return ''
}
}
// gd folder => files
// 非公开接口
const folder = async (id, _) => {
checkAuthId(id)
_ = _ || {}
let nocache = _.nocache
// shareid
if (/^s!/.test(id)) {
id = await conv(id)
}
let resid = 'od:' + id
let resp = { id, type: 'folder', protocol: defaultProtocol, children: [] }
if (cache.get(resid) && !nocache) {
resp = cache.get(resid)
if (
resp.$cached_at &&
resp.children &&
(Date.now() - resp.$cached_at < getConfig('max_age_dir'))
) {
console.log('get od folder from cache')
return resp
}
}
let { authkey, cookie, appid } = await getAuth()
let children = []
console.log('>>>>',id)
let cid = id.split('!')[0].toLowerCase()
let opts = {
authKey: authkey,
id: id,
cid: cid,
//以上参数必须
caller: '',
sb: 0,
ps: 100,
sd: 0,
gb: '0,1,2',
d: '1',
m: 'zh-CN',
iabch: '1',
pi: '5',
path: '1',
lct: '1',
rset: 'odweb',
v: Math.random(),
si: '0',
}
let headers = {
"X-SkyApiOriginId": "" + Math.random(),
"AppId": appid,
"Accept": "application/json",
//以上三项必须
"Host": "skyapi.onedrive.live.com",
"Referer": "https://skyapi.onedrive.live.com/xmlproxy.htm?domain=live.com",
"Cookie": cookie
}
r = await request.get('https://skyapi.onedrive.live.com/API/2/GetItems?' + querystring.stringify(opts), { followRedirect: false, headers })
r = JSON.parse(r.body)
if (r.error) {
console.log(r)
} else {
r = (r.items || [r.item])[0]
children = r.folder ? r.folder.children.map((i) => {
let ext = i.extension ? i.extension.replace(/\./g, '') : ''
return {
id: i.id,
name: i.name + (i.folder ? '' : i.extension),
ext: ext,
protocol: defaultProtocol,
parent: i.parentId,
mime: i.mimeType,
created_at: datetime(i.displayCreationDate.replace(/\//g, '-')),
updated_at: datetime(i.displayModifiedDate.replace(/\//g, '-')),
size: parseInt(i.size),
type: i.folder ? 'folder' : undefined,
url: i.folder ? '' : i.urls.download,
$cached_at: Date.now()
}
}) : []
resp.$cached_at = Date.now()
resp.children = children
cache.set(resid, resp)
}
return resp
}
const file = async (id, { data = {} } = {}) => {
if (
data &&
data.$cached_at &&
data.url &&
(Date.now() - data.$cached_at < getConfig('max_age_file'))
) {
console.log('get od file from cache')
return data
}
//刷新父路径
let parent = await folder(data.parent, { nocache: true })
let hit = parent.children.find( i => i.id == id )
return hit || ''
}
return { name, label:'OD ID挂载版', version, drive:{protocols, folder, file} }
}