-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdown
executable file
·96 lines (78 loc) · 3.35 KB
/
down
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
#!/usr/bin/env python3
import json
import sys
import urllib.parse
import sqlite3
import os
import argparse
import itertools
import requests
conn = sqlite3.connect('data.db', timeout=sys.float_info.max)
cur = conn.cursor()
parser = argparse.ArgumentParser()
parser.add_argument('URL', help='https://onedrive.live.com/?authkey=XXXXX&id=XXXXX&cid=XXXXX')
parser.add_argument('-f', '--force-dirs', action='store_true', help='crawls through directories even if they are unchanged')
args = parser.parse_args()
def download_onedrive_folder(authkey, id, cid, folder):
data = {
'gb': '0,1,2',
'd': 1,
'authKey': authkey,
'id': id,
'cid': cid,
'ps': 2500 # max page size
}
headers = {
'AppId': '1141147648',
'Accept': 'application/json'
}
for count in itertools.count(data['ps'], data['ps']):
response = requests.get('https://skyapi.onedrive.live.com/API/2/GetItems', params=data, headers=headers)
response.raise_for_status()
for item in response.json()['items']:
if 'folder' in item:
for child in item['folder']['children']:
# check etag if child is a folder and force-dirs is true or if child is not a folder
if (child['itemType'] == 32 and not args.force_dirs) or child['itemType'] != 32:
# check if local copy is up to date
cur.execute('SELECT etag FROM download WHERE id = ?', (child['id'],))
rows = cur.fetchall()
if len(rows) == 1 and rows[0][0] == child['eTag']:
continue
# if folder
if child['itemType'] == 32:
path = folder + '/' + item['name'] + '/' + child['name']
os.makedirs(path, exist_ok=True)
print(path)
download_onedrive_folder(data['authKey'], child['id'], child['resourcePartitionCid'], path)
# if file
elif child['itemType'] == 1:
path = folder + '/' + child['name']
if 'extension' in child:
path += child['extension']
print(path)
response = requests.get(child['urls']['download'])
response.raise_for_status()
with open(path, 'wb') as f:
f.write(response.content)
else:
raise Exception('Unknown itemType ' + str(item['itemType']))
# store Etag
cur.execute('INSERT OR REPLACE INTO download(id, etag) VALUES(?,?)', (child['id'], child['eTag']))
conn.commit()
if count < item['folder']['childCount']:
data['si'] = count
else:
return
cur.execute('''CREATE TABLE IF NOT EXISTS download(
id TEXT NOT NULL PRIMARY KEY,
etag TEXT NOT NULL
)''')
url = urllib.parse.urlparse(args.URL)
params = urllib.parse.parse_qs(url.query)
data = {
'authKey': urllib.parse.unquote(params['authkey'][0]),
'id': urllib.parse.unquote(params['id'][0]),
'cid': params['cid']
}
download_onedrive_folder(data['authKey'], data['id'], data['cid'], 'downloads')