summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-ximdb-lookup/imdbinfo.py139
1 files changed, 78 insertions, 61 deletions
diff --git a/imdb-lookup/imdbinfo.py b/imdb-lookup/imdbinfo.py
index 54ab2b6..7718bd1 100755
--- a/imdb-lookup/imdbinfo.py
+++ b/imdb-lookup/imdbinfo.py
@@ -13,7 +13,9 @@ import shutil
import logging
import hashlib
from io import BytesIO
-from multiprocessing import Pool
+from multiprocessing.dummy import Pool
+from threading import Lock
+
try:
from PIL import Image, ImageFilter
import tmdbsimple as tmdb
@@ -24,6 +26,18 @@ except ImportError as e:
print("or `pip install --user (one of: Pillow tmdbsimple requests)`")
sys.exit(1)
+def synchronized(lock):
+ """ Synchronization decorator. """
+
+ def wrap(f):
+ def newFunction(*args, **kw):
+ lock.acquire()
+ try:
+ return f(*args, **kw)
+ finally:
+ lock.release()
+ return newFunction
+ return wrap
def read_key():
if "TMDB_KEY" in os.environ.keys():
@@ -285,61 +299,20 @@ def do_rating(args, imdb_ids):
for info in infos:
print("{rating:.02f} {imdb_id} {title:30s} avg=" +
"{vote_average:.1f} count={vote_count:.0f}".format(**info))
-def getInfo(a):
- def listMovieFiles(path):
- valid_extensions = [".mkv", ".avi", ".mov", ".mp4", ".mpg"]
- def listFiles():
- for root, dirs, files in os.walk(path):
- for curfile in files:
- if sum(map(curfile.endswith, valid_extensions)):
- yield os.path.join(root, curfile)
- files = list(listFiles())
- if not files:
- files = [path]
- return files
-
- def poster(imdb_id):
- data = db.poster_low(imdb_id)
- if data:
- if not os.path.exists(".index.html/poster"):
- os.makedirs(".index.html/poster")
-
- out = os.path.join(".index.html/poster",imdb_id+".jpg")
- open(out, "wb").write(data)
- return out
- else:
- return None
- path, imdb_id = a
- db = Protector(TMDBCache())
- imdb = db.imdb_movie(imdb_id)
- omdb = db.omdb_movie(imdb_id)
- tmdb = db.infos(imdb_id)
- if not imdb or not omdb or not tmdb:
- print("Error in {} {}".format(path, imdb_id))
- return None
- else:
- return {
- 'id': imdb_id,
- 'title': tmdb['title'],
- 'path': path,
- 'poster': poster(imdb_id),
- 'tagline': 'tagline' in imdb and imdb['tagline'] or None,
- 'plot': 'plot' in imdb and imdb['plot']['outline'] or None,
- 'website': 'homepage' in imdb and imdb['homepage'] or omdb['Website'] != 'N/A' and omdb['Website'] or None,
- 'release': 'release_date' in tmdb and tmdb['release_date'] or None,
- 'movieFiles': list(listMovieFiles(path)),
- 'imdbRating': imdb['rating'],
- 'imdbVotes': imdb['num_votes'],
- 'omdbTomatoConsensus': (omdb['tomatoConsensus'] != 'N/A') and omdb['tomatoConsensus'] or None,
- 'omdbTomato': (omdb['tomatoMeter'] != 'N/A') and float(omdb['tomatoMeter']) or None,
- 'omdbUserTomato': (omdb['tomatoUserMeter'] != 'N/A') and float(omdb['tomatoUserMeter']) or None,
- 'omdbTomatoRating': (omdb['tomatoRating'] != 'N/A') and float(omdb['tomatoRating']) or None,
- 'omdbTomatoUserRating': (omdb['tomatoUserRating'] != 'N/A') and float(omdb['tomatoUserRating']) or None,
- 'tmdbId': tmdb['id'],
- }
def do_index(args, imdb_ids):
"""creates a index website"""
+ try:
+ from jinja2 import Template
+ except ImportError:
+ print("Failed to import jinja2 library for html-templating")
+ sys.exit(1)
+
+ assert not os.path.exists("index.html") and not os.path.exists(".index.html"), \
+ "index.html or folder .index.html already exists"
+ outdir = ".index.html"
+ os.mkdir(outdir)
+ os.mkdir(os.path.join(outdir, "poster"))
def install(filename):
outpath = os.path.join(".index.html", os.path.dirname(filename))
@@ -358,18 +331,64 @@ def do_index(args, imdb_ids):
out = open(filename, "w")
out.write(callbackName)
out.write("(")
- pool = Pool(processes=6) # parallel fetchers
+ pool = Pool(processes=12) # parallel fetchers
infos = pool.map(getInfo, imdb_ids)
json.dump(list(filter(bool,infos)), out)
out.write(")")
out.close()
return filename
- try:
- from jinja2 import Template
- except ImportError:
- print("Failed to import jinja2 library for html-templating")
- sys.exit(1)
+ def getInfo(a):
+ def listMovieFiles(path):
+ valid_extensions = [".mkv", ".avi", ".mov", ".mp4", ".mpg"]
+ def listFiles():
+ for root, dirs, files in os.walk(path):
+ for curfile in files:
+ if sum(map(curfile.endswith, valid_extensions)):
+ yield os.path.join(root, curfile)
+ files = list(listFiles())
+ if not files:
+ files = [path]
+ return files
+
+ def poster(imdb_id):
+ data = db.poster_low(imdb_id)
+ if data:
+ out = os.path.join(".index.html/poster",imdb_id+".jpg")
+ open(out, "wb").write(data)
+ return out
+ else:
+ return None
+ path, imdb_id = a
+ db = Protector(TMDBCache())
+ imdb = db.imdb_movie(imdb_id)
+ omdb = db.omdb_movie(imdb_id)
+ tmdb = db.infos(imdb_id)
+ if not imdb or not omdb or not tmdb:
+ print("Error in {} {}".format(path, imdb_id))
+ return None
+ else:
+ return {
+ 'id': imdb_id,
+ 'title': tmdb['title'],
+ 'path': path,
+ 'poster': poster(imdb_id),
+ 'tagline': 'tagline' in imdb and imdb['tagline'] or None,
+ 'plot': 'plot' in imdb and imdb['plot']['outline'] or None,
+ 'website': 'homepage' in imdb and imdb['homepage'] or omdb['Website'] != 'N/A' and omdb['Website'] or None,
+ 'release': 'release_date' in tmdb and tmdb['release_date'] or None,
+ 'movieFiles': list(listMovieFiles(path)),
+ 'imdbRating': imdb['rating'],
+ 'imdbVotes': imdb['num_votes'],
+ 'omdbTomatoConsensus': (omdb['tomatoConsensus'] != 'N/A') and omdb['tomatoConsensus'] or None,
+ 'omdbTomato': (omdb['tomatoMeter'] != 'N/A') and float(omdb['tomatoMeter']) or None,
+ 'omdbUserTomato': (omdb['tomatoUserMeter'] != 'N/A') and float(omdb['tomatoUserMeter']) or None,
+ 'omdbTomatoRating': (omdb['tomatoRating'] != 'N/A') and float(omdb['tomatoRating']) or None,
+ 'omdbTomatoUserRating': (omdb['tomatoUserRating'] != 'N/A') and float(omdb['tomatoUserRating']) or None,
+ 'tmdbId': tmdb['id'],
+ }
+
+
template_file = os.path.join(os.path.dirname(__file__),
"index.jinja2.html")
template = Template(open(template_file, "r").read())
@@ -378,8 +397,6 @@ def do_index(args, imdb_ids):
'install': install,
'data': data,
}
- assert not os.path.exists("index.html") and not os.path.exists(".index.html"), \
- "index.html or folder .index.html already exists"
stream = template.generate(mapping)
outfile = open("index.html", "wb")
for output in stream: