-
Notifications
You must be signed in to change notification settings - Fork 0
/
offline_download.py
executable file
·127 lines (100 loc) · 3.8 KB
/
offline_download.py
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
#! /usr/bin/env python
"""Download files to onedrive offline.
Usage:
offline_download.py [--name=FILENAME] <url>
offline_download.py (-h | --help)
offline_download.py --version
Options:
-h --help Show this screen.
--version Show version.
"""
import os
import urllib
import requests
from docopt import docopt
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
def get_auth_code(
username, password, client_id, redirect_uri,
url="https://login.microsoftonline.com/common/oauth2/v2.0/authorize", time_wait=60):
"""https://docs.microsoft.com/zh-cn/onedrive/developer/rest-api/getting-started/authentication?view=odsp-graph-online"""
params = {
"response_type": "code",
"client_id": client_id,
"redirect_uri": redirect_uri,
"scope": 'Files.ReadWrite',
}
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
driver = webdriver.Chrome(options=chrome_options)
driver.get('{}?{}'.format(url, urllib.parse.urlencode(params)))
try:
element = WebDriverWait(driver, time_wait).until(
EC.element_to_be_clickable((By.NAME, "loginfmt"))
)
element.clear()
element.send_keys(username)
element.send_keys(Keys.RETURN)
element = WebDriverWait(driver, time_wait).until(
EC.element_to_be_clickable((By.NAME, "passwd"))
)
element.clear()
element.send_keys(password)
element.send_keys(Keys.RETURN)
#element = WebDriverWait(driver, time_wait).until(
# EC.element_to_be_clickable((By.ID, "idBtn_Accept"))
#)
#element.send_keys(Keys.RETURN)
location = driver.current_url
driver.close()
except Exception as e:
print(e)
exit()
finally:
driver.quit()
return urllib.parse.parse_qs(urllib.parse.urlparse(location).query)['code'][0]
def redeem_token(code, client_id, redirect_uri,
url="https://login.microsoftonline.com/common/oauth2/v2.0/token"):
data = {
"client_id": client_id,
"redirect_uri": redirect_uri,
"code": code,
"grant_type": "authorization_code",
}
return requests.post(url, data).json()
def auth(username, password, client_id,
redirect_uri="https://login.microsoftonline.com/common/oauth2/nativeclient"):
code = get_auth_code(username, password, client_id, redirect_uri)
return redeem_token(code, client_id, redirect_uri)['access_token']
def offline_download(token, target_url, file_name=None,
url="https://graph.microsoft.com/v1.0/me/drive/items/root/children"):
"""https://docs.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_upload_url?view=odsp-graph-online"""
if not file_name:
file_name = os.path.basename(urllib.parse.urlparse(target_url).path)
data = {
"@microsoft.graph.sourceUrl": target_url,
"name": file_name,
"file": {}
}
headers = {
"Prefer": "respond-async",
"Authorization": "Bearer {}".format(token),
}
r = requests.post(url, json=data, headers=headers)
return r.headers['Location']
def main(url, file_name=None):
token = auth(
os.getenv("MS_USERNAME"),
os.getenv("MS_PASSWORD"),
os.getenv("MS_CLIENT_ID"),
)
return offline_download(token, url, file_name)
if __name__ == '__main__':
arguments = docopt(__doc__, version='Offline Download 1.0.0')
monitor_url = main(arguments['<url>'], arguments['--name'])
print(monitor_url)