-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGPhoto.py
209 lines (187 loc) · 7.65 KB
/
GPhoto.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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import apiclient
import oauth2client
from oauth2client import tools
from oauth2client.file import Storage
from apiclient import discovery
import httplib2
import oauth2client
import os
import ItemCache
try:
import argparse
flags = argparse.ArgumentParser(parents=[tools.argparser], add_help=False)
except:
flags = None
OAUTH2_SCOPE = 'https://www.googleapis.com/auth/drive'
DIR_MIME = 'application/vnd.google-apps.folder'
class _GPIter(object):
def __init__(self, keys):
self.keys = keys
self.count = 0
def __iter__(self):
return self
def __next__(self):
if self.count < len(self.keys):
result = self.keys[self.count]
self.count += 1
return result
raise StopIteration
class _GPhotoCache(object):
def __init__(self):
self.cache = ItemCache.ItemDiskCache('GPhoto.cache', '/tmp', None)
self.rcache = ItemCache.ItemDiskCache('GPhoto.rcache', '/tmp', None)
self.cache['root'] = ("/", "")
self.rcache[self._rkey("/", "")] = 'root'
def __len__(self):
return len(self.cache)
def __getitem__(self, file_id):
return self.cache[file_id]
def __setitem__(self, file_id, value):
(title, parent_id) = value
self.cache[file_id] = (title, parent_id)
self.rcache[self._rkey(title, parent_id)] = file_id
def __delitem__(self, file_id):
(title, parent_id) = self.cache[file_id]
del self.cache[file_id]
del self.rcache[self._rkey(title, parent_id)]
def __iter__(self):
return _GPIter(self.cache.keys())
def __contains__(self, file_id):
return file_id in self.cache
def _rkey(self, title, parent_id):
return str(title + '::' + parent_id)
def find(self, title, parent_id):
try:
return self.rcache[self._rkey(title, parent_id)]
except:
for c in self.cache:
(t, p_id) = self.cache[c]
if (parent_id == p_id) and (title == t):
self.rcache[self._rkey(title, parent_id)] = c
return c
return None
class GPhoto(object):
def __init__(self, oauth2json = None, oauth2storage = None):
self.oauth2json = oauth2json
self.oauth2storage = oauth2storage
self.store = None
self.creds = None
self.service = None
self.cache = _GPhotoCache()
def auth(self, oauth2json = None, oauth2storage = None):
if oauth2json is not None:
self.oauth2json = oauth2json
if oauth2storage is not None:
self.oauth2storage = oauth2storage
if self.oauth2json is None:
raise ValueError('Attribute oauth2json needs to be defined')
if self.oauth2storage is None:
raise ValueError('Attribute oauth2storage needs to be defined')
self.store = Storage(self.oauth2storage)
self.creds = self.store.get()
if self.creds is None or self.creds.invalid:
flow = oauth2client.client.flow_from_clientsecrets(self.oauth2json, OAUTH2_SCOPE)
if flags is not None:
self.creds = oauth2client.tools.run_flow(flow, self.store, flags.parse_args())
else:
self.creds = oauth2client.tools.run(flow, self.store)
self.store.put(self.creds)
self.service = discovery.build('drive', 'v2', http = self.creds.authorize(httplib2.Http()))
def create_dir(self, folder_title, parent_folder = 'root'):
# Check whether the directory does not already exists in cache
dir_id = self.cache.find(folder_title, parent_folder)
if dir_id is not None:
dentry = self.cache[dir_id]
return {
'id': dir_id,
'title': dentry[0],
'parents': [{"id": dentry[1]}],
'mimeType': DIR_MIME,
}
# The directory has not been found in the cache, lets ask Google
body = {
"title": folder_title,
"parents": [{"id": parent_folder}],
"mimeType": DIR_MIME,
}
directory = self.service.files().insert(body = body).execute()
self.cache[directory['id']] = (folder_title, parent_folder)
return directory
def upload_file(self, filename, parent_folder = 'root'):
media_body = apiclient.http.MediaFileUpload(filename, resumable = True)
basename = os.path.basename(filename)
body = {
"title": basename,
"parents": [{"id": parent_folder}],
}
try:
f = self.service.files().insert(body = body, media_body = media_body).execute()
self.cache[f['id']] = (basename, parent_folder)
return f
except apiclient.errors.HttpError as error:
return None
def get_id(self, path):
path_list = path.split('/')
pid = 'root'
for p in path_list:
fid = self.get_file_id(p, pid)
if fid is None:
return None
pid = fid
return fid
def get_file_id(self, title, parent_id = 'root'):
# Try cache
cache = self.cache.find(title, parent_id)
if cache is not None:
return cache
# Not found in cache - ask Google
page_token = None
while True:
try:
param = {}
if page_token:
param['pageToken'] = page_token
children = self.service.children().list(folderId = parent_id, **param).execute()
for child in children.get('items', []):
if child['id'] not in self.cache:
ch = self.service.files().get(fileId = child['id']).execute()
self.cache[ch['id']] = (ch['title'], parent_id)
if ch['title'] == title:
return child['id']
page_token = children.get('nextPageToken')
if not page_token:
break
except apiclient.errors.HttpError as error:
raise
return None
def file_exists(self, file_name, root_dir = 'root'):
fn = file_name.split('/')
file_id = self.get_file_id(fn[0], root_dir)
if file_id is None:
return False
if len(fn) == 1:
# Check existence of the file
return file_id is not None
# Go one level deeper
fn.pop(0)
return self.file_exists('/'.join(fn), file_id)
if __name__ == "__main__":
oauth2json = os.path.expanduser('~/.gp.json')
oauth2storage = os.path.expanduser('~/.gp')
gp = GPhoto(oauth2json = oauth2json, oauth2storage = oauth2storage)
gp.auth()
#d = gp.create_dir("BufGuf")
#gp.upload_file(os.path.expanduser('~/fscheck.sh'), d['id'])
#gp.upload_file(os.path.expanduser('~/readiness-f23-alpha.txt'), d['id'])
#print 'U-Temp', gp.file_exists('U-Temp')
#print 'Work/Links', gp.file_exists('Work/Links')
#print 'Personal', gp.file_exists('Personal')
#print 'Personal/Blbost', gp.file_exists('Personal/Blbost')
#print 'Pictures/Foto/2015/07/29', gp.file_exists('Pictures/Foto/2015/07/29')
print('Pictures/Foto/2015/07/29/IMG_2552.JPG', gp.file_exists('Pictures/Foto/2015/07/29/IMG_2552.JPG'))
print('Pictures/Foto/2015/07/29', gp.file_exists('Pictures/Foto/2015/07/29'))
print('Pictures/Foto/2015/07/29/', gp.file_exists('Pictures/Foto/2015/07/29/'))
print('Pictures/Foto/2015/07/29/IMG_2552.jpg', gp.file_exists('Pictures/Foto/2015/07/29/IMG_2552.jpg'))
print('Pictures/Foto/2015/07/29/IMG2552.JPG', gp.file_exists('Pictures/Foto/2015/07/29/IMG2552.JPG'))