No Description

__init__.py 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. Module piwigotools
  5. """
  6. __version_info__ = (0, 0, 2)
  7. __version__ = '.'.join([str(val) for val in __version_info__])
  8. import inspect
  9. import requests
  10. import piwigo
  11. class LoginException(Exception):
  12. def __str__(self):
  13. return "You are not logged"
  14. class PiwigoException(Exception):
  15. def __init__(self, strerr):
  16. self._strerr = strerr
  17. def __str__(self):
  18. return self._strerr
  19. class PiwigoExistException(PiwigoException):
  20. def __init__(self, strerr):
  21. PiwigoException.__init__(self, strerr)
  22. class Piwigo(piwigo.Piwigo):
  23. """
  24. describe piwigo gallery
  25. """
  26. def __init__(self, url):
  27. piwigo.Piwigo.__init__(self, url)
  28. self._login = False
  29. def login(self, username, password):
  30. """
  31. login on piwigo gallery
  32. """
  33. self.pwg.session.login(username=username, password=password)
  34. self._login = True
  35. return True
  36. def logout(self):
  37. """
  38. logout on piwigo gallery
  39. """
  40. self.pwg.session.logout()
  41. self._login = False
  42. return True
  43. @property
  44. def plan(self):
  45. return { i["name"] : i["id"] for i in self.pwg.categories.getList(recursive=True, fullname=True)['categories'] }
  46. def _checkarg(fn):
  47. def checking(self, *args, **kw):
  48. args = list(args)
  49. # manage path
  50. if inspect.getargspec(fn).args.count('path'):
  51. pos = inspect.getargspec(fn).args.index('path') -1
  52. if args[pos][-2:] == ' /' : args[pos] = args[pos][:-2]
  53. args = tuple(args)
  54. return fn(self, *args, **kw)
  55. return checking
  56. def _checklogin(fn):
  57. def checking(self, *args, **kw):
  58. if self._login:
  59. return fn(self, *args, **kw)
  60. raise LoginException()
  61. return checking
  62. @property
  63. @_checklogin
  64. def token(self):
  65. """
  66. return pwg_token
  67. """
  68. return self.pwg.session.getStatus()["pwg_token"]
  69. def islogged(self):
  70. try:
  71. self.token
  72. except LoginException:
  73. return False
  74. return True
  75. @_checkarg
  76. def iscategory(self, path):
  77. if path in self.plan:
  78. return True
  79. return False
  80. @_checkarg
  81. def idcategory(self, path):
  82. if not self.iscategory(path):
  83. raise PiwigoExistException("category %s not exist" % path)
  84. return self.plan[path]
  85. @_checkarg
  86. def images(self, path, **kw):
  87. """
  88. return list of file name image for path
  89. """
  90. kw["cat_id"]= self.idcategory(path)
  91. kw["per_page"] = 200
  92. kw["page"] = 0
  93. imgs = {}
  94. loop = True
  95. while loop:
  96. req = self.pwg.categories.getImages(**kw)
  97. for img in req["images"]:
  98. imgs[img["file"]] = img
  99. if req["paging"]["count"] < req["paging"]["per_page"]:
  100. loop = False
  101. return imgs
  102. @_checkarg
  103. def sublevels(self, path, **kw):
  104. """
  105. return list of category in for path
  106. """
  107. kw["cat_id"]= self.idcategory(path)
  108. return { i["name"] : i for i in self.pwg.categories.getList(**kw)['categories'] if i["id"] != kw["cat_id"] }
  109. @_checkarg
  110. def isimage(self, path):
  111. img = path.split(' / ')[-1]
  112. path = ' / '.join(path.split(' / ')[:-1])
  113. if img in self.images(path):
  114. return True
  115. return False
  116. @_checkarg
  117. def idimage(self, path):
  118. if not self.isimage(path):
  119. raise PiwigoExistException("image %s not exist" % path)
  120. img = path.split(' / ')[-1]
  121. path = ' / '.join(path.split(' / ')[:-1])
  122. return self.images(path)[img]["id"]
  123. @_checkarg
  124. @_checklogin
  125. def mkdir(self, path, **kw):
  126. """
  127. create a category named path
  128. """
  129. kw['name'] = path.split(' / ')[-1]
  130. parent = ' / '.join(path.split(' / ')[:-1])
  131. if parent and not self.iscategory(parent):
  132. raise PiwigoExistException("category %s not exist" % parent)
  133. if parent : kw['parent'] = self.plan[parent]
  134. self.pwg.categories.add(**kw)
  135. return self.idcategory(path)
  136. @_checkarg
  137. @_checklogin
  138. def makedirs(self, path, **kw):
  139. """
  140. recursive category create function
  141. """
  142. pp = ''
  143. for p in path.split(' / '):
  144. pp = '%s%s' % (pp, p)
  145. if not self.iscategory(pp):
  146. self.mkdir(pp, **kw)
  147. pp = '%s / ' % pp
  148. return self.idcategory(path)
  149. @_checkarg
  150. @_checklogin
  151. def removedirs(self, path, **kw):
  152. """
  153. remove (delete) category
  154. """
  155. self.pwg.categories.delete(category_id=self.idcategory(path), pwg_token=self.token, **kw)
  156. return True
  157. @_checkarg
  158. @_checklogin
  159. def upload(self, image, path="", **kw):
  160. """
  161. upload image in path
  162. """
  163. kw["image"] = image
  164. if len(path):
  165. if not self.iscategory(path):
  166. raise PiwigoExistException("category %s not exist" % parent)
  167. kw['category'] = self.idcategory(path)
  168. return self.pwg.images.addSimple(**kw)['image_id']
  169. @_checkarg
  170. @_checklogin
  171. def download(self, path, dst, **kw):
  172. """
  173. download image dst
  174. """
  175. if not self.isimage(path):
  176. raise PiwigoException("image %s not exist" % path)
  177. img = path.split(' / ')[-1]
  178. path = ' / '.join(path.split(' / ')[:-1])
  179. url = self.images(path)[img]['element_url']
  180. with open(dst, 'wb') as img:
  181. r = requests.get(url)
  182. img.write(r.content)
  183. r.connection.close()
  184. return True
  185. @_checklogin
  186. def remove(self, path, **kw):
  187. """
  188. remove (delete) image
  189. """
  190. if not self.isimage(path):
  191. raise PiwigoException("image %s not exist" % path)
  192. self.pwg.images.delete(image_id= self.idimage(path), pwg_token=self.token)
  193. return True