412 lines
9.5 KiB
Python
Executable File
412 lines
9.5 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#from tmdbv3api import TMDb
|
|
from __future__ import print_function
|
|
from iso639 import languages
|
|
import tmdbv3api
|
|
import sys
|
|
import os
|
|
import locale
|
|
from datetime import datetime
|
|
import sqlite3
|
|
import argparse
|
|
import re
|
|
import wget
|
|
|
|
class ForeColors:
|
|
RED = '\033[31m'
|
|
GREEN = '\033[32m'
|
|
YELLOW = '\033[33m'
|
|
BLUE = '\033[34m'
|
|
MAGENTA = '\033[35m'
|
|
CYAN = '\033[36m'
|
|
LIRED = '\033[91m'
|
|
LIGREEN = '\033[92m'
|
|
LIYELLOW = '\033[93m'
|
|
LIBLUE = '\033[94m'
|
|
LIMAGENTA = '\033[95m'
|
|
LICYAN = '\033[96m'
|
|
END = '\033[0m'
|
|
BOLD = '\033[1m'
|
|
UNDERLINE = '\033[4m'
|
|
|
|
class BackColors:
|
|
RED = '\033[41m'
|
|
GREEN = '\033[42m'
|
|
YELLOW = '\033[43m'
|
|
BLUE = '\033[44m'
|
|
MAGENTA = '\033[45m'
|
|
CYAN = '\033[46m'
|
|
LIRED = '\033[101m'
|
|
LIGREEN = '\033[102m'
|
|
LIYELLOW = '\033[103m'
|
|
LIBLUE = '\033[104m'
|
|
LIMAGENTA = '\033[105m'
|
|
LICYAN = '\033[106m'
|
|
END = '\033[0m'
|
|
BOLD = '\033[1m'
|
|
UNDERLINE = '\033[4m'
|
|
|
|
locale.setlocale(locale.LC_ALL, 'fr_FR.UTF-8')
|
|
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("-p", "--purge", action="store_true", help="delete non existing film and saga if empty")
|
|
parser.add_argument("-s", "--symbolic", action="store_true", help="use symbolic path")
|
|
parser.add_argument("-q", "--quiet", action="store_true", help="quite mode reduce verbosity")
|
|
parser.add_argument("-d", "--database", default='bdd.db', help="Database location")
|
|
parser.add_argument("path", nargs='?', default='list/', help="defined the directory or the file to add in database")
|
|
|
|
args = parser.parse_args()
|
|
|
|
path = args.path
|
|
|
|
if not args.symbolic:
|
|
path = os.path.realpath(path)
|
|
|
|
if args.quiet:
|
|
print("Quiet mode")
|
|
def printr(*args):
|
|
pass
|
|
else:
|
|
def printr(*args):
|
|
print(*args)
|
|
|
|
bdd = args.database
|
|
|
|
|
|
img_url = 'http://image.tmdb.org/t/p/'
|
|
|
|
img_dir = '/var/www/html/img/'
|
|
|
|
|
|
nbFiles = 0
|
|
nbExist = 0
|
|
nbAdd = 0
|
|
|
|
def query_yes_no(question, default="yes"):
|
|
"""Ask a yes/no question via raw_input() and return their answer.
|
|
|
|
"question" is a string that is presented to the user.
|
|
"default" is the presumed answer if the user just hits <Enter>.
|
|
It must be "yes" (the default), "no" or None (meaning
|
|
an answer is required of the user).
|
|
|
|
The "answer" return value is True for "yes" or False for "no".
|
|
"""
|
|
valid = {"yes": True, "y": True, "ye": True,
|
|
"no": False, "n": False}
|
|
if default is None:
|
|
prompt = " [y/n] "
|
|
elif default == "yes":
|
|
prompt = " [Y/n] "
|
|
elif default == "no":
|
|
prompt = " [y/N] "
|
|
else:
|
|
raise ValueError("invalid default answer: '%s'" % default)
|
|
|
|
while True:
|
|
sys.stdout.write(question + prompt)
|
|
choice = input().lower()
|
|
if default is not None and choice == '':
|
|
return valid[default]
|
|
elif choice in valid:
|
|
return valid[choice]
|
|
else:
|
|
sys.stdout.write("Please respond with 'yes' or 'no' "
|
|
"(or 'y' or 'n').\n")
|
|
|
|
def browse(path):
|
|
|
|
files = []
|
|
files_path = []
|
|
ext = [".mkv", ".avi", ".mp4"]
|
|
global nbFiles
|
|
|
|
if os.path.isdir(path):
|
|
|
|
# r=root, d=directories, f = files
|
|
for r, d, f in os.walk(path):
|
|
for file in f:
|
|
if os.path.splitext(file)[1].lower() in ext:
|
|
files_path.append(os.path.join(r, file))
|
|
file = os.path.splitext(file)[0]
|
|
files.append(file)
|
|
nbFiles+=1
|
|
elif os.path.isfile(path):
|
|
if os.path.splitext(path)[1].lower() in ext:
|
|
files_path.append(path)
|
|
file = os.path.basename(path)
|
|
file = os.path.splitext(file)[0]
|
|
files.append(file)
|
|
|
|
return files, files_path
|
|
|
|
def createTable():
|
|
|
|
conn = sqlite3.connect(bdd)
|
|
|
|
cursor = conn.cursor()
|
|
|
|
cursor.execute("""
|
|
CREATE TABLE IF NOT EXISTS sagas(
|
|
id INTEGER PRIMARY KEY UNIQUE NOT NULL,
|
|
name TEXT,
|
|
poster_path TEXT
|
|
)
|
|
""")
|
|
|
|
cursor.execute("""
|
|
CREATE TABLE IF NOT EXISTS movies(
|
|
id INTEGER PRIMARY KEY UNIQUE NOT NULL,
|
|
title TEXT,
|
|
release_date DATE,
|
|
overview TEXT,
|
|
original_language TEXT,
|
|
original_title TEXT,
|
|
file_name TEXT,
|
|
file_path TEXT,
|
|
poster_path TEXT,
|
|
sagaID INTEGER,
|
|
CONSTRAINT movies_sagas_FK FOREIGN KEY (sagaID) REFERENCES sagas(id)
|
|
)
|
|
""")
|
|
|
|
conn.commit()
|
|
|
|
conn.close()
|
|
|
|
|
|
def searchMovie(file_name):
|
|
|
|
# create an instance of the IMDb class
|
|
tmdb = tmdbv3api.TMDb()
|
|
m = tmdbv3api.Movie()
|
|
|
|
tmdb.language = 'fr'
|
|
tmdb.debug = False
|
|
tmdb.api_key = '64f8b9bb9ba42cfe74ee0e9da1fa1690'
|
|
|
|
year = None
|
|
|
|
if re.search(r'\([0-9]*\)', file_name):
|
|
|
|
name = (re.findall(r'(.*) \([0-9]*\)', file_name))[0]
|
|
year = (re.findall(r'\(([0-9]*)\)', file_name))[0]
|
|
|
|
else:
|
|
name = file_name
|
|
|
|
printr (name)
|
|
printr (year)
|
|
|
|
movies = m.search(name)
|
|
movie = None
|
|
|
|
if not movies:
|
|
return None
|
|
|
|
if year != None:
|
|
for n in movies:
|
|
if hasattr(n, 'release_date'):
|
|
if n.release_date.split('-')[0] == year:
|
|
movie = n
|
|
break
|
|
else:
|
|
movie = movies[0]
|
|
|
|
return movie
|
|
|
|
def add():
|
|
|
|
tmdb = tmdbv3api.TMDb()
|
|
m = tmdbv3api.Movie()
|
|
|
|
tmdb.language = 'fr'
|
|
tmdb.debug = False
|
|
tmdb.api_key = '64f8b9bb9ba42cfe74ee0e9da1fa1690'
|
|
|
|
global nbAdd
|
|
global nbExist
|
|
|
|
|
|
conn = sqlite3.connect(bdd)
|
|
|
|
cursor = conn.cursor()
|
|
|
|
files, files_path = browse(path)
|
|
|
|
|
|
for file_name, file_path in zip(files, files_path):
|
|
|
|
cursor.execute("""SELECT * FROM movies WHERE file_path=?""", (file_path,))
|
|
|
|
result = cursor.fetchone()
|
|
|
|
if result:
|
|
print(ForeColors.GREEN + "{} alredy exist for movie {} {}".format(file_path,ForeColors.END,result[1]))
|
|
|
|
else:
|
|
|
|
printr(file_name)
|
|
|
|
movie = searchMovie(file_name)
|
|
|
|
if not movie:
|
|
print(ForeColors.MAGENTA + '{} Aucun resultat'.format(file_path) + ForeColors.END)
|
|
continue
|
|
|
|
id = movie.id
|
|
title = movie.title
|
|
release_date = movie.release_date
|
|
overview = movie.overview
|
|
original_language = movie.original_language
|
|
original_title = movie.original_title
|
|
poster_path = movie.poster_path
|
|
|
|
printr("Movie : {}".format(title))
|
|
|
|
printr(release_date)
|
|
|
|
if not os.path.isdir(img_dir):
|
|
os.mkdir(img_dir)
|
|
|
|
if poster_path != None:
|
|
for size in ['original', 'w154']:
|
|
if not os.path.isdir(img_dir+size):
|
|
os.mkdir(img_dir+size)
|
|
url = img_url+size+poster_path
|
|
out = img_dir+size+poster_path
|
|
if not os.path.isfile(out):
|
|
wget.download(url, out=out, bar=None)
|
|
|
|
detail = m.details(movie.id)
|
|
|
|
if detail.belongs_to_collection != None:
|
|
sagaID = detail.belongs_to_collection['id']
|
|
sagaNAME = detail.belongs_to_collection['name']
|
|
sagaPATH = detail.belongs_to_collection['poster_path']
|
|
if sagaPATH != None:
|
|
for size in ['original', 'w154']:
|
|
if not os.path.isdir(img_dir+size):
|
|
os.mkdir(img_dir+size)
|
|
url = img_url+size+sagaPATH
|
|
out = img_dir+size+sagaPATH
|
|
if not os.path.isfile(out):
|
|
wget.download(url, out=out, bar=None)
|
|
else:
|
|
printr("No Saga")
|
|
sagaID = 0
|
|
|
|
|
|
cursor.execute("""SELECT * FROM movies WHERE id=?""", (id,))
|
|
|
|
result = cursor.fetchone()
|
|
|
|
if result:
|
|
print(ForeColors.LIRED + "{} find for Movie {} {} {} and his alredy exist at {}".format(file_path, ForeColors.END, title, ForeColors.LIRED, result[7]) + ForeColors.END)
|
|
nbExist+=1
|
|
else:
|
|
if detail.belongs_to_collection != None:
|
|
cursor.execute("""SELECT 1 FROM sagas WHERE id=?""", (sagaID,))
|
|
|
|
if cursor.fetchone():
|
|
printr("{} alredy exist".format(sagaNAME))
|
|
|
|
else:
|
|
cursor.execute("""INSERT INTO sagas VALUES(?, ?, ?)""", (sagaID, sagaNAME, sagaPATH))
|
|
printr("Saga added")
|
|
|
|
|
|
cursor.execute("""INSERT INTO movies VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)""", (id, title, release_date, overview, original_language, original_title, file_name, file_path, poster_path, sagaID))
|
|
print(ForeColors.LIBLUE + "{} find for Movie {} {}".format(file_path, ForeColors.END, title))
|
|
nbAdd+=1
|
|
|
|
|
|
conn.commit()
|
|
|
|
printr()
|
|
|
|
|
|
#cursor.execute("""SELECT id, title, release_date, overview, original_language, original_title, poster_path FROM movies""")
|
|
#for row in cursor:
|
|
# print('{0} :\n{1}\n{2}\n{3}\n{4}\n{5}\n{6}'.format(row[0], row[1], row[2], row[3], row[4], row[5], row[6]))
|
|
# print()
|
|
|
|
conn.close()
|
|
|
|
|
|
def purge():
|
|
|
|
delarray = []
|
|
|
|
compt = 0
|
|
|
|
conn = sqlite3.connect(bdd)
|
|
|
|
cursor = conn.cursor()
|
|
|
|
cursor.execute("SELECT id, title, file_path, poster_path, sagaID FROM movies")
|
|
|
|
result = cursor.fetchall()
|
|
|
|
for file in result:
|
|
if not os.path.isfile(file[2]):
|
|
delarray.append(file)
|
|
compt+=1
|
|
|
|
for title in delarray:
|
|
print(title[1])
|
|
|
|
if compt > 0:
|
|
if query_yes_no("Do you want to delete this {} entry".format(compt), "no"):
|
|
for title in delarray:
|
|
cursor.execute("""DELETE FROM movies WHERE id=?""", (title[0],))
|
|
|
|
if title[4] != 0:
|
|
|
|
cursor.execute("""SELECT name FROM sagas WHERE id=?""", (title[4],))
|
|
|
|
sagaNAME = cursor.fetchone()[0]
|
|
|
|
cursor.execute("""SELECT 1 FROM movies WHERE sagaID=?""", (title[4],))
|
|
|
|
if cursor.fetchone():
|
|
printr("Found Movie in {} not delete it".format(sagaNAME))
|
|
else:
|
|
cursor.execute("""DELETE FROM sagas WHERE id=?""", (title[4],))
|
|
printr("{} deleted".format(sagaNAME))
|
|
|
|
|
|
if query_yes_no("Do you want to delete their images", "yes"):
|
|
for title in delarray:
|
|
if title[3] != None:
|
|
if os.path.isfile(img_dir+title[3]):
|
|
print("Del image "+title[3])
|
|
os.remove(img_dir+title[3])
|
|
else:
|
|
print("Nothing to purge")
|
|
|
|
|
|
conn.commit()
|
|
|
|
conn.close()
|
|
|
|
|
|
|
|
|
|
|
|
createTable()
|
|
|
|
if args.purge == True:
|
|
print("Purge mode")
|
|
purge()
|
|
else:
|
|
print("Add mode")
|
|
add()
|
|
print("Nombre de fichiers : {}".format(nbFiles))
|
|
print("Nombre de fichiers déjà existant : {}".format(nbExist))
|
|
print("Nombre de fichiers ajouter : {}".format(nbAdd))
|
|
|
|
|