mirror of
https://github.com/fraoustin/piwigotools.git
synced 2025-06-07 16:06:27 +00:00
add cache
This commit is contained in:
parent
a4277d7e8b
commit
872d16abd6
@ -14,6 +14,9 @@ import requests
|
|||||||
|
|
||||||
import piwigo
|
import piwigo
|
||||||
|
|
||||||
|
FORCE_PLAN = False
|
||||||
|
FORCE_IMAGES = False
|
||||||
|
|
||||||
class LoginException(Exception):
|
class LoginException(Exception):
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
@ -41,6 +44,8 @@ class Piwigo(piwigo.Piwigo):
|
|||||||
def __init__(self, url):
|
def __init__(self, url):
|
||||||
piwigo.Piwigo.__init__(self, url)
|
piwigo.Piwigo.__init__(self, url)
|
||||||
self._login = False
|
self._login = False
|
||||||
|
self._plan = {}
|
||||||
|
self._images = {}
|
||||||
|
|
||||||
def login(self, username, password):
|
def login(self, username, password):
|
||||||
"""
|
"""
|
||||||
@ -60,9 +65,11 @@ class Piwigo(piwigo.Piwigo):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def plan(self):
|
def plan(self):
|
||||||
plan = { i["name"] : i["id"] for i in self.pwg.categories.getList(recursive=True, fullname=True)['categories'] }
|
if FORCE_PLAN or not len(self._plan):
|
||||||
plan[""] = 0
|
plan = { i["name"] : i["id"] for i in self.pwg.categories.getList(recursive=True, fullname=True)['categories'] }
|
||||||
return plan
|
plan[""] = 0
|
||||||
|
self._plan = plan
|
||||||
|
return self._plan
|
||||||
|
|
||||||
def _checkarg(fn):
|
def _checkarg(fn):
|
||||||
def checking(self, *args, **kw):
|
def checking(self, *args, **kw):
|
||||||
@ -114,21 +121,24 @@ class Piwigo(piwigo.Piwigo):
|
|||||||
"""
|
"""
|
||||||
return list of file name image for path
|
return list of file name image for path
|
||||||
"""
|
"""
|
||||||
try:
|
if FORCE_IMAGES or path not in self._images:
|
||||||
kw["cat_id"]= self.idcategory(path)
|
try:
|
||||||
except PiwigoExistException:
|
kw["cat_id"]= self.idcategory(path)
|
||||||
return {}
|
except PiwigoExistException:
|
||||||
kw["per_page"] = 200
|
return {}
|
||||||
kw["page"] = 0
|
kw["per_page"] = 200
|
||||||
imgs = {}
|
kw["page"] = 0
|
||||||
loop = True
|
imgs = {}
|
||||||
while loop:
|
loop = True
|
||||||
req = self.pwg.categories.getImages(**kw)
|
while loop:
|
||||||
for img in req["images"]:
|
req = self.pwg.categories.getImages(**kw)
|
||||||
imgs[img["file"]] = img
|
for img in req["images"]:
|
||||||
if req["paging"]["count"] < req["paging"]["per_page"]:
|
imgs[img["file"]] = img
|
||||||
loop = False
|
if req["paging"]["count"] < req["paging"]["per_page"]:
|
||||||
return imgs
|
loop = False
|
||||||
|
kw["page"] = kw["page"] + 1
|
||||||
|
self._images[path] = imgs
|
||||||
|
return self._images[path]
|
||||||
|
|
||||||
@_checkarg
|
@_checkarg
|
||||||
def sublevels(self, path, **kw):
|
def sublevels(self, path, **kw):
|
||||||
@ -149,11 +159,13 @@ class Piwigo(piwigo.Piwigo):
|
|||||||
|
|
||||||
@_checkarg
|
@_checkarg
|
||||||
def idimage(self, path):
|
def idimage(self, path):
|
||||||
if not self.isimage(path):
|
|
||||||
raise PiwigoExistException("image %s not exist" % path)
|
|
||||||
img = path.split(' / ')[-1]
|
img = path.split(' / ')[-1]
|
||||||
path = ' / '.join(path.split(' / ')[:-1])
|
path = ' / '.join(path.split(' / ')[:-1])
|
||||||
return self.images(path)[img]["id"]
|
try:
|
||||||
|
return self.images(path)[img]["id"]
|
||||||
|
except:
|
||||||
|
raise PiwigoExistException("image %s not exist" % path)
|
||||||
|
|
||||||
|
|
||||||
@_checkarg
|
@_checkarg
|
||||||
@_checklogin
|
@_checklogin
|
||||||
@ -166,7 +178,9 @@ class Piwigo(piwigo.Piwigo):
|
|||||||
if parent and not self.iscategory(parent):
|
if parent and not self.iscategory(parent):
|
||||||
raise PiwigoExistException("category %s not exist" % parent)
|
raise PiwigoExistException("category %s not exist" % parent)
|
||||||
if parent : kw['parent'] = self.plan[parent]
|
if parent : kw['parent'] = self.plan[parent]
|
||||||
self.pwg.categories.add(**kw)
|
id = self.pwg.categories.add(**kw)['id']
|
||||||
|
if not FORCE_PLAN:
|
||||||
|
self._plan[path] = id
|
||||||
return self.idcategory(path)
|
return self.idcategory(path)
|
||||||
|
|
||||||
@_checkarg
|
@_checkarg
|
||||||
@ -190,6 +204,8 @@ class Piwigo(piwigo.Piwigo):
|
|||||||
remove (delete) category
|
remove (delete) category
|
||||||
"""
|
"""
|
||||||
self.pwg.categories.delete(category_id=self.idcategory(path), pwg_token=self.token, **kw)
|
self.pwg.categories.delete(category_id=self.idcategory(path), pwg_token=self.token, **kw)
|
||||||
|
if not FORCE_PLAN and path in self._plan:
|
||||||
|
del self._plan[path]
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@_checkarg
|
@_checkarg
|
||||||
@ -201,9 +217,12 @@ class Piwigo(piwigo.Piwigo):
|
|||||||
kw["image"] = image
|
kw["image"] = image
|
||||||
if len(path):
|
if len(path):
|
||||||
if not self.iscategory(path):
|
if not self.iscategory(path):
|
||||||
raise PiwigoExistException("category %s not exist" % parent)
|
raise PiwigoExistException("category %s not exist" % path)
|
||||||
kw['category'] = self.idcategory(path)
|
kw['category'] = self.idcategory(path)
|
||||||
return self.pwg.images.addSimple(**kw)['image_id']
|
res = self.pwg.images.addSimple(**kw)['image_id']
|
||||||
|
if ' / '.join(path.split(' / ')[:-1]) in self._images:
|
||||||
|
del self._images[' / '.join(path.split(' / ')[:-1])]
|
||||||
|
return res
|
||||||
|
|
||||||
@_checkarg
|
@_checkarg
|
||||||
@_checklogin
|
@_checklogin
|
||||||
@ -231,4 +250,6 @@ class Piwigo(piwigo.Piwigo):
|
|||||||
if not self.isimage(path):
|
if not self.isimage(path):
|
||||||
raise PiwigoException("image %s not exist" % path)
|
raise PiwigoException("image %s not exist" % path)
|
||||||
self.pwg.images.delete(image_id= self.idimage(path), pwg_token=self.token)
|
self.pwg.images.delete(image_id= self.idimage(path), pwg_token=self.token)
|
||||||
|
if ' / '.join(path.split(' / ')[:-1]) in self._images:
|
||||||
|
del self._images[' / '.join(path.split(' / ')[:-1])]
|
||||||
return True
|
return True
|
||||||
|
@ -212,6 +212,8 @@ def main():
|
|||||||
if not os.path.isdir(options.source):
|
if not os.path.isdir(options.source):
|
||||||
raise Exception("%s is not directory" % options.source)
|
raise Exception("%s is not directory" % options.source)
|
||||||
piwigo.iscategory(options.category)
|
piwigo.iscategory(options.category)
|
||||||
|
if len(options.category) and options.category[-1] != '/' and options.category[:-3] != ' / ':
|
||||||
|
options.category = options.category+ ' / '
|
||||||
# treatment
|
# treatment
|
||||||
run = Run(verb, options.thread)
|
run = Run(verb, options.thread)
|
||||||
kw = purge_kw(options.__dict__,('user','password','url','source','category','thread'))
|
kw = purge_kw(options.__dict__,('user','password','url','source','category','thread'))
|
||||||
@ -221,22 +223,24 @@ def main():
|
|||||||
for ext in options.extension.split(',')[1:]:
|
for ext in options.extension.split(',')[1:]:
|
||||||
filtering = filtering + fnmatch.filter(filenames, ext)
|
filtering = filtering + fnmatch.filter(filenames, ext)
|
||||||
for filename in filtering:
|
for filename in filtering:
|
||||||
path = os.path.abspath(os.path.join(root, filename))[len(options.source)+1:]
|
pathrel = os.path.abspath(os.path.join(root, filename))[len(options.source)+1:]
|
||||||
if not piwigo.isimage(path.replace(os.sep, ' / ')):
|
pathabs = os.path.abspath(os.path.join(root, filename))
|
||||||
category = ' / '.join(path.split(os.sep)[:-1])
|
category = options.category + ' / '.join(pathrel.split(os.sep)[:-1])
|
||||||
|
if not piwigo.isimage(category + ' / ' + filename):
|
||||||
run.add(piwigo.makedirs,[category,], kw)
|
run.add(piwigo.makedirs,[category,], kw)
|
||||||
run.add(piwigo.upload,[path, category], kw)
|
run.add(piwigo.upload,[pathabs, category], kw)
|
||||||
# piwigo -> local
|
# piwigo -> local
|
||||||
for category, item in piwigo.plan.iteritems():
|
#for category, item in piwigo.plan.iteritems():
|
||||||
path = os.path.join(options.source, *category.split(' / '))
|
# if options.category == category[0:len(options.category)]:
|
||||||
if not os.path.exists(path):
|
# path = os.path.join(options.source, *category[len(options.category):].split(' / '))
|
||||||
os.makedirs(path)
|
# if not os.path.exists(path):
|
||||||
for img in piwigo.images(category):
|
# os.makedirs(path)
|
||||||
pathimg = os.path.join(path, img)
|
# for img in piwigo.images(category):
|
||||||
if not os.path.exists(pathimg):
|
# pathimg = os.path.join(path, img)
|
||||||
run.add(piwigo.download,
|
# if not os.path.exists(pathimg):
|
||||||
["%s / %s" % (category, str(img)), pathimg],
|
# run.add(piwigo.download,
|
||||||
kw)
|
# ["%s / %s" % (category, str(img)), pathimg],
|
||||||
|
# kw)
|
||||||
ana.stop()
|
ana.stop()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
ana.stop()
|
ana.stop()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user