-
Notifications
You must be signed in to change notification settings - Fork 0
/
syncasa
executable file
·340 lines (294 loc) · 12.5 KB
/
syncasa
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#!/usr/bin/python
# -*- coding: utf-8 -*-
from gdata.photos import service, AlbumEntryFromString, PhotoEntryFromString
from mimetypes import guess_type
from pyinotify import WatchManager, Notifier, ProcessEvent
from xml.dom import minidom
import pyinotify as p
import logging as l
import os
import shutil
import sys
G_CLIENT = None
PATH = None
class Syncasa(ProcessEvent):
__slots__ = ('wm', 'wdd', 'notifier', 'albums', 'push')
def __init__(self, push=False):
ProcessEvent.__init__(self)
# Object needed for pyinotify
self.wm = WatchManager()
mask = p.IN_CREATE | p.IN_CLOSE_WRITE | p.IN_DELETE | p.IN_DELETE_SELF
mask |= p.IN_MOVED_TO | p.IN_MOVED_FROM | p.IN_MOVE_SELF
self.wdd = self.wm.add_watch(PATH, mask, rec=True, auto_add=True)
# Thread to treat the queue of event
self.notifier = Notifier(self.wm, self)
self.albums = {}
self.push = push
def __getattr__(self, name):
return None
def start(self, blacklist):
# Little hack as GetUserFeed was not normally prepared for this
fields = 'album&v=4.0&fields=entry(gphoto:id,title)'
o_albums = G_CLIENT.GetUserFeed(kind=fields).ToString()
#Delete album online but not locally
for title, album_id in parse_feed(o_albums):
if title in blacklist:
continue
o_album_path = os.path.join(PATH, title)
if os.path.exists(o_album_path):
# The album exists also locally
l.debug("%s exists both ways, syncing...", title)
self.albums[title] = Album(title, int(album_id))
if self.push:
del self.albums[title]
shutil.rmtree(o_album_path)
elif not self.push:
l.debug("%s does not exist locally, deleting..", o_album_path)
url = '/data/entry/api/user/default/albumid/%s' % album_id
G_CLIENT.Delete(url, extra_headers={'If-Match':'*'})
# Find the album that exists locally but not yet online
for album in os.listdir(PATH):
a_path = os.path.join(PATH, album)
if not os.path.isdir(a_path) or album[0] == '.':
continue
# The album does not exists online, we will create it
if album not in self.albums:
self.albums[album] = Album(album)
if self.push:
del self.albums[album]
shutil.rmtree(a_path)
# Loop forever and handle events.
# It seems the CTR+C event is taken care of by pyinotify
self.notifier.loop()
def process_IN_CREATE(self, event):
if event.dir:
# Create the album online
self.albums[event.name] = Album(event.name)
if self.push:
del self.albums[event.name]
shutil.rmtree(os.path.join(PATH, event.name))
return
def process_IN_CLOSE_WRITE(self, event):
if event.dir or self.push:
# We are not interested in theses events
return
# Create photo when created picture (in any way)
album = event.path.split('/')[-1]
if album not in self.albums:
return
self.albums[album].photos[event.name] = Photo(self.albums[album],
event.name)
def process_IN_DELETE(self, event):
if event.dir or self.push:
return
album = event.path.split('/')[-1]
if album not in self.albums:
return
self.albums[album].photos[event.name].delete()
del self.albums[album].photos[event.name]
def process_IN_DELETE_SELF(self, event):
if not event.dir or self.push:
return
if event.name not in self.albums:
return
self.albums[event.name].delete()
del self.albums[event.name]
watch_id = self.wm.get_wd(event.path)
self.wm.rm_watch(watch_id)
def process_IN_MOVED_TO(self, event):
if not hasattr(event, 'src_pathname'):
# File from a non watched directory to a watched one.
# Treat it as a create
if event.dir:
self.process_IN_CREATE(event)
else:
self.process_IN_CLOSE_WRITE(event)
return
if self.push:
return
old_name = event.src_pathname.split('/')[-1]
l.debug("Rename %s -> %s", old_name, event.name)
if event.dir:
self.albums[old_name].rename(event.name)
self.albums[event.name] = self.albums.pop(old_name)
return
album = event.path.split('/')[-1]
self.albums[album].photos[old_name].rename(event.name)
self.albums[album].photos[event.name] = self.albums[album].photos.pop(old_name)
def process_IN_MOVE_SELF(self, event):
# Moving from a watched directory to somewhere externals
# Treat as a delete
if "-unknown-path" not in event.path or self.push:
return
tmp_path = event.path.split('-unknown-path')[0]
event.name = tmp_path.split('/')[-1]
if event.dir:
self.process_IN_DELETE_SELF(event)
else:
event.path = tmp_path
self.process_IN_DELETE(event)
class Album():
__slots__ = ('name', 'album_id', 'fields', 'photos')
def __init__(self, name, album_id=None):
self.name = name
self.album_id = album_id
self.fields = 'kind=photo&v=4.0&fields=entry(gphoto:id,title)'
if self.album_id is None:
l.debug("Creating album %s online.", self.name)
g_obj = G_CLIENT.InsertAlbum(title=self.name,
summary="Sync of %s folder" % self.name, access='private')
self.album_id = int(g_obj.gphoto_id.text)
self.photos = {}
o_photos_url = '/data/feed/api/user/default/albumid/%d?%s' % (
self.album_id, self.fields)
o_photos = G_CLIENT.GetFeed(o_photos_url).ToString()
# Delete the picture online but not present locally
for title, id in parse_feed(o_photos):
pic_path = os.path.join(PATH, self.name, title)
if not os.path.exists(pic_path):
l.debug("%s does not exist locally, deleting.", pic_path)
url = '/data/entry/api/user/default/albumid/%d/photoid/%s' % (
self.album_id, id)
G_CLIENT.Delete(url, extra_headers={'If-Match':'*'})
else:
# The photo exists locally
self.photos[title] = Photo(self, title, int(id))
for photo in os.listdir(os.path.join(PATH, self.name)):
file_path = os.path.join(PATH, self.name, photo)
# Avoid directory and hidden file
if not os.path.isfile(file_path) or photo[0] == '.':
continue
# Check that it's an image
file_mime_type = guess_type(photo)
if file_mime_type[0] is None \
or not file_mime_type[0].startswith("image/"):
continue
# If the photo is not online, we must create it
if photo not in self.photos:
self.photos[photo] = Photo(self, photo)
def __getattr__(self, name):
return None
def rename(self, new_name):
url = '/data/entry/api/user/default/albumid/%d?v=4.0' % self.album_id
g_obj = G_CLIENT.Get(url, converter=AlbumEntryFromString)
g_obj.title.text = new_name
g_obj = G_CLIENT.Put(g_obj, url, converter=AlbumEntryFromString)
self.name = new_name
def delete(self):
l.debug("Remove %s...", self.name)
url = '/data/entry/api/user/default/albumid/%d' % self.album_id
G_CLIENT.Delete(url, extra_headers={'If-Match':'*'})
class Photo():
__slots__ = ('album', 'name', 'photo_id')
def __init__(self, album, name, photo_id=None):
# Setup the photo, if it doesn't exists, upload it
self.album = album
self.name = name
self.photo_id = photo_id
if self.photo_id is None:
pic_path = os.path.join(PATH, self.album.name, name)
fd = open(pic_path, 'rb')
l.debug("Adding %s/%s...", self.album.name, self.name)
url = "/data/feed/api/user/default/albumid/%s" % self.album.album_id
try:
g_obj = G_CLIENT.InsertPhotoSimple(url, self.name,
'Uploaded using Syncasa', fd)
self.photo_id = int(g_obj.gphoto_id.text)
except Exception as gpe:
l.error(gpe)
self.photo_id = None
fd.close()
def __getattr__(self, name):
return None
def delete(self):
# Delete photo from online
l.debug("Remove %s/%s...", self.album.name, self.name)
url = "/data/feed/api/user/default/albumid/%d/photoid/%d" % (
self.album.album_id, self.photo_id)
G_CLIENT.Delete(url, extra_headers={'If-Match':'*'})
def rename(self, new_name):
url = '/data/entry/api/user/default/albumid/%d/photoid/%d?v=4.0' % (
self.album.album_id, self.photo_id)
g_obj = G_CLIENT.Get(url, converter=PhotoEntryFromString)
g_obj.title.text = new_name
g_obj = G_CLIENT.Put(g_obj, url, converter=PhotoEntryFromString)
self.name = new_name
###############################################################################
def login(user, pwd):
# log into the service
tmp_client = service.PhotosService()
tmp_client.email = user
tmp_client.password = pwd
tmp_client.source = 'syncasa'
try:
tmp_client.ProgrammaticLogin()
except Exception as err:
l.error("Error while authenticating : %s", err)
return None
return tmp_client
def parse_feed(feed):
data = minidom.parseString(feed)
entries = data.getElementsByTagName('ns0:entry')
for entry in entries:
title = entry.getElementsByTagName('ns0:title')[0].firstChild.data
id = entry.getElementsByTagName('ns1:id')[0].firstChild.data
yield title, id
def main():
''' launch everything '''
global G_CLIENT, PATH
import argparse
import ConfigParser
config = ConfigParser.RawConfigParser()
parser = argparse.ArgumentParser(prog='syncasa')
parser.add_argument("folder",
help="Folder to watch, default is the current path.",
default=os.getcwd())
parser.add_argument("-c", "--config", dest="config", action="store",
default="%s/.syncasa" % os.getenv("HOME"),
help="Specify a config file to use, default will be in"
" your home."
)
parser.add_argument("-p", "--push", dest="push", action="store_const",
const=True, default=False, help="Push files to G+ instead"
" of keeping them in sync. This will delete local files"
"once they are uploaded")
parser.add_argument("-d", "--debug", dest="loglevel", action="store_const",
const=l.DEBUG, default=l.INFO, help="Output"
" debugging information")
options = parser.parse_args()
PATH = options.folder
l.basicConfig(level=options.loglevel, format='%(message)s')
p.log.setLevel(l.CRITICAL)
if not os.path.exists(options.config):
import getpass
config.add_section('credential')
config.set('credential', 'username', raw_input("Username : "))
config.set('credential', 'password', getpass.getpass())
config.add_section('blacklist')
config.set('blacklist', 'names', raw_input("Album name to blacklist: "))
with open(options.config, 'w') as cred:
config.write(cred)
# Chmod it
os.chmod(options.config, 0600)
# Check the rights of the config file
import stat
if oct(stat.S_IMODE(os.stat(options.config).st_mode)) != "0600":
l.error("%s must have 0600 rights, correct it.", options.config)
return 1
# Open config file and get username/password
config.read(options.config)
userid = config.get('credential', 'username')
pwd = config.get('credential', 'password')
blacklist = config.get('blacklist', 'names').split(',')
G_CLIENT = login(userid, pwd)
if G_CLIENT is None:
l.error("Error while authenticating.")
return 0
picasa = Syncasa(push=options.push)
picasa.start(blacklist)
return 0
if __name__ == '__main__':
try:
sys.exit(main())
except (KeyboardInterrupt):
pass