New handling method
This commit is contained in:
parent
defcc3d618
commit
b305f63b46
7 changed files with 172 additions and 122 deletions
80
main.py
80
main.py
|
@ -1,3 +1,4 @@
|
|||
#coding:utf-8
|
||||
'''
|
||||
|
||||
Pierre Cadart
|
||||
|
@ -11,6 +12,8 @@ import posixpath
|
|||
import re
|
||||
import config
|
||||
import piexel
|
||||
import tokenizer
|
||||
import filerule
|
||||
from ftplib import FTP
|
||||
import time
|
||||
import file
|
||||
|
@ -39,36 +42,39 @@ def ftpwalk(directory, ftp):
|
|||
# mais retourne les résultats intermédiaires
|
||||
yield (current, Ldirs, Lfiles)
|
||||
|
||||
def visit_server(domain, conf, api):
|
||||
def visit_folder(domain, api, rules):
|
||||
# Connection au serveur
|
||||
print('connect to:', conf.get_domain(domain))
|
||||
ftp = FTP(conf.get_domain(domain), user='rez', passwd='rez')
|
||||
print('connect to:', domain['server'])
|
||||
ftp = FTP(domain['server'][6:], user=domain['username'], passwd=domain['password'])
|
||||
ftp.encoding = 'UTF-8'
|
||||
# Initialisation des listes de mises à jour
|
||||
L_missing = [] # fichiers non trouvés sur le serveur FTP
|
||||
L_unreferenced = [] # fichiers non référencés dans l'API
|
||||
L_moved = [] # fichiers déplacés sur le serveur FTP
|
||||
# Lecture à distance des deux BDD
|
||||
for directory in conf.get_movie_dir(domain):
|
||||
# Visite l'arborescence de chaque dossier
|
||||
Lloc = []
|
||||
for path, _, files in ftpwalk(directory, ftp):
|
||||
# Vérifie si le parcours du dossier est autorisé
|
||||
if not any(path.startswith(p) for p in conf.get_excluded_movie_dir(domain)):
|
||||
# Ajoute les fichiers correspondants aux extensions
|
||||
for f in files:
|
||||
if conf.is_valid_file(f):
|
||||
F = file.File(path, f)
|
||||
Lloc.append(F)
|
||||
# Récupère les fichiers de l'api
|
||||
Lapi = []
|
||||
for info in api.get_files(path='ftp://'+conf.get_domain(domain)+directory, like=1):
|
||||
Lapi.append(file.File(info['path'][len('ftp://'+conf.get_domain(domain)):], info['name'], api_id=info['filable_id']))
|
||||
# Lecture des fichiers sur le serveur FTP
|
||||
Lloc = []
|
||||
for path, _, files in ftpwalk(domain['path'], ftp):
|
||||
# Ajoute les fichiers correspondants aux extensions
|
||||
for f in files:
|
||||
match = filerule.match_rules(path+'/'+f, rules)
|
||||
if match:
|
||||
print('got match:',match[1], 'name:',path+'/'+f)
|
||||
F = file.File(path+'/'+f, match[1])
|
||||
Lloc.append(F)
|
||||
ftp.close()
|
||||
|
||||
print('total:',len(Lloc))
|
||||
exit(0)
|
||||
# Application des règles de chemins
|
||||
|
||||
# Récupère les fichiers de l'api
|
||||
Lapi = []
|
||||
for info in api.get_files(path='ftp://'+domain['server']+domain['path'], like=1):
|
||||
Lapi.append(file.File(info['path'][len('ftp://'+domain['server']):], info['name'], api_id=info['filable_id']))
|
||||
|
||||
# supprime les dossiers de l'api
|
||||
Lapi = [f for f in Lapi if conf.is_valid_file(f.name)]
|
||||
|
||||
# TODO: gérer ces noms
|
||||
# supprime les noms avec un '+'
|
||||
Lloc = [f for f in Lloc if '+' not in f.name]
|
||||
|
||||
|
@ -123,11 +129,11 @@ def visit_server(domain, conf, api):
|
|||
for f, _ in Llink2:
|
||||
Lunref.remove(f)
|
||||
|
||||
print('missing:',Lmissing)
|
||||
print('missing:',[str(e.api_id)+':'+repr(e) for e in Lmissing])
|
||||
print('\n'*3)
|
||||
print('unreferenced:','\n'.join(str(f) for f in Lunref))
|
||||
print('\n'*3)
|
||||
print('unreferenced titles:', '\n'.join([f.title for f in Lunref]))
|
||||
print('unreferenced titles:', '\n'.join(sorted([f.title for f in Lunref])))
|
||||
|
||||
# Put les renommages / déplacements
|
||||
i = 0
|
||||
|
@ -175,28 +181,33 @@ def visit_server(domain, conf, api):
|
|||
raise Exception('end')
|
||||
|
||||
# Poste tout les films locaux
|
||||
"""
|
||||
i = 0
|
||||
for film in Lunref:
|
||||
i += 1
|
||||
print('['+str(i)+'/'+str(len(Lunref))+']'+'post:', film.title)
|
||||
print('['+str(i)+'/'+str(len(Lunref))+']'+'post:', film.title, str(film.year))
|
||||
try:
|
||||
api.debug_print = True
|
||||
posted = False
|
||||
if film.year is not None:
|
||||
resp = api.post_film(title=film.title, year=film.year)
|
||||
else:
|
||||
if "id" in resp:
|
||||
resp = api.post_file(path='ftp://'+conf.get_domain(domain)+film.path, name=film.name, type='Film', type_id=resp["id"], **film.additional_info())
|
||||
posted = True
|
||||
if not posted:
|
||||
resp = api.post_film(title=film.title)
|
||||
if "id" in resp:
|
||||
api.post_file(path='ftp://'+conf.get_domain(domain)+film.path, name=film.name, type='Film', type_id=resp["id"], **film.additional_info())
|
||||
api.debug_print = False
|
||||
if "id" in resp:
|
||||
resp = api.post_file(path='ftp://'+conf.get_domain(domain)+film.path, name=film.name, type='Film', type_id=resp["id"], **film.additional_info())
|
||||
|
||||
print('response:', resp)
|
||||
time.sleep(1)
|
||||
except Exception as e:
|
||||
print(e)
|
||||
print('film '+film.title+' not posted')
|
||||
raise Exception('end')
|
||||
|
||||
"""
|
||||
'''
|
||||
# Marque comme broken les films référencés non présents
|
||||
# TODO: màj des broken_links
|
||||
'''
|
||||
i = 0
|
||||
for film in Lmissing:
|
||||
i += 1
|
||||
|
@ -208,8 +219,13 @@ def visit_server(domain, conf, api):
|
|||
def main():
|
||||
conf = config.Config()
|
||||
api = piexel.Piexel(conf.server, conf.app, conf.token)
|
||||
for dom in conf.domains:
|
||||
visit_server(dom, conf, api)
|
||||
tokens = tokenizer.Tokenizer(conf, api)
|
||||
folders = api.get_folders()
|
||||
rules = api.get_paths()
|
||||
|
||||
for fold in folders:
|
||||
applicable = [filerule.FileRule(re.escape(fold['path'])+'\\/'+r['regex'], conf) for r in rules if int(r['indexer_folder_id']) == fold['id']]
|
||||
visit_folder(fold, api, applicable)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue