-
Notifications
You must be signed in to change notification settings - Fork 7
/
GDrive.py
73 lines (54 loc) · 2.36 KB
/
GDrive.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
#!/usr/bin/env python
# -*- mode: python; indent-tabs-mode: nil; -*- coding: utf-8 -*-
"""
GDrive.py
Copyright 2013-15 by Marcello Perathoner
Distributable under the GNU General Public License Version 3 or newer.
The send-to-google-drive pages.
"""
from __future__ import unicode_literals
from contextlib import closing
import json
import CloudStorage
class GDriveSession (CloudStorage.CloudOAuth2Session):
""" Hold parameters for OAuth. """
#
# OAuth 2.0 flow see:
# http://tools.ietf.org/html/rfc6749
# https://developers.google.com/api-client-library/python/guide/aaa_oauth
#
name_prefix = 'gdrive'
oauth2_auth_endpoint = 'https://accounts.google.com/o/oauth2/auth'
oauth2_token_endpoint = 'https://accounts.google.com/o/oauth2/token'
# Check https://developers.google.com/drive/web/scopes for all available scopes
oauth2_scope = 'https://www.googleapis.com/auth/drive.file'
class GDrive (CloudStorage.CloudStorage):
""" Send files to Google Drive. """
name = 'Google Drive'
session_class = GDriveSession
user_agent = 'PG2GDrive/0.2'
upload_endpoint = 'https://www.googleapis.com/upload/drive/v2/files?uploadType=resumable'
def upload_file (self, session, request):
""" Upload a file to google drive. """
file_metadata = {
'title': self.fix_filename (session.ebook.get_filename ()),
'description': 'A Project Gutenberg Ebook',
}
headers = {
'X-Upload-Content-Type': request.headers['Content-Type'],
'X-Upload-Content-Length': str(len(request.content)),
'Content-Type': 'application/json; charset=UTF-8',
}
with session as s:
with closing (s.post (self.upload_endpoint,
data = json.dumps (file_metadata),
headers = headers)) as r2:
r2.raise_for_status ()
session_uri = r2.headers['Location']
headers = {
'Content-Type': request.headers['Content-Type'],
}
with closing (s.put (session_uri,
data = request.iter_content (1024 * 1024),
headers = headers)) as r3:
r3.raise_for_status ()