forked from malaohu/OneList--
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathonedrive.py
More file actions
160 lines (125 loc) · 5.08 KB
/
Copy pathonedrive.py
File metadata and controls
160 lines (125 loc) · 5.08 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
# Author: MoeClub.org, sxyazi
import json
import pickle
import hashlib
from dcache import Cache
from utils import path_format
from config import config
from urllib import request, parse
class _ItemInfo:
def __init__(self):
self.files = []
self.folders = []
self.is_file = False
class OneDrive():
_request_headers = {'User-Agent': 'ISV|MoeClub|OneList/1.0',
'Accept': 'application/json; odata.metadata=none'}
def __init__(self):
self.api_url = ''
self.resource_id = ''
self.expires_on = ''
self.access_token = ''
self.refresh_token = config.token
def get_access(self, resource='https://api.office.com/discovery/'):
res = self._http_request('https://login.microsoftonline.com/common/oauth2/token', method='POST', data={
'client_id': 'ea2b36f6-b8ad-40be-bc0f-e5e4a4a7d4fa',
'client_secret': 'h27zG8pr8BNsLU0JbBh5AOznNS5Of5Y540l/koc7048=',
'redirect_uri': 'http://localhost/onedrive-login',
'refresh_token': self.refresh_token,
'grant_type': 'refresh_token',
'resource': resource
})
self.expires_on = res['expires_on']
self.access_token = res['access_token']
self.refresh_token = res['refresh_token']
if not self.access_token:
print('Unauthorized')
exit(1)
def get_resource(self):
res = self._http_request(
'https://api.office.com/discovery/v2.0/me/services')
for item in res['value']:
if item['serviceApiVersion'] == 'v2.0':
self.api_url = item['serviceEndpointUri']
self.resource_id = item['serviceResourceId']
if not self.api_url:
raise Exception('Failed to get api url')
def list_items(self, path='/'):
url = '%s/drive/root:%s/?expand=children(select=name,size,file,folder,parentReference,lastModifiedDateTime)' % (
self.api_url, parse.quote(path_format(path)))
res = self._http_request(url)
info = _ItemInfo()
self._append_item(info, res)
if 'children' in res:
for children in res['children']:
self._append_item(info, children)
if info.files and not info.folders:
info.is_file = True
return info
def list_all_items(self, path='/'):
ret = _ItemInfo()
tasks = [{'full_path': path}]
while len(tasks) > 0:
c = tasks.pop(0)
tmp = self.list_items(c['full_path'])
tasks += tmp.folders[1:]
ret.files += tmp.files
ret.folders += tmp.folders[1:]
if ret.files and not ret.folders:
ret.is_file = True
return ret
def list_items_with_cache(self, path='/', flash=False):
path = path_format(path)
key = ('tmp:' + path) if flash else path
if not Cache.has(key):
if flash:
Cache.set(key, self.list_items(path), 10)
else:
print('missing: %s' % path)
info = self.list_items(path)
if info.is_file:
Cache.set(key, info, config.metadata_cached_seconds)
else:
Cache.set(key, info, config.structure_cached_seconds)
return Cache.get(key)
def _http_request(self, url, method='GET', data={}):
headers = self._request_headers.copy()
if self.access_token:
headers['Authorization'] = "Bearer " + self.access_token
data = parse.urlencode(data).encode('utf-8')
res = json.loads(request.urlopen(request.Request(
url, method=method, data=data, headers=headers)).read().decode('utf-8'))
if 'error' in res:
raise Exception(res['error']['message'])
return res
def _append_item(self, info, item):
if 'path' not in item['parentReference']:
path = item['name'] = '/'
else:
path = item['parentReference']['path'][12:] or '/'
dic = {
'name': item['name'],
'size': item['size'],
'hash': self._get_item_hash(item),
'folder': path,
'full_path': path_format(path + '/' + item['name']),
'updated_at': item['lastModifiedDateTime']
}
if '@content.downloadUrl' in item:
dic['download_url'] = item['@content.downloadUrl']
if 'file' in item:
info.files.append(dic)
else:
info.folders.append(dic)
def _get_item_hash(self, item):
dic = {
'name': item['name'],
'size': item['size'],
'parentReference': item['parentReference'],
'lastModifiedDateTime': item['lastModifiedDateTime']
}
if 'file' in item:
dic['file'] = item['file']
else:
dic['folder'] = item['folder']
return hashlib.md5(pickle.dumps(dic)).hexdigest()