mirror of
https://github.com/fraoustin/piwigotools.git
synced 2025-06-07 16:06:27 +00:00
Merge branch 'develop'
valid version 0.1.0
This commit is contained in:
commit
d47f8e2006
@ -1,3 +1,8 @@
|
|||||||
|
0.1.0
|
||||||
|
=====
|
||||||
|
|
||||||
|
- add verb sync-up and sync-down
|
||||||
|
|
||||||
0.0.2
|
0.0.2
|
||||||
=====
|
=====
|
||||||
|
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
Module piwigotools
|
Module piwigotools
|
||||||
"""
|
"""
|
||||||
|
|
||||||
__version_info__ = (0, 0, 2)
|
__version_info__ = (0, 1, 0)
|
||||||
__version__ = '.'.join([str(val) for val in __version_info__])
|
__version__ = '.'.join([str(val) for val in __version_info__])
|
||||||
|
|
||||||
import inspect
|
import inspect
|
||||||
@ -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,8 +65,11 @@ class Piwigo(piwigo.Piwigo):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def plan(self):
|
def plan(self):
|
||||||
return { 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 = { i["name"] : i["id"] for i in self.pwg.categories.getList(recursive=True, fullname=True)['categories'] }
|
||||||
|
plan[""] = 0
|
||||||
|
self._plan = plan
|
||||||
|
return self._plan
|
||||||
|
|
||||||
def _checkarg(fn):
|
def _checkarg(fn):
|
||||||
def checking(self, *args, **kw):
|
def checking(self, *args, **kw):
|
||||||
@ -113,7 +121,11 @@ class Piwigo(piwigo.Piwigo):
|
|||||||
"""
|
"""
|
||||||
return list of file name image for path
|
return list of file name image for path
|
||||||
"""
|
"""
|
||||||
|
if FORCE_IMAGES or path not in self._images:
|
||||||
|
try:
|
||||||
kw["cat_id"]= self.idcategory(path)
|
kw["cat_id"]= self.idcategory(path)
|
||||||
|
except PiwigoExistException:
|
||||||
|
return {}
|
||||||
kw["per_page"] = 200
|
kw["per_page"] = 200
|
||||||
kw["page"] = 0
|
kw["page"] = 0
|
||||||
imgs = {}
|
imgs = {}
|
||||||
@ -124,7 +136,9 @@ class Piwigo(piwigo.Piwigo):
|
|||||||
imgs[img["file"]] = img
|
imgs[img["file"]] = img
|
||||||
if req["paging"]["count"] < req["paging"]["per_page"]:
|
if req["paging"]["count"] < req["paging"]["per_page"]:
|
||||||
loop = False
|
loop = False
|
||||||
return imgs
|
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):
|
||||||
@ -145,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])
|
||||||
|
try:
|
||||||
return self.images(path)[img]["id"]
|
return self.images(path)[img]["id"]
|
||||||
|
except:
|
||||||
|
raise PiwigoExistException("image %s not exist" % path)
|
||||||
|
|
||||||
|
|
||||||
@_checkarg
|
@_checkarg
|
||||||
@_checklogin
|
@_checklogin
|
||||||
@ -162,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
|
||||||
@ -186,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
|
||||||
@ -197,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
|
||||||
@ -227,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
|
||||||
|
@ -70,6 +70,7 @@ class Run:
|
|||||||
' ',
|
' ',
|
||||||
progressbar.Timer()],
|
progressbar.Timer()],
|
||||||
maxval=self._qin.qsize()).start()
|
maxval=self._qin.qsize()).start()
|
||||||
|
if self._qin.qsize():
|
||||||
for thread in self._threads:
|
for thread in self._threads:
|
||||||
thread.start()
|
thread.start()
|
||||||
while not self._qout.full():
|
while not self._qout.full():
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
import sys
|
import sys
|
||||||
import os, os.path
|
import os, os.path
|
||||||
import glob
|
import glob
|
||||||
|
import fnmatch
|
||||||
import pprint
|
import pprint
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@ -35,7 +36,7 @@ VERBS = {
|
|||||||
"description" : "upload file in piwigo gallery",
|
"description" : "upload file in piwigo gallery",
|
||||||
"arg" :
|
"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"},
|
"source" : {"type":"string", "default":"*.jpg", "help":"path of upload picture"},
|
||||||
"url" : {"type":"string", "default":"", "help":"url of piwigo gallery"},
|
"url" : {"type":"string", "default":"", "help":"url of piwigo gallery"},
|
||||||
"user" : {"type":"string", "default":"", "help":"user of piwigo gallery"},
|
"user" : {"type":"string", "default":"", "help":"user of piwigo gallery"},
|
||||||
@ -49,7 +50,7 @@ VERBS = {
|
|||||||
"description" : "download image from piwigo gallery",
|
"description" : "download image from piwigo gallery",
|
||||||
"arg" :
|
"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"},
|
"dest" : {"type":"string", "default":".", "help":"path of destination"},
|
||||||
"url" : {"type":"string", "default":"", "help":"url of piwigo gallery"},
|
"url" : {"type":"string", "default":"", "help":"url of piwigo gallery"},
|
||||||
"user" : {"type":"string", "default":"", "help":"user of piwigo gallery"},
|
"user" : {"type":"string", "default":"", "help":"user of piwigo gallery"},
|
||||||
@ -57,18 +58,34 @@ VERBS = {
|
|||||||
"thread" : {"type":"int", "default":"1", "help":"number of thread"},
|
"thread" : {"type":"int", "default":"1", "help":"number of thread"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
"sync":
|
"sync-up":
|
||||||
{
|
{
|
||||||
"usage" : "usage for verb sync",
|
"usage" : "usage for verb sync-up",
|
||||||
"description" : "synchronization between path and piwigo gallery",
|
"description" : "synchronization local path to piwigo gallery",
|
||||||
"arg" :
|
"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"},
|
"source" : {"type":"string", "default":".", "help":"path of picture"},
|
||||||
"url" : {"type":"string", "default":"", "help":"url of piwigo gallery"},
|
"url" : {"type":"string", "default":"", "help":"url of piwigo gallery"},
|
||||||
"user" : {"type":"string", "default":"", "help":"user of piwigo gallery"},
|
"user" : {"type":"string", "default":"", "help":"user of piwigo gallery"},
|
||||||
"password" : {"type":"string", "default":"", "help":"password of piwigo gallery"},
|
"password" : {"type":"string", "default":"", "help":"password of piwigo gallery"},
|
||||||
"thread" : {"type":"int", "default":"1", "help":"number of thread"},
|
"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":
|
"ws":
|
||||||
@ -166,7 +183,7 @@ def main():
|
|||||||
kw = purge_kw(options.__dict__,('user','password','url','dest','category','thread'))
|
kw = purge_kw(options.__dict__,('user','password','url','dest','category','thread'))
|
||||||
for img in piwigo.images(options.category, **kw):
|
for img in piwigo.images(options.category, **kw):
|
||||||
run.add(piwigo.download,
|
run.add(piwigo.download,
|
||||||
["%s / %s" % (options.category, str(img)), "%s/%s" % (options.dest, str(img))],
|
["%s / %s" % (options.category, str(img)), "%s%s%s" % (options.dest, os.path.sep, str(img))],
|
||||||
kw)
|
kw)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
ana.stop()
|
ana.stop()
|
||||||
@ -199,6 +216,78 @@ def main():
|
|||||||
piwigo.logout()
|
piwigo.logout()
|
||||||
if run.error:
|
if run.error:
|
||||||
parser.error(run.strerror)
|
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:
|
except Exception as e:
|
||||||
parser.error(e)
|
parser.error(e)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user