2019-07-06 23:00:00 -07:00
|
|
|
from comicapi import comicarchive
|
|
|
|
from blinker import Namespace
|
2019-07-23 15:49:57 -07:00
|
|
|
|
2019-07-11 17:35:30 -07:00
|
|
|
from io import BytesIO
|
2019-07-15 22:51:10 -07:00
|
|
|
from wand.image import Image
|
2019-07-23 15:49:57 -07:00
|
|
|
import os, sys, re
|
2019-07-06 23:00:00 -07:00
|
|
|
|
|
|
|
from scripts import database
|
|
|
|
|
|
|
|
rpi_signals = Namespace()
|
|
|
|
comic_loaded = rpi_signals.signal("comic-loaded")
|
2019-07-23 15:49:57 -07:00
|
|
|
movie_loaded = rpi_signals.signal("movie-loaded")
|
2019-07-06 23:00:00 -07:00
|
|
|
|
2019-07-15 22:51:10 -07:00
|
|
|
publishers_to_ignore = ["***REMOVED***"]
|
|
|
|
|
2019-07-06 23:00:00 -07:00
|
|
|
# Directories
|
|
|
|
|
2019-07-15 22:51:10 -07:00
|
|
|
RPI_COMICS_DIRECTORY = "/usb/storage/media/Comics/"
|
|
|
|
RPI_MOVIES_DIRECTORY = "/usb/storage/media/Videos/Movies/"
|
|
|
|
RPI_TV_SHOWS_DIRECTORY = "/usb/storage/media/Videos/TV/"
|
|
|
|
RPI_VIDEOS_DIRECTORY = "/usb/storage/media/Videos/Videos/"
|
|
|
|
RPI_GAMES_DIRECTORY = "/usb/storage/media/games/"
|
|
|
|
RPI_MUSIC_DIRECTORY = "/usb/storage/media/Music/"
|
|
|
|
|
|
|
|
MC_COMICS_DIRECTORY = "C:\\Users\\Matthew\\Documents\\Comics"
|
|
|
|
|
|
|
|
COMICS_DIRECTORY = RPI_COMICS_DIRECTORY if os.path.exists(RPI_COMICS_DIRECTORY) else MC_COMICS_DIRECTORY
|
2019-07-23 15:49:57 -07:00
|
|
|
MOVIES_DIRECTORY = RPI_MOVIES_DIRECTORY
|
2019-07-06 23:00:00 -07:00
|
|
|
|
|
|
|
#############
|
|
|
|
|
|
|
|
|
|
|
|
def get_comics():
|
2019-07-11 17:35:30 -07:00
|
|
|
total_comics = 0
|
|
|
|
comics_in_db = 0
|
|
|
|
comics_added = 0
|
2019-07-06 23:00:00 -07:00
|
|
|
meta = []
|
2019-07-11 17:35:30 -07:00
|
|
|
thumbnails = []
|
2019-07-06 23:00:00 -07:00
|
|
|
i = 0
|
|
|
|
for root, dirs, files in os.walk(COMICS_DIRECTORY):
|
|
|
|
for f in files:
|
2019-07-25 00:02:54 -07:00
|
|
|
if "temp" in root:
|
|
|
|
continue
|
2019-07-06 23:00:00 -07:00
|
|
|
if f.endswith(".cbr"):
|
2019-07-11 17:35:30 -07:00
|
|
|
total_comics += 1
|
2019-07-06 23:00:00 -07:00
|
|
|
path = os.path.join(root, f)
|
|
|
|
if not database.comic_path_in_db(path):
|
2019-07-11 17:35:30 -07:00
|
|
|
try:
|
|
|
|
test_path = path.encode("utf8")
|
|
|
|
except Exception as e:
|
|
|
|
print("encoding failed on:", path)
|
|
|
|
continue
|
|
|
|
archive = open_comic(path)
|
2019-07-15 22:51:10 -07:00
|
|
|
md = archive.readCIX()
|
|
|
|
if md.publisher in publishers_to_ignore:
|
|
|
|
continue
|
|
|
|
print(path)
|
|
|
|
meta.append((path, md))
|
2019-07-11 17:35:30 -07:00
|
|
|
thumbnails.append(get_comic_thumbnails(archive))
|
|
|
|
comics_added += 1
|
2019-07-06 23:00:00 -07:00
|
|
|
i += 1
|
2019-07-23 15:49:57 -07:00
|
|
|
if i >= 2:
|
2019-07-11 17:35:30 -07:00
|
|
|
comic_loaded.send("anonymous", meta=meta.copy(), thumbnails=thumbnails.copy())
|
2019-07-06 23:00:00 -07:00
|
|
|
meta.clear()
|
2019-07-11 17:35:30 -07:00
|
|
|
thumbnails.clear()
|
2019-07-06 23:00:00 -07:00
|
|
|
i = 0
|
2019-07-23 15:49:57 -07:00
|
|
|
comics_in_db += 1
|
2019-07-11 17:35:30 -07:00
|
|
|
print("total number of comics:", total_comics)
|
|
|
|
print("comics in database:", comics_in_db)
|
|
|
|
print("number of comics added:", comics_added)
|
|
|
|
comic_loaded.send("anonymous", meta=meta, thumbnails=thumbnails)
|
|
|
|
|
|
|
|
|
2019-07-23 15:49:57 -07:00
|
|
|
def get_comic(path):
|
|
|
|
meta = []
|
|
|
|
thumbnails = []
|
|
|
|
if not database.comic_path_in_db(path):
|
|
|
|
try:
|
|
|
|
test_path = path.encode("utf8")
|
|
|
|
except Exception as e:
|
|
|
|
print("encoding failed on:", path)
|
|
|
|
return
|
|
|
|
archive = open_comic(path)
|
|
|
|
md = archive.readCIX()
|
|
|
|
if md.publisher in publishers_to_ignore:
|
|
|
|
return
|
|
|
|
print(path)
|
|
|
|
meta.append((path, md))
|
|
|
|
thumbnails.append(get_comic_thumbnails(archive))
|
|
|
|
comic_loaded.send("anonymous", meta=meta, thumbnails=thumbnails)
|
|
|
|
|
|
|
|
|
2019-07-11 17:35:30 -07:00
|
|
|
def get_comic_thumbnails(comic):
|
|
|
|
thumbnails = []
|
2019-07-15 22:51:10 -07:00
|
|
|
size = "256x256"
|
|
|
|
new_height = 256
|
|
|
|
new_width = 256
|
2019-07-11 17:35:30 -07:00
|
|
|
for page in range(comic.getNumberOfPages()):
|
|
|
|
image_bytes = BytesIO(comic.getPage(page))
|
2019-07-15 22:51:10 -07:00
|
|
|
image = Image(file=image_bytes)
|
|
|
|
orig_height = image.height
|
|
|
|
orig_width = image.width
|
2019-07-25 00:02:54 -07:00
|
|
|
if orig_height >= orig_width:
|
2019-07-15 22:51:10 -07:00
|
|
|
width = int((orig_width/orig_height) * new_height)
|
|
|
|
height = new_height
|
2019-07-23 15:49:57 -07:00
|
|
|
else:
|
|
|
|
height = int((orig_height/orig_width) * new_width)
|
|
|
|
width = new_width
|
2019-07-15 22:51:10 -07:00
|
|
|
image.thumbnail(width, height)
|
|
|
|
thumbnails.append((image.make_blob(), "image/"+image.format))
|
2019-07-11 17:35:30 -07:00
|
|
|
return thumbnails
|
|
|
|
|
|
|
|
|
|
|
|
def open_comic(path):
|
2019-07-15 22:51:10 -07:00
|
|
|
archive = comicarchive.ComicArchive(path, default_image_path="static/images/icon.png")
|
2019-07-11 17:35:30 -07:00
|
|
|
return archive
|
2019-07-06 23:00:00 -07:00
|
|
|
|
|
|
|
|
2019-07-23 15:49:57 -07:00
|
|
|
def get_movies():
|
|
|
|
print("start load movies")
|
2019-07-25 00:02:54 -07:00
|
|
|
pattern = r"(.+)( \(....\))(\(extended\))?( Director's Cut)?(\.mkv)"
|
2019-07-23 15:49:57 -07:00
|
|
|
movies = []
|
|
|
|
total_movies = 0
|
|
|
|
movies_in_db = 0
|
|
|
|
movies_added = 0
|
|
|
|
for root, dirs, files in os.walk(MOVIES_DIRECTORY):
|
|
|
|
for f in files:
|
|
|
|
if f.endswith(".mkv"):
|
2019-07-25 00:02:54 -07:00
|
|
|
total_movies += 1
|
2019-07-23 15:49:57 -07:00
|
|
|
path = os.path.join(root, f)
|
|
|
|
if not database.movie_path_in_db(path):
|
|
|
|
try:
|
|
|
|
match = re.fullmatch(pattern, f)
|
|
|
|
if not match:
|
2019-07-25 00:02:54 -07:00
|
|
|
print(f, "did not match regex.")
|
2019-07-23 15:49:57 -07:00
|
|
|
continue
|
|
|
|
print("movie path:", path)
|
|
|
|
title = f[:-4].replace(match.group(2), "")
|
|
|
|
print("movie title:", title)
|
|
|
|
year = int(match.group(2)[2:-1])
|
|
|
|
extended = 0
|
|
|
|
directors_cut = 0
|
|
|
|
if match.group(3):
|
|
|
|
extended = 1
|
|
|
|
imdb_data = database.imdb_get_movie(title.replace(match.group(3), ""), year)
|
|
|
|
elif match.group(4):
|
|
|
|
imdb_data = database.imdb_get_movie(title.replace(match.group(4), ""), year)
|
|
|
|
directors_cut = 1
|
|
|
|
else:
|
|
|
|
imdb_data = database.imdb_get_movie(title, year)
|
|
|
|
if not imdb_data:
|
|
|
|
print("could not get imdb data")
|
|
|
|
continue
|
|
|
|
imdb_id = imdb_data["tconst"]
|
|
|
|
length = imdb_data["runtimeMinutes"]
|
|
|
|
|
|
|
|
tmdb_data = database.tmdb_get_movie_by_imdb_id(imdb_id)
|
|
|
|
if not tmdb_data:
|
|
|
|
print("could not get tmdb data")
|
|
|
|
continue
|
|
|
|
tmdb_id = tmdb_data[0]
|
|
|
|
description = tmdb_data[1]
|
|
|
|
poster_path = tmdb_data[2]
|
|
|
|
backdrop_path = tmdb_data[3]
|
2019-07-25 00:02:54 -07:00
|
|
|
movies_added += 1
|
2019-07-23 15:49:57 -07:00
|
|
|
|
|
|
|
movies.append((path, imdb_id, tmdb_id, title, year, length, description, extended, directors_cut, poster_path, backdrop_path))
|
|
|
|
if len(movies) >= 20:
|
|
|
|
movie_loaded.send("anonymous", movies=movies.copy())
|
|
|
|
movies.clear()
|
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
2019-07-25 00:02:54 -07:00
|
|
|
movies_in_db += 1
|
2019-07-23 15:49:57 -07:00
|
|
|
movie_loaded.send("anonymous", movies=movies)
|
|
|
|
print("finish load movies")
|
|
|
|
print("total movies:", total_movies)
|
|
|
|
print("movies in database:", movies_in_db)
|
|
|
|
print("movies added:", movies_added)
|