get OneDrive working

onedrive
eric 2019-04-24 13:38:14 -04:00
parent b81bf41015
commit 2e6fce455a
3 changed files with 51 additions and 26 deletions

View File

@ -23,6 +23,7 @@ host_mobile: 'm.gutenberg.org'
host_https: 1 host_https: 1
file_host: 'www.gutenberg.org' file_host: 'www.gutenberg.org'
sqlalchemy.pool_size: 20 sqlalchemy.pool_size: 20
sqlalchemy.max_overflow: 0 sqlalchemy.max_overflow: 0
sqlalchemy.timeout: 3 sqlalchemy.timeout: 3
@ -35,7 +36,7 @@ dropbox_client_secret: 'add secret in .autocat3 or /etc/autocat3.conf files'
gdrive_client_id: '586299000268.apps.googleusercontent.com' gdrive_client_id: '586299000268.apps.googleusercontent.com'
gdrive_client_secret: 'add secret in .autocat3 or /etc/autocat3.conf files' gdrive_client_secret: 'add secret in .autocat3 or /etc/autocat3.conf files'
msdrive_client_id: '0000000044111115' msdrive_client_id: '6902b111-a9d6-461f-bd8a-83dafee3da66'
msdrive_client_secret: 'add secret in .autocat3 or /etc/autocat3.conf files' msdrive_client_secret: 'add secret in .autocat3 or /etc/autocat3.conf files'
recaptcha_public_key: '6Lf0cpkUAAAAAIHB6OW_6H0SIqt5O2xmgsnF4Is8' recaptcha_public_key: '6Lf0cpkUAAAAAIHB6OW_6H0SIqt5O2xmgsnF4Is8'

View File

@ -135,7 +135,7 @@ class CloudStorage (object):
session_class = CloudOAuth2Session session_class = CloudOAuth2Session
user_agent = None user_agent = None
upload_endpoint = None upload_endpoint = None
re_filename = re.compile (r'[/\<>:"|?*]') re_filename = re.compile (r'[/\<>:"|?* ]')
def __init__ (self): def __init__ (self):

View File

@ -4,11 +4,10 @@
""" """
MSDrive.py MSDrive.py
Copyright 2014,15 by Marcello Perathoner
Distributable under the GNU General Public License Version 3 or newer. Distributable under the GNU General Public License Version 3 or newer.
The send-to-microsoft-drive pages. The send-to-onedrive pages using the Graph API
https://docs.microsoft.com/en-us/graph/api/driveitem-createuploadsession?view=graph-rest-1.0
""" """
@ -18,8 +17,7 @@ from contextlib import closing
import CloudStorage import CloudStorage
class MSDriveSession(CloudStorage.CloudOAuth2Session):
class MSDriveSession (CloudStorage.CloudOAuth2Session):
""" Hold parameters for OAuth. """ """ Hold parameters for OAuth. """
# #
@ -28,29 +26,55 @@ class MSDriveSession (CloudStorage.CloudOAuth2Session):
# http://msdn.microsoft.com/en-us/library/live/hh243649 # http://msdn.microsoft.com/en-us/library/live/hh243649
# #
name_prefix = 'msdrive' name_prefix = 'msdrive'
oauth2_auth_endpoint = 'https://login.microsoftonline.com/common/oauth2/v2.0/authorize' oauth2_auth_endpoint = 'https://login.microsoftonline.com/common/oauth2/v2.0/authorize'
oauth2_token_endpoint = 'https://login.microsoftonline.com/common/oauth2/v2.0/token' oauth2_token_endpoint = 'https://login.microsoftonline.com/common/oauth2/v2.0/token'
oauth2_scope = 'files.readwrite' oauth2_scope = 'Files.ReadWrite'
class MSDrive (CloudStorage.CloudStorage): class MSDrive(CloudStorage.CloudStorage):
""" Send files to Microsoft Drive. """ """ Send files to Microsoft OneDrive. """
name = 'OneDrive' name = 'OneDrive'
session_class = MSDriveSession session_class = MSDriveSession
user_agent = 'PG2OneDrive/2019.0' user_agent = 'PG2OneDrive/2019.0'
#upload_endpoint = 'https://apis.live.net/v5.0/me/skydrive/files/' #upload_endpoint = 'https://apis.live.net/v5.0/me/skydrive/files/'
upload_endpoint = 'https://graph.microsoft.com/v1.0/me/drive/items/root:/{filename}:/createUploadSession' upload_endpoint = 'https://graph.microsoft.com/v1.0/me/drive/items/root:/Documents/Gutenberg/{filename}:/createUploadSession'
def upload_file (self, session, request): def upload_file(self, session, response):
""" Upload a file to microsoft drive. """ """ Upload a file to microsoft onedrive. """
filename = self.fix_filename(session.ebook.get_filename())
item_data = {
'name': filename,
'description': 'A Project Gutenberg Ebook',
"@microsoft.graph.conflictBehavior": "rename",
}
filesize = int(response.headers['Content-Length'])
url = self.upload_endpoint.format(filename=filename)
chunk_size = 327680 # weird onedrive thing related to FAT tables
upload_data = session.post(url, json={'item': item_data}).json()
url = self.upload_endpoint.format( def headers(start, end, filesize):
{'filename': self.fix_filename (session.ebook.get_filename ())} return {
) 'Content-Length': str(end - start + 1),
'Content-Range': 'bytes {}-{}/{}'.format(start, end, filesize)
}
upload_session = session.post (url) if 'uploadUrl' in upload_data:
if 'uploadUrl' in upload_session: session_uri = upload_data['uploadUrl']
with closing (session.put (upload_session['uploadUrl'], data = request.iter_content (1024 * 1024))) as r: start = 0
r.raise_for_status () end = min(chunk_size - 1, filesize - 1)
for chunk in response.iter_content(chunk_size):
r = session.put(
session_uri,
data=chunk,
headers=headers(start, end, filesize),
)
start = start + chunk_size
end = min(end + chunk_size, filesize - 1)
r.raise_for_status()
else:
CloudStorage.log('no uploadUrl in %s' % upload_data)
session.close()