217 lines
6.2 KiB
Python
217 lines
6.2 KiB
Python
#!/usr/bin/env python3.7
|
|
# -*- coding: utf-8 -*-
|
|
|
|
import os
|
|
import exifread
|
|
import datetime
|
|
import sys
|
|
from piwigo import *
|
|
from calendar import monthrange
|
|
import tinytag
|
|
import subprocess
|
|
sys.stdout.reconfigure(encoding='utf-8')
|
|
sys.stdin.reconfigure(encoding='utf-8')
|
|
exif_choice = "EXIF DateTimeOriginal"
|
|
|
|
mySite = Piwigo('https://pho.flywithu.com/')
|
|
mySite.pwg.session.login(username="flywithu", password="1q2w3e4r5t")
|
|
|
|
#aaa=mySite.pwg.categories.add(name="zaza")
|
|
#print(aaa['id'])
|
|
#exit()
|
|
|
|
|
|
|
|
categories=mySite.pwg.categories.getList(recursive="true")['categories']
|
|
categoryidnames = []
|
|
for category in categories:
|
|
# print (category['id_uppercat'])
|
|
categoryidname={'id':category['id'] , 'name':category['name'], 'id_uppercat':category['id_uppercat']}
|
|
categoryidnames.append(categoryidname)
|
|
|
|
#print(categoryidnames)
|
|
|
|
def getcategoryfromid(id):
|
|
|
|
for cat in categoryidnames:
|
|
# print(cat['id'])
|
|
if int(cat['id']) ==int(id):
|
|
return cat['name']
|
|
return id
|
|
|
|
#print("NAME::::::::"+str(getcategoryfromid(2930)))
|
|
|
|
#:wqexit()
|
|
#print(categoryidnames)
|
|
findcategorys = []
|
|
for category in categories:
|
|
if category['id_uppercat'] is None:
|
|
findcategory={'id':category['id'], 'name':category['name']}
|
|
findcategorys.append(findcategory)
|
|
#print (findcategorys)
|
|
elif (len(category['uppercats'].split(','))) is 2:
|
|
id_upper=category['id_uppercat']
|
|
findcategory={'id':category['id'], 'name':getcategoryfromid(id_upper)+"/"+category['name']}
|
|
findcategorys.append(findcategory)
|
|
elif (len(category['uppercats'].split(','))) is 3:
|
|
id_upper=category['id_uppercat']
|
|
id_upper2=""
|
|
for category2 in categories:
|
|
# print(str(category2['id'])+":"+id_upper)
|
|
if str(category2['id']) == id_upper:
|
|
id_upper2=category2['id_uppercat']
|
|
break
|
|
findcategory={'id':category['id'], 'name':getcategoryfromid(id_upper2)+"/"+getcategoryfromid(id_upper)+"/"+category['name']}
|
|
findcategorys.append(findcategory)
|
|
# print (category['name'])
|
|
|
|
|
|
def getidbyname(year):
|
|
global findcategorys
|
|
name=year
|
|
for findcat in findcategorys:
|
|
# print(findcat)
|
|
if findcat['name'] == name:
|
|
return findcat['id']
|
|
|
|
newid=mySite.pwg.categories.add(name=year,status="private")
|
|
findcategory={'id':newid['id'], 'name':name}
|
|
findcategorys.append(findcategory)
|
|
return newid['id']
|
|
|
|
def getidbyname2(year,month):
|
|
global findcategorys
|
|
name=year+"/"+month
|
|
for findcat in findcategorys:
|
|
# print(findcat)
|
|
if findcat['name'] == name:
|
|
return findcat['id']
|
|
|
|
yearid=getidbyname(year)
|
|
# print("YEARID:::"+str(yearid))
|
|
newid=mySite.pwg.categories.add(name=month,parent=int(yearid),status="private")
|
|
findcategory={'id':newid['id'], 'name':name}
|
|
findcategorys.append(findcategory)
|
|
return newid['id']
|
|
|
|
def getidbyname3(year,month,day):
|
|
global findcategorys
|
|
name=year+"/"+month+"/"+day
|
|
for findcat in findcategorys:
|
|
# print(findcat)
|
|
if findcat['name'] == name:
|
|
return findcat['id']
|
|
|
|
monthid=getidbyname2(year,month)
|
|
# print("Monthid:::"+str(monthid))
|
|
newid=mySite.pwg.categories.add(name=day,parent=int(monthid),status="private")
|
|
findcategory={'id':newid['id'], 'name':name}
|
|
findcategorys.append(findcategory)
|
|
return newid['id']
|
|
|
|
|
|
#print(str(getidbyname3("2017","05","1_59")))
|
|
#print(str(getidbyname3("2017","05","1_59")))
|
|
#print(str(getidbyname3("2017","05","1_59")))
|
|
|
|
|
|
|
|
#exit()
|
|
|
|
|
|
def files(path):
|
|
for file in os.listdir(path):
|
|
if os.path.isfile(os.path.join(path, file)):
|
|
yield file
|
|
|
|
|
|
targetfolder = "./data/"
|
|
|
|
def get_video_date(fileloc):
|
|
print(fileloc)
|
|
command="/usr/local/bin/ffprobe -v quiet -print_format flat -show_format "+fileloc+" | grep creation_time | cut -d= -f2-"
|
|
output=subprocess.run(command, shell=True, stdout = subprocess.PIPE,universal_newlines=True)
|
|
return (output.stdout)
|
|
|
|
def photoupload(targetfolder):
|
|
filenames = os.listdir(targetfolder)
|
|
testfiles = [ x for x in filenames if os.path.isfile(os.path.join(targetfolder,x)) == True ]
|
|
|
|
testdirs = [ x for x in filenames if os.path.isdir(os.path.join(targetfolder,x)) == True ]
|
|
for testdir in testdirs:
|
|
photoupload(os.path.join(targetfolder,testdir))
|
|
|
|
for filename in testfiles:
|
|
# newfilename = filename.encode('utf-8')
|
|
try:
|
|
print("targetfile:",filename)
|
|
targetname,targetext = os.path.splitext(filename)
|
|
pathfile = os.path.join(targetfolder,filename)
|
|
f = open(pathfile,'rb')
|
|
if targetext == ".mp4":
|
|
print (".mp4")
|
|
resultvideo = get_video_date(pathfile)
|
|
print (resultvideo)
|
|
T=datetime.datetime.strptime(str(resultvideo)[1:11],'%Y-%m-%d')
|
|
print (T)
|
|
else:
|
|
print("targetext:::",targetext)
|
|
tags = exifread.process_file(f, stop_tag=exif_choice)
|
|
#print("tags::",tags)
|
|
for tag in tags.keys():
|
|
if tag in (exif_choice):
|
|
photodatetime=tags[tag]
|
|
T=datetime.datetime.strptime(str(photodatetime)[0:10],'%Y:%m:%d')
|
|
|
|
day=T.strftime('%d')
|
|
if(int(day)<=7):
|
|
fromto=T.strftime('%Y%m01_%Y%m07')
|
|
elif (int(day)<=14):
|
|
fromto=T.strftime('%Y%m08_%Y%m14')
|
|
elif (int(day)<=21):
|
|
fromto=T.strftime('%Y%m15_%Y%m21')
|
|
else:
|
|
year=int(T.strftime('%Y'))
|
|
month=int(T.strftime('%m'))
|
|
lastday=monthrange(year,month)[1]
|
|
fromto=T.strftime('%Y%m22_%Y%m'+str(lastday))
|
|
print(T.strftime('%Y'),T.strftime('%m'),fromto)
|
|
parentid=getidbyname3(T.strftime('%Y'),T.strftime('%m'),fromto)
|
|
|
|
print("Try:",pathfile)
|
|
mySite.pwg.images.addSimple(image=pathfile, category=parentid)
|
|
print("Success:",pathfile)
|
|
# os.rename(pathfile,f2)
|
|
os.remove(pathfile)
|
|
#delete
|
|
|
|
except Exception as e:
|
|
print(e)
|
|
print(pathfile)
|
|
os.rename(pathfile,os.path.join("done",pathfile))
|
|
continue
|
|
#print (T)
|
|
#print ("Key: %s, value %s" % (tag, tags[tag]))
|
|
|
|
photoupload(targetfolder)
|
|
|
|
|
|
|
|
|
|
|
|
#print (testfiles)
|
|
|
|
#exit()
|
|
#def print_all_metadata_atoms(file):
|
|
# atoms = file.findall('.//data')
|
|
# for atom in atoms:
|
|
# data = atom.get_attribute('data')
|
|
# print (atom.parent.name, data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#for filename in filenames:
|