-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathonedrive.py
More file actions
65 lines (54 loc) · 2.33 KB
/
Copy pathonedrive.py
File metadata and controls
65 lines (54 loc) · 2.33 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
import logging
import onedrivesdk
from os import listdir
from os.path import isfile, join
class OneDrive:
def __init__(self, config):
redirect_uri = "http://" + config["webService"]["name"] + ":" + str(config["webService"]["port"])
client_secret = config["oneDrive"]["clientSecret"]
client_id = config["oneDrive"]["clientId"]
api_base_url = config["oneDrive"]["url"]
scopes = ['wl.signin', 'wl.offline_access', 'onedrive.readwrite']
http_provider = onedrivesdk.HttpProvider()
auth_provider = onedrivesdk.AuthProvider(
http_provider=http_provider,
client_id=client_id,
scopes=scopes)
loaded = True
#reload session ??
try:
auth_provider.load_session()
auth_provider.refresh_token()
except:
loaded = False
#reload session ??
self.client = onedrivesdk.OneDriveClient(api_base_url, auth_provider, http_provider)
if loaded == False :
auth_url = self.client.auth_provider.get_auth_url(redirect_uri)
# Ask for the code
print('Paste this URL into your browser, approve the app\'s access.')
print('Copy everything in the address bar after "code=", and paste it below.')
print(auth_url)
code = input('Paste code here: ')
self.client.auth_provider.authenticate(code, redirect_uri, client_secret)
auth_provider.save_session()
def getItem(self, parentID, name):
items = self.client.item(id=parentID).children.get()
for item in items:
if item.name == name:
return item
return None
def createFolder(self, parentID, name):
f = onedrivesdk.Folder()
i = onedrivesdk.Item()
i.name = name
i.folder = f
return self.client.item(drive='me', id=parentID).children.add(i)
def upload(self, id, path, fileName):
try:
paths = fileName.split("/")
if len(paths) == 2:
directory = self.createFolder(id, paths[0])
self.client.item(drive='me', id=directory.id).children[paths[1]].upload(join(path, fileName))
except:
logging.error("Error when uploading file " + fileName)