ecasa: some code adjustments for future extensions
[enigma2-plugins.git] / ecasa / src / PictureApi.py
1 from __future__ import print_function
2
3 import os
4 import shutil
5
6 def list_recursive(dirname):
7         for file in os.listdir(dirname):
8                 fn = os.path.join(dirname, file)
9                 if os.path.isfile(fn):
10                         yield fn
11                 elif os.path.isdir(fn):
12                         for f in list_recursive(fn):
13                                 yield f
14
15 def remove_empty(dirname):
16         files = os.listdir(dirname)
17         if files:
18                 for file in os.listdir(dirname):
19                         fn = os.path.join(dirname, file)
20                         if os.path.isdir(fn):
21                                 remove_empty(fn)
22         else:
23                 try:
24                         os.rmdir(dirname)
25                 except OSError as ose:
26                         print("Unable to remove directory", dirname + ":", ose)
27
28 class PictureApi:
29         """Base class for browser APIs"""
30         def __init__(self, cache='/tmp/ecasa'):
31                 self.cache = cache
32
33         def setCredentials(self, email, password):
34                 pass
35
36         def getAlbums(self, user='default'):
37                 pass
38
39         def getSearch(self, query, limit='10'):
40                 pass
41
42         def getAlbum(self, album):
43                 pass
44
45         def getTags(self, feed):
46                 pass
47
48         def getComments(self, feed):
49                 pass
50
51         def getFeatured(self):
52                 pass
53
54         def downloadPhoto(self, photo, thumbnail=False):
55                 pass
56
57         def downloadThumbnail(self, photo):
58                 return self.downloadPhoto(photo, thumbnail=True)
59
60         def copyPhoto(self, photo, target, recursive=True):
61                 """Attempt to copy photo from cache to given destination.
62
63                 Arguments:
64                 photo: photo object to download.
65                 target: target filename
66                 recursive (optional): attempt to download picture if it does not exist yet
67
68                 Returns:
69                 True if image was copied successfully,
70                 False if image did not exist and download was initiated,
71                 otherwise None.
72
73                 Raises:
74                 shutil.Error if an error occured during moving the file.
75                 """
76                 pass
77
78         def cleanupCache(self, maxSize):
79                 """Housekeeping for our download cache.
80
81                 Removes pictures and thumbnails (oldest to newest) until the cache is smaller than maxSize MB.
82
83                 Arguments:
84                 maxSize: maximum size of cache im MB.
85                 """
86                 stat = os.stat
87                 maxSize *= 1048576 # input size is assumed to be in mb, but we work with bytes internally
88
89                 files = [(f, stat(f)) for f in list_recursive(self.cache)]
90                 curSize = sum(map(lambda x: x[1].st_size, files))
91                 if curSize > maxSize:
92                         files.sort(key=lambda x: x[1].st_mtime)
93                         while curSize > maxSize:
94                                 file, stat = files.pop(0)
95                                 try:
96                                         os.unlink(file)
97                                 except Exception as e:
98                                         print("[PicasaApi] Unable to unlink file", file + ":", e)
99                                 else:
100                                         curSize -= stat.st_size
101                         remove_empty(self.cache)
102
103 __all__ = ['PictureApi']