1 from __future__ import print_function
6 def list_recursive(dirname):
7 for file in os.listdir(dirname):
8 fn = os.path.join(dirname, file)
11 elif os.path.isdir(fn):
12 for f in list_recursive(fn):
15 def remove_empty(dirname):
16 files = os.listdir(dirname)
18 for file in os.listdir(dirname):
19 fn = os.path.join(dirname, file)
25 except OSError as ose:
26 print("Unable to remove directory", dirname + ":", ose)
29 """Base class for browser APIs"""
30 def __init__(self, cache='/tmp/ecasa'):
33 def setCredentials(self, email, password):
36 def getAlbums(self, user='default'):
39 def getSearch(self, query, limit='10'):
42 def getAlbum(self, album):
45 def getTags(self, feed):
48 def getComments(self, feed):
51 def getFeatured(self):
54 def downloadPhoto(self, photo, thumbnail=False):
57 def downloadThumbnail(self, photo):
58 return self.downloadPhoto(photo, thumbnail=True)
60 def copyPhoto(self, photo, target, recursive=True):
61 """Attempt to copy photo from cache to given destination.
64 photo: photo object to download.
65 target: target filename
66 recursive (optional): attempt to download picture if it does not exist yet
69 True if image was copied successfully,
70 False if image did not exist and download was initiated,
74 shutil.Error if an error occured during moving the file.
78 def cleanupCache(self, maxSize):
79 """Housekeeping for our download cache.
81 Removes pictures and thumbnails (oldest to newest) until the cache is smaller than maxSize MB.
84 maxSize: maximum size of cache im MB.
87 maxSize *= 1048576 # input size is assumed to be in mb, but we work with bytes internally
89 files = [(f, stat(f)) for f in list_recursive(self.cache)]
90 curSize = sum(map(lambda x: x[1].st_size, files))
92 files.sort(key=lambda x: x[1].st_mtime)
93 while curSize > maxSize:
94 file, stat = files.pop(0)
97 except Exception as e:
98 print("[PicasaApi] Unable to unlink file", file + ":", e)
100 curSize -= stat.st_size
101 remove_empty(self.cache)
103 __all__ = ['PictureApi']