1
0
mirror of https://github.com/fraoustin/piwigotools.git synced 2025-07-10 07:58:37 +00:00

Compare commits

..

No commits in common. "master" and "V0.2" have entirely different histories.
master ... V0.2

4 changed files with 34 additions and 170 deletions

View File

@ -1,23 +1,3 @@
0.1.3
=====
- correction per page in images
0.1.2
=====
- correction per page to int
0.1.1
=====
- correction of help for piwigotools.main
0.1.0
=====
- add verb sync-up and sync-down
0.0.2
=====

View File

@ -5,7 +5,7 @@
Module piwigotools
"""
__version_info__ = (0, 3, 0)
__version_info__ = (0, 0, 2)
__version__ = '.'.join([str(val) for val in __version_info__])
import inspect
@ -14,9 +14,6 @@ import requests
import piwigo
FORCE_PLAN = False
FORCE_IMAGES = False
class LoginException(Exception):
def __str__(self):
@ -44,8 +41,6 @@ class Piwigo(piwigo.Piwigo):
def __init__(self, url):
piwigo.Piwigo.__init__(self, url)
self._login = False
self._plan = {}
self._images = {}
def login(self, username, password):
"""
@ -65,11 +60,8 @@ class Piwigo(piwigo.Piwigo):
@property
def plan(self):
if FORCE_PLAN or not len(self._plan):
plan = { i["name"] : i["id"] for i in self.pwg.categories.getList(recursive=True, fullname=True)['categories'] }
plan[""] = 0
self._plan = plan
return self._plan
return { i["name"] : i["id"] for i in self.pwg.categories.getList(recursive=True, fullname=True)['categories'] }
def _checkarg(fn):
def checking(self, *args, **kw):
@ -121,24 +113,18 @@ class Piwigo(piwigo.Piwigo):
"""
return list of file name image for path
"""
if FORCE_IMAGES or path not in self._images:
try:
kw["cat_id"]= self.idcategory(path)
except PiwigoExistException:
return {}
kw["per_page"] = 200
kw["page"] = 0
imgs = {}
loop = True
while loop:
req = self.pwg.categories.getImages(**kw)
for img in req["images"]:
imgs[img["file"]] = img
if len(req["images"]) < req["paging"]["per_page"]:
loop = False
kw["page"] = kw["page"] + 1
self._images[path] = imgs
return self._images[path]
kw["cat_id"]= self.idcategory(path)
kw["per_page"] = 200
kw["page"] = 0
imgs = {}
loop = True
while loop:
req = self.pwg.categories.getImages(**kw)
for img in req["images"]:
imgs[img["file"]] = img
if req["paging"]["count"] < req["paging"]["per_page"]:
loop = False
return imgs
@_checkarg
def sublevels(self, path, **kw):
@ -159,13 +145,11 @@ class Piwigo(piwigo.Piwigo):
@_checkarg
def idimage(self, path):
if not self.isimage(path):
raise PiwigoExistException("image %s not exist" % path)
img = path.split(' / ')[-1]
path = ' / '.join(path.split(' / ')[:-1])
try:
return self.images(path)[img]["id"]
except:
raise PiwigoExistException("image %s not exist" % path)
return self.images(path)[img]["id"]
@_checkarg
@_checklogin
@ -178,9 +162,7 @@ class Piwigo(piwigo.Piwigo):
if parent and not self.iscategory(parent):
raise PiwigoExistException("category %s not exist" % parent)
if parent : kw['parent'] = self.plan[parent]
id = self.pwg.categories.add(**kw)['id']
if not FORCE_PLAN:
self._plan[path] = id
self.pwg.categories.add(**kw)
return self.idcategory(path)
@_checkarg
@ -204,8 +186,6 @@ class Piwigo(piwigo.Piwigo):
remove (delete) category
"""
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
@_checkarg
@ -217,12 +197,9 @@ class Piwigo(piwigo.Piwigo):
kw["image"] = image
if len(path):
if not self.iscategory(path):
raise PiwigoExistException("category %s not exist" % path)
raise PiwigoExistException("category %s not exist" % parent)
kw['category'] = self.idcategory(path)
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
return self.pwg.images.addSimple(**kw)['image_id']
@_checkarg
@_checklogin
@ -250,6 +227,4 @@ class Piwigo(piwigo.Piwigo):
if not self.isimage(path):
raise PiwigoException("image %s not exist" % path)
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

View File

@ -70,12 +70,11 @@ class Run:
' ',
progressbar.Timer()],
maxval=self._qin.qsize()).start()
if self._qin.qsize():
for thread in self._threads:
thread.start()
while not self._qout.full():
time.sleep(0.1) # sleep 0.1s
pbar.update(self._qout.qsize())
for thread in self._threads:
thread.start()
while not self._qout.full():
time.sleep(0.1) # sleep 0.1s
pbar.update(self._qout.qsize())
pbar.finish()
return self._qerr

View File

@ -3,7 +3,6 @@
import sys
import os, os.path
import glob
import fnmatch
import pprint
try:
@ -20,8 +19,7 @@ USAGE = """piwigo verb --param1=value1 --param2=value2
verb list
- upload
- download
- sync-up
- sync-down
- sync
- ws
to get help: piwigo verb --help
@ -37,7 +35,7 @@ VERBS = {
"description" : "upload file in piwigo gallery",
"arg" :
{
"category" : {"type":"string", "default":"", "help":"destination category of piwigo gallery"},
"category" : {"type":"string", "default":"/", "help":"destination category of piwigo gallery"},
"source" : {"type":"string", "default":"*.jpg", "help":"path of upload picture"},
"url" : {"type":"string", "default":"", "help":"url of piwigo gallery"},
"user" : {"type":"string", "default":"", "help":"user of piwigo gallery"},
@ -51,7 +49,7 @@ VERBS = {
"description" : "download image from piwigo gallery",
"arg" :
{
"category" : {"type":"string", "default":"", "help":"source category of piwigo gallery"},
"category" : {"type":"string", "default":"/", "help":"source category of piwigo gallery"},
"dest" : {"type":"string", "default":".", "help":"path of destination"},
"url" : {"type":"string", "default":"", "help":"url of piwigo gallery"},
"user" : {"type":"string", "default":"", "help":"user of piwigo gallery"},
@ -59,34 +57,18 @@ VERBS = {
"thread" : {"type":"int", "default":"1", "help":"number of thread"},
},
},
"sync-up":
"sync":
{
"usage" : "usage for verb sync-up",
"description" : "synchronization local path to piwigo gallery",
"usage" : "usage for verb sync",
"description" : "synchronization between path and piwigo gallery",
"arg" :
{
"category" : {"type":"string", "default":"", "help":"category of piwigo gallery"},
"category" : {"type":"string", "default":"/", "help":"category of piwigo gallery"},
"source" : {"type":"string", "default":".", "help":"path of picture"},
"url" : {"type":"string", "default":"", "help":"url of piwigo gallery"},
"user" : {"type":"string", "default":"", "help":"user of piwigo gallery"},
"password" : {"type":"string", "default":"", "help":"password of piwigo gallery"},
"thread" : {"type":"int", "default":"1", "help":"number of thread"},
"extension" : {"type":"string", "default":"*.JPG,*.jpg,*.PNG,*.png,*.JPEG,*.jpeg,*.GIF,*.gif", "help":"extension for upload"},
},
},
"sync-down":
{
"usage" : "usage for verb sync-down",
"description" : "synchronization piwigo gallery to local path",
"arg" :
{
"category" : {"type":"string", "default":"", "help":"category of piwigo gallery"},
"source" : {"type":"string", "default":".", "help":"path of picture"},
"url" : {"type":"string", "default":"", "help":"url of piwigo gallery"},
"user" : {"type":"string", "default":"", "help":"user of piwigo gallery"},
"password" : {"type":"string", "default":"", "help":"password of piwigo gallery"},
"thread" : {"type":"int", "default":"1", "help":"number of thread"},
"extension" : {"type":"string", "default":"*.JPG,*.jpg,*.PNG,*.png,*.JPEG,*.jpeg,*.GIF,*.gif", "help":"extension for upload"},
},
},
"ws":
@ -184,7 +166,7 @@ def main():
kw = purge_kw(options.__dict__,('user','password','url','dest','category','thread'))
for img in piwigo.images(options.category, **kw):
run.add(piwigo.download,
["%s / %s" % (options.category, str(img)), "%s%s%s" % (options.dest, os.path.sep, str(img))],
["%s / %s" % (options.category, str(img)), "%s/%s" % (options.dest, str(img))],
kw)
except Exception as e:
ana.stop()
@ -217,78 +199,6 @@ def main():
piwigo.logout()
if run.error:
parser.error(run.strerror)
if verb == "sync-up":
ana = Analyse('Analyze')
ana.start()
try:
piwigo = Piwigo(url=options.url)
piwigo.login(options.user, options.password)
# check
options.source = os.path.abspath(options.source)
if not os.path.isdir(options.source):
raise Exception("%s is not directory" % options.source)
piwigo.iscategory(options.category)
if len(options.category) and options.category[-1] != '/' and options.category[:-3] != ' / ':
options.category = options.category+ ' / '
# treatment
run = Run(verb, options.thread)
kw = purge_kw(options.__dict__,('user','password','url','source','category','thread'))
# local -> piwigo
for root, dirnames, filenames in os.walk(options.source):
filtering = fnmatch.filter(filenames, options.extension.split(',')[0])
for ext in options.extension.split(',')[1:]:
filtering = filtering + fnmatch.filter(filenames, ext)
for filename in filtering:
pathrel = os.path.abspath(os.path.join(root, filename))[len(options.source)+1:]
pathabs = os.path.abspath(os.path.join(root, filename))
category = options.category + ' / '.join(pathrel.split(os.sep)[:-1])
if not piwigo.isimage(category + ' / ' + filename):
run.add(piwigo.makedirs,[category,], kw)
run.add(piwigo.upload,[pathabs, category], kw)
ana.stop()
except Exception as e:
ana.stop()
raise e
run.start()
piwigo.logout()
if run.error:
parser.error(run.strerror)
if verb == "sync-down":
ana = Analyse('Analyze')
ana.start()
try:
piwigo = Piwigo(url=options.url)
piwigo.login(options.user, options.password)
# check
options.source = os.path.abspath(options.source)
if not os.path.isdir(options.source):
raise Exception("%s is not directory" % options.source)
piwigo.iscategory(options.category)
if len(options.category) and options.category[-1] != '/' and options.category[:-3] != ' / ':
options.category = options.category+ ' / '
# treatment
run = Run(verb, options.thread)
kw = purge_kw(options.__dict__,('user','password','url','source','category','thread'))
# piwigo -> local
for category, item in piwigo.plan.iteritems():
if options.category == category[0:len(options.category)]:
path = os.path.join(options.source, *category[len(options.category):].split(' / '))
if not os.path.exists(path):
os.makedirs(path)
for img in piwigo.images(category):
pathimg = os.path.join(path, img)
if not os.path.exists(pathimg):
run.add(piwigo.download,
["%s / %s" % (category, str(img)), pathimg],
kw)
ana.stop()
except Exception as e:
ana.stop()
raise e
run.start()
piwigo.logout()
if run.error:
parser.error(run.strerror)
except Exception as e:
parser.error(e)
sys.exit(1)