From c4e6824a8aa7c04bb41d76741f66b223401ece69 Mon Sep 17 00:00:00 2001 From: Matthew Welch Date: Sat, 10 Jul 2021 13:17:28 -0700 Subject: [PATCH] Move some files out of scripts folder Change error logging to log messages as error rather than info Add 404 error when video or comic not found Change paths to use pathlib --- comics/comics.py | 20 ++- database.py | 46 +++--- scripts/func.py => func.py | 143 ++++++++---------- games/games.py | 16 +- rpiWebApp.py | 50 +++--- scripts/fix_thumbnail_size.py | 16 +- templates/base.html | 8 +- scripts/tmdb.py => tmdb.py | 6 +- .../templates/tv_movies/movieViewer.html | 21 ++- tv_movies/tv_movies.py | 20 ++- 10 files changed, 179 insertions(+), 167 deletions(-) rename scripts/func.py => func.py (82%) rename scripts/tmdb.py => tmdb.py (92%) diff --git a/comics/comics.py b/comics/comics.py index 507e1ba..17483c2 100644 --- a/comics/comics.py +++ b/comics/comics.py @@ -8,7 +8,8 @@ import pytz from flask import Blueprint, current_app, make_response, render_template, request from flask_login import login_required -from scripts import database, func +import func +import database Comics = Blueprint("comics", __name__, template_folder="templates") @@ -35,7 +36,7 @@ def index(): item_count=len(publishers), ) except Exception as e: - current_app.logger.info(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) + current_app.logger.error(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) return str(type(e)) + " " + str(e) @@ -92,7 +93,7 @@ def search(): comics_end=comics_end, ) except Exception as e: - current_app.logger.info(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) + current_app.logger.error(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) return str(type(e)) + " " + str(e) @@ -149,7 +150,7 @@ def comics_publisher(publisher): item_count=len(publisher_series), ) except Exception as e: - current_app.logger.info(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) + current_app.logger.error(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) return str(type(e)) + " " + str(e) @@ -176,7 +177,7 @@ def comic_gallery(comic_id): item_count=meta.pagecount, ) except Exception as e: - current_app.logger.info(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) + current_app.logger.error(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) return str(type(e)) + " " + str(e) @@ -184,6 +185,8 @@ def comic_gallery(comic_id): @login_required def get_comic_page(comic_id, page_number): meta = database.db_get_comic_by_id(comic_id) + if not os.path.exists(meta.path): + return "File not found", 404 comic = func.open_comic(meta.path) byte_image = comic.getPage(page_number) type = filetype.guess(byte_image).mime @@ -205,8 +208,9 @@ def get_comic_thumbnail(comic_id, page_number): thumb = database.db_get_thumbnail_by_id_page(comic_id, page_number) response = make_response(thumb.image) response.headers["cache-control"] = "public" - date = pytz.utc.localize(datetime.datetime.utcfromtimestamp(os.path.getmtime(meta.path))) - response.headers["last-modified"] = date.strftime("%a, %d %b %Y %H:%M:%S %Z") + if os.path.exists(meta.path): + date = pytz.utc.localize(datetime.datetime.utcfromtimestamp(os.path.getmtime(meta.path))) + response.headers["last-modified"] = date.strftime("%a, %d %b %Y %H:%M:%S %Z") response.headers["content-type"] = thumb.type - response.headers["Content-Disposition"] = 'attachment; filename="{} {}_{}_thumbnail"'.format(str(meta.series), meta.issuetext, str(page_number)) + response.headers["Content-Disposition"] = 'filename="{} {}_{}_thumbnail"'.format(str(meta.series), meta.issuetext, str(page_number)) return response diff --git a/database.py b/database.py index 8646276..f570342 100644 --- a/database.py +++ b/database.py @@ -76,7 +76,7 @@ class Comic(Base): setattr(self, column.name, data[i]) i += 1 except Exception as e: - current_app.logger.info(inspect.stack()[0][3] + "Comic" + " " + str(type(e)) + " " + str(e)) + current_app.logger.error(inspect.stack()[0][3] + "Comic" + " " + str(type(e)) + " " + str(e)) def __repr__(self): return "".format(series=self.series, issue=self.issuetext) @@ -100,7 +100,7 @@ class ComicThumbnail(Base): setattr(self, column.name, data[i]) i += 1 except Exception as e: - current_app.logger.info(inspect.stack()[0][3] + "ComicThumbnail" + " " + str(type(e)) + " " + str(e)) + current_app.logger.error(inspect.stack()[0][3] + "ComicThumbnail" + " " + str(type(e)) + " " + str(e)) class Movie(Base): @@ -124,7 +124,7 @@ class Movie(Base): setattr(self, column.name, data[i]) i += 1 except Exception as e: - current_app.logger.info(inspect.stack()[0][3] + "Movie" + " " + str(type(e)) + " " + str(e)) + current_app.logger.error(inspect.stack()[0][3] + "Movie" + " " + str(type(e)) + " " + str(e)) class TvShow(Base): @@ -144,7 +144,7 @@ class TvShow(Base): setattr(self, column.name, data[i]) i += 1 except Exception as e: - current_app.logger.info(inspect.stack()[0][3] + "TvShow" + " " + str(type(e)) + " " + str(e)) + current_app.logger.error(inspect.stack()[0][3] + "TvShow" + " " + str(type(e)) + " " + str(e)) class TvEpisode(Base): @@ -166,7 +166,7 @@ class TvEpisode(Base): setattr(self, column.name, data[i]) i += 1 except Exception as e: - current_app.logger.info(inspect.stack()[0][3] + "TvEpisode" + " " + str(type(e)) + " " + str(e)) + current_app.logger.error(inspect.stack()[0][3] + "TvEpisode" + " " + str(type(e)) + " " + str(e)) class DupComic(Base): @@ -219,7 +219,7 @@ class DupComic(Base): setattr(self, column.name, data[i]) i += 1 except Exception as e: - current_app.logger.info(inspect.stack()[0][3] + "DupComic" + " " + str(type(e)) + " " + str(e)) + current_app.logger.error(inspect.stack()[0][3] + "DupComic" + " " + str(type(e)) + " " + str(e)) class Game(Base): @@ -242,7 +242,7 @@ class Game(Base): setattr(self, column.name, data[i]) i += 1 except Exception as e: - current_app.logger.info(inspect.stack()[0][3] + "Game" + " " + str(type(e)) + " " + str(e)) + current_app.logger.error(inspect.stack()[0][3] + "Game" + " " + str(type(e)) + " " + str(e)) class User(Base, UserMixin): @@ -262,7 +262,7 @@ class User(Base, UserMixin): i += 1 pass except Exception as e: - current_app.logger.info(inspect.stack()[0][3] + "User" + " " + str(type(e)) + " " + str(e)) + current_app.logger.error(inspect.stack()[0][3] + "User" + " " + str(type(e)) + " " + str(e)) def get_id(self): return self.email @@ -297,7 +297,7 @@ class UserTvMovieData(Base): setattr(self, column.name, data[i]) i += 1 except Exception as e: - current_app.logger.info(inspect.stack()[0][3] + "UserTvMovieData" + " " + str(type(e)) + " " + str(e)) + current_app.logger.error(inspect.stack()[0][3] + "UserTvMovieData" + " " + str(type(e)) + " " + str(e)) class TvMovieKeywords(Base): @@ -319,7 +319,7 @@ class TvMovieKeywords(Base): setattr(self, column.name, data[i]) i += 1 except Exception as e: - current_app.logger.info(inspect.stack()[0][3] + "TvMovieKeywords" + " " + str(type(e)) + " " + str(e)) + current_app.logger.error(inspect.stack()[0][3] + "TvMovieKeywords" + " " + str(type(e)) + " " + str(e)) Base.metadata.create_all(engine) @@ -340,7 +340,7 @@ Base.metadata.create_all(engine) setattr(self, column.name, data[i]) i += 1 except Exception as e: - current_app.logger.info(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e))""" + current_app.logger.error(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e))""" # def get_db(): @@ -415,7 +415,7 @@ def add_tv_shows(tv_show_data): session.add(tv_show) session.commit() except Exception as e: - current_app.logger.info(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) + current_app.logger.error(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) def add_tv_episodes(episodes): @@ -429,7 +429,7 @@ def add_tv_episodes(episodes): else: current_app.logger.info(f"TV episode: The TMDB id {episode.tmdb_id} for {episode.path} is already in the database.") except Exception as e: - current_app.logger.info(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) + current_app.logger.error(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) def add_comics(meta, thumbnails): @@ -587,8 +587,8 @@ def comic_path_in_db(path): if result or result2: return True except Exception as e: - current_app.logger.info(path) - current_app.logger.info(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) + current_app.logger.error(path) + current_app.logger.error(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) return False @@ -599,8 +599,8 @@ def movie_path_in_db(path): if result: return True except Exception as e: - current_app.logger.info(path) - current_app.logger.info(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) + current_app.logger.error(path) + current_app.logger.error(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) return False @@ -611,8 +611,8 @@ def tv_show_path_in_db(path): if result: return True except Exception as e: - current_app.logger.info(path) - current_app.logger.info(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) + current_app.logger.error(path) + current_app.logger.error(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) return False @@ -623,8 +623,8 @@ def tv_episode_path_in_db(path): if result: return True except Exception as e: - current_app.logger.info(path) - current_app.logger.info(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) + current_app.logger.error(path) + current_app.logger.error(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) return False @@ -635,7 +635,7 @@ def game_in_db(game_id): if result: return True except Exception as e: - current_app.logger.info(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) + current_app.logger.error(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) return False @@ -978,4 +978,4 @@ def get_user(email): return result return None except Exception as e: - current_app.logger.info(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) + current_app.logger.error(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) diff --git a/scripts/func.py b/func.py similarity index 82% rename from scripts/func.py rename to func.py index 3d45983..305824b 100644 --- a/scripts/func.py +++ b/func.py @@ -4,7 +4,6 @@ import os import pathlib import re from datetime import timedelta -from io import BytesIO import enzyme import requests @@ -13,7 +12,7 @@ from comicapi import comicarchive from flask import current_app from wand.image import Image -from scripts import database +import database rpi_signals = Namespace() comic_loaded = rpi_signals.signal("comic-loaded") @@ -28,22 +27,10 @@ API_KEY = "***REMOVED***" # Directories -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/Games/" -RPI_MUSIC_DIRECTORY = "/usb/storage/media/Music/" - -MC_COMICS_DIRECTORY = "/mnt/c/Users/Matthew/Documents/Comics/" -MC_MOVIES_DIRECTORY = "/mnt/c/Users/Matthew/Documents/Movies/" -MC_TV_SHOWS_DIRECTORY = "/mnt/c/Users/Matthew/Documents/TV/" -MC_GAMES_DIRECTORY = "/mnt/g/Humble Bundle/rpi/" - -COMICS_DIRECTORY = RPI_COMICS_DIRECTORY if os.path.exists(RPI_COMICS_DIRECTORY) else MC_COMICS_DIRECTORY -MOVIES_DIRECTORY = RPI_MOVIES_DIRECTORY if os.path.exists(RPI_MOVIES_DIRECTORY) else MC_MOVIES_DIRECTORY -TV_SHOWS_DIRECTORY = RPI_TV_SHOWS_DIRECTORY if os.path.exists(RPI_TV_SHOWS_DIRECTORY) else MC_TV_SHOWS_DIRECTORY -GAMES_DIRECTORY = RPI_GAMES_DIRECTORY if os.path.exists(RPI_GAMES_DIRECTORY) else MC_GAMES_DIRECTORY +COMICS_DIRECTORY = pathlib.Path("/srv/comics/") +MOVIES_DIRECTORY = pathlib.Path("/srv/movies/") +TV_SHOWS_DIRECTORY = pathlib.Path("/srv/tv/") +GAMES_DIRECTORY = pathlib.Path("/srv/games/") ############# @@ -59,27 +46,27 @@ def get_comics(): for f in files: if "temp" in root: continue - if f.endswith(".cbr"): + if f.endswith((".cbr", ".cbz")): total_comics += 1 - path = os.path.join(root, f) - if not database.comic_path_in_db(path): + path = pathlib.Path(root, f) + if not database.comic_path_in_db(str(path)): try: test_path = path.encode("utf8") except Exception as e: - current_app.logger.info("encoding failed on: " + path) + current_app.logger.error("encoding failed on: " + str(path)) continue archive = open_comic(path) md = archive.readCIX() if md.publisher in publishers_to_ignore: continue - current_app.logger.info(path) + current_app.logger.info(str(path)) try: - meta.append((path, md)) + meta.append((str(path), md)) thumbnails.append(get_comic_thumbnails(archive)) comics_added += 1 i += 1 except Exception as e: - current_app.logger.info(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) + current_app.logger.error(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) continue if i >= 2: comic_loaded.send("anonymous", meta=meta.copy(), thumbnails=thumbnails.copy()) @@ -101,9 +88,9 @@ def get_comic(path: pathlib.Path): try: test_path = str(path).encode("utf8") except Exception as e: - current_app.logger.info(f"encoding failed on: {path}") + current_app.logger.error(f"encoding failed on: {path}") return - archive = open_comic(str(path)) + archive = open_comic(path) md = archive.readCIX() if md.publisher in publishers_to_ignore: return @@ -112,7 +99,7 @@ def get_comic(path: pathlib.Path): try: thumbnails.append(get_comic_thumbnails(archive)) except Exception as e: - current_app.logger.info(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) + current_app.logger.error(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) return comic_loaded.send("anonymous", meta=meta, thumbnails=thumbnails) @@ -123,23 +110,22 @@ def get_comic_thumbnails(comic): new_height = 256 new_width = 256 for page in range(comic.getNumberOfPages()): - image_bytes = BytesIO(comic.getPage(page)) - image = Image(file=image_bytes) - orig_height = image.height - orig_width = image.width - if orig_height >= orig_width: - width = int((orig_width / orig_height) * new_height) - height = new_height - else: - height = int((orig_height / orig_width) * new_width) - width = new_width - image.thumbnail(width, height) - thumbnails.append((image.make_blob(), "image/" + image.format)) + with Image(blob=comic.getPage(page)) as image: + orig_height = image.height + orig_width = image.width + if orig_height >= orig_width: + width = int((orig_width / orig_height) * new_height) + height = new_height + else: + height = int((orig_height / orig_width) * new_width) + width = new_width + image.thumbnail(width, height) + thumbnails.append((image.make_blob(), "image/" + image.format)) return thumbnails def open_comic(path): - archive = comicarchive.ComicArchive(path, default_image_path="static/images/icon.png") + archive = comicarchive.ComicArchive(str(path), default_image_path="static/images/icon.png") return archive @@ -155,19 +141,21 @@ def get_movies(): for f in files: if f.endswith(".mkv"): total_movies += 1 - path = os.path.join(root, f) - if not database.movie_path_in_db(path): + path = pathlib.Path(root, f) + if not database.movie_path_in_db(str(path)): try: match = re.match(pattern, f) if not match: current_app.logger.info(f + " did not match regex.") continue - current_app.logger.info("movie path: " + path) + current_app.logger.info("movie path: " + str(path)) title = match.group("title") current_app.logger.info("movie title: " + title) year = int(match.group("year")) extended = True if match.group("extended") else False directors_cut = True if match.group("directors_cut") else False + res_4k_path = (path.parent / path.name.replace(f"({year})", f"({year})(4k)")) + res_4k = res_4k_path.exists() data = { "api_key": API_KEY, @@ -195,13 +183,12 @@ def get_movies(): backdrop_path = info["backdrop_path"] movies_added += 1 - movies.append((path, tmdb_id, title, year, description, extended, directors_cut, poster_path, backdrop_path,)) + movies.append((str(path), tmdb_id, title, year, description, extended, directors_cut, poster_path, backdrop_path, res_4k,)) if len(movies) >= 20: movie_loaded.send("anonymous", movies=movies.copy()) movies.clear() except Exception as e: - current_app.logger.info(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) - # print(e) + current_app.logger.error(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) movies_in_db += 1 movie_loaded.send("anonymous", movies=movies) current_app.logger.info("finish loading movies") @@ -226,6 +213,7 @@ def get_movie(path: pathlib.Path): year = int(match.group("year")) extended = match.group("extended") is True directors_cut = match.group("directors_cut") is True + res_4k = (path.parent / path.name.replace(f"({year})", f"({year})(4k)")).exists() data = { "api_key": API_KEY, @@ -252,12 +240,12 @@ def get_movie(path: pathlib.Path): poster_path = info["poster_path"] backdrop_path = info["backdrop_path"] - movies.append((str(path), tmdb_id, title, year, description, extended, directors_cut, poster_path, backdrop_path,)) + movies.append((str(path), tmdb_id, title, year, description, extended, directors_cut, poster_path, backdrop_path, res_4k,)) movie_loaded.send("anonymous", movies=movies.copy()) movies.clear() current_app.logger.info("finish loading movie") except Exception as e: - current_app.logger.info(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) + current_app.logger.error(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) def get_tv_shows(): @@ -265,14 +253,14 @@ def get_tv_shows(): search_url = "https://api.themoviedb.org/3/search/tv" tv_url = "https://api.themoviedb.org/3/tv/" current_app.logger.info("start loading tv shows") - for dir in sorted(os.listdir(TV_SHOWS_DIRECTORY)): - dir_match = re.match(dir_pattern, dir) + for dir in sorted(TV_SHOWS_DIRECTORY.iterdir()): + dir_match = re.match(dir_pattern, str(dir)) if dir_match: - path = TV_SHOWS_DIRECTORY + dir - if not database.tv_show_path_in_db(path): + path = TV_SHOWS_DIRECTORY / dir + if not database.tv_show_path_in_db(str(path)): json_info = {} - if os.path.exists(path + "/info.json"): - with open(path + "/info.json") as f: + if (path / "info.json").exists(): + with (path / "info.json").open() as f: json_info = json.load(f) series_name = dir_match.group("title") series_year = int(dir_match.group("year")) @@ -306,7 +294,7 @@ def get_tv_shows(): series_year, description, poster_path, - path, + str(path), ) tv_show_loaded.send("anonymous", tv_show=tv_show_data) current_app.logger.info("finished loading tv shows.") @@ -319,11 +307,12 @@ def get_tv_episodes(): for tv_show in rows: try: episodes = [] - for video in sorted(os.listdir(tv_show.path)): - video_match = re.match(video_pattern, video) + tv_show_path = pathlib.Path(tv_show.path) + for video in sorted(tv_show_path.iterdir()): + video_match = re.match(video_pattern, str(video)) if video_match: - path = os.path.join(tv_show.path, video) - if not database.tv_episode_path_in_db(path): + path = tv_show_path / video + if not database.tv_episode_path_in_db(str(path)): season = int(video_match.group("season")) episode = int(video_match.group("episode")) episode_name = video_match.group("title") @@ -341,14 +330,14 @@ def get_tv_episodes(): episode_description = info["overview"] episode_still_path = info["still_path"] episodes.append( - (episode_tmdb_id, tv_show.tmdb_id, episode_name, season, episode, episode_description, episode_still_path, path,) + (episode_tmdb_id, tv_show.tmdb_id, episode_name, season, episode, episode_description, episode_still_path, str(path),) ) if len(episodes) >= 10: tv_episodes_loaded.send("anonymous", tv_episodes=episodes.copy()) episodes.clear() tv_episodes_loaded.send("anonymous", tv_episodes=episodes) except Exception as e: - current_app.logger.info(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) + current_app.logger.error(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) current_app.logger.info("finished loading tv episodes") @@ -388,7 +377,7 @@ def get_chapters(path): with open(path, "rb") as f: mkv = enzyme.MKV(f) except Exception as e: - current_app.logger.info(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) + current_app.logger.error(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) return {} mkv_info = {} for chapter in mkv.chapters: @@ -414,7 +403,7 @@ def get_tags(path): with open(path, "rb") as f: mkv = enzyme.MKV(f) except Exception as e: - current_app.logger.info(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) + current_app.logger.error(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) mkv_info = {} for tag in mkv.tags: if tag.targets.data[0].data == 70: @@ -463,12 +452,12 @@ def get_games(): } i = 0 current_app.logger.info("start loading games") - for folder in sorted(os.listdir(GAMES_DIRECTORY), key=str.casefold): - root = os.path.join(GAMES_DIRECTORY, folder) - if os.path.isdir(os.path.join(root)): + for folder in sorted(GAMES_DIRECTORY.iterdir()): + root = folder.absolute() + if root.is_dir(): try: - path = os.path.join(root, "info.json") - with open(path, "r") as f: + path = root / "info.json" + with path.open() as f: info = json.load(f) game_id = info["id"] if not database.game_in_db(game_id): @@ -498,11 +487,11 @@ def get_games(): game_id, description, poster_path, - root, + str(root), windows, mac, linux, - folder, + folder.name, ) games.append(game) i += 1 @@ -511,7 +500,7 @@ def get_games(): games.clear() i = 0 except Exception as e: - current_app.logger.info(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) + current_app.logger.error(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) games_loaded.send("anonymous", games=games) current_app.logger.info("finished loading games") @@ -562,7 +551,7 @@ def get_game(path: pathlib.Path): game_id, description, poster_path, - str(dir), + str(dir.relative_to(GAMES_DIRECTORY)), windows, mac, linux, @@ -572,13 +561,13 @@ def get_game(path: pathlib.Path): games_loaded.send("anonymous", games=games) current_app.logger.info("finished loading game") except Exception as e: - current_app.logger.info(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) + current_app.logger.error(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) def update_games(): current_app.logger.info("start updating game data") - for folder in sorted(os.listdir(GAMES_DIRECTORY), key=str.casefold): - root = pathlib.Path(GAMES_DIRECTORY, folder, "info.json") + for folder in sorted(GAMES_DIRECTORY.iterdir()): + root = folder / "info.json" update_game(root) current_app.logger.info("finished updating game data") @@ -600,4 +589,4 @@ def update_game(path: pathlib.Path): linux = True database.update_game((game_id, windows, mac, linux)) except Exception as e: - current_app.logger.info(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) + current_app.logger.error(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) diff --git a/games/games.py b/games/games.py index c5cf8c0..306c0c4 100644 --- a/games/games.py +++ b/games/games.py @@ -5,7 +5,7 @@ import os from flask import Blueprint, abort, current_app, jsonify, render_template, request, send_file from flask_login import login_required -from scripts import database, func +import database Games = Blueprint("games", __name__, template_folder="templates") @@ -21,7 +21,7 @@ def index(): end = len(games) if len(games) < max_items * page else max_items * page return render_template("games/index.html", title="Games", games=games) except Exception as e: - current_app.logger.info(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) + current_app.logger.error(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) return str(type(e)) + " " + str(e) @@ -44,7 +44,7 @@ def get_games(): } return jsonify(games_json) except Exception as e: - current_app.logger.info(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) + current_app.logger.error(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) return str(type(e)) + " " + str(e) @@ -67,7 +67,7 @@ def get_games_windows(): } return jsonify(games_json) except Exception as e: - current_app.logger.info(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) + current_app.logger.error(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) return str(type(e)) + " " + str(e) @@ -92,7 +92,7 @@ def get_games_mac(): games_json[game.game_id]["mac"] = {"executable": info["mac"]["executable"]} return jsonify(games_json) except Exception as e: - current_app.logger.info(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) + current_app.logger.error(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) return str(type(e)) + " " + str(e) @@ -115,7 +115,7 @@ def get_games_linux(): } return jsonify(games_json) except Exception as e: - current_app.logger.info(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) + current_app.logger.error(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) return str(type(e)) + " " + str(e) @@ -149,7 +149,7 @@ def get_game(game_id): return jsonify(game_json) abort(404) except Exception as e: - current_app.logger.info(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) + current_app.logger.error(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) return str(type(e)) + " " + str(e) @@ -165,5 +165,5 @@ def download_game_hash(game_id, file_hash): else: abort(404) except Exception as e: - current_app.logger.info(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) + current_app.logger.error(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) return str(type(e)) + " " + str(e) diff --git a/rpiWebApp.py b/rpiWebApp.py index 1e71cb3..a3aed32 100644 --- a/rpiWebApp.py +++ b/rpiWebApp.py @@ -11,15 +11,15 @@ from urllib.parse import urljoin, urlsplit, urlunsplit import inotify.adapters import inotify.constants import requests -from flask import Flask, Response, current_app, flash, g, redirect, render_template, request, url_for +from flask import Flask, Response, current_app, flash, g, redirect, render_template, request, url_for, make_response from flask_login import LoginManager, current_user, login_required, login_user, logout_user from oauthlib.oauth2 import WebApplicationClient -import scripts.func as func +import func from admin import admin from comics import comics from games import games -from scripts import database +import database from tv_movies import tv_movies @@ -163,35 +163,35 @@ def update_comic_db(sender, **kw): try: database.add_comics(kw["meta"], kw["thumbnails"]) except Exception as e: - current_app.logger.info(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) + current_app.logger.error(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) def update_movie_db(sender, **kw): try: database.add_movies(kw["movies"]) except Exception as e: - current_app.logger.info(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) + current_app.logger.error(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) def update_tv_show_db(sender, **kw): try: database.add_tv_shows(kw["tv_show"]) except Exception as e: - current_app.logger.info(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) + current_app.logger.error(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) def update_tv_episodes_db(sender, **kw): try: database.add_tv_episodes(kw["tv_episodes"]) except Exception as e: - current_app.logger.info(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) + current_app.logger.error(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) def update_games_db(sender, **kw): try: database.add_games(kw["games"]) except Exception as e: - current_app.logger.info(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) + current_app.logger.error(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) func.comic_loaded.connect(update_comic_db) @@ -206,28 +206,30 @@ def load_user(email): try: return database.get_user(email) except Exception as e: - current_app.logger.info(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) + current_app.logger.error(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) return str(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) @login_manager.request_loader def load_user_from_request(request): try: - api_key = request.headers.get("Authorization") - if api_key: - api_key = api_key.replace("Basic ", "", 1) + basic_auth = request.headers.get("Authorization") + api_key = request.args.get("api_key") + auth_key = basic_auth if basic_auth else api_key + if auth_key: + auth_key = auth_key.replace("Basic ", "", 1) try: - api_key = base64.b64decode(api_key).decode("utf-8") + auth_key = base64.b64decode(auth_key).decode("utf-8") except TypeError: pass - email = api_key.split(":")[0] - password = api_key.split(":")[1] + email = auth_key.split(":")[0] + password = auth_key.split(":")[1] user = load_user(email) if user and user.check_password(password): return user return None except Exception as e: - current_app.logger.info(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) + current_app.logger.error(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) return str(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) @@ -264,7 +266,7 @@ def login(): return redirect(next_page) return render_template("login.html", title="login", auth_url=request_uri) except Exception as e: - current_app.logger.info(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) + current_app.logger.error(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) return str(e) @@ -313,7 +315,7 @@ def callback(): return redirect(request.args.get("state")) except Exception as e: - current_app.logger.info(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) + current_app.logger.error(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) return str(e) @@ -323,7 +325,7 @@ def logout(): logout_user() return redirect(url_for("login")) except Exception as e: - current_app.logger.info(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) + current_app.logger.error(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) return str(e) @@ -338,7 +340,7 @@ def home(): try: return render_template("home.html", title="Home", current_user=current_user) except Exception as e: - current_app.logger.info(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) + current_app.logger.error(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) return str(e) @@ -352,7 +354,7 @@ def apply_headers(response: Response): response.headers.set("email", user_name) return response except Exception as e: - current_app.logger.info(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) + current_app.logger.error(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) return str(e) @@ -367,14 +369,16 @@ def resource_not_found(e): try: return render_template("404.html"), 404 except Exception as e: - current_app.logger.info(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) + current_app.logger.error(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) return str(e) @login_manager.unauthorized_handler def handle_unauthorized(): temp = login() - return temp, 401 + t = make_response(temp) + t.headers['WWW-Authenticate'] = 'Basic realm="Access to the staging site"' + return t, 401 @app.errorhandler(Exception) diff --git a/scripts/fix_thumbnail_size.py b/scripts/fix_thumbnail_size.py index e70cc81..a85462d 100644 --- a/scripts/fix_thumbnail_size.py +++ b/scripts/fix_thumbnail_size.py @@ -37,14 +37,14 @@ def fix_thumbnails(): print("got list of all thumbnails\n") for row in rows: - image = Image(file=BytesIO(row["image"])) - if image.width > new_width or image.height > new_height: - print("id:", row["id"], "pageNumber:", row["pageNumber"]) - db.execute( - "UPDATE comic_thumbnails SET image=? WHERE id=? AND pageNumber=?", - [resize_image(image, new_width, new_height).make_blob(), row["id"], row["pageNumber"]], - ) - db.commit() + with Image(blob=row["image"]) as image: + if image.width > new_width or image.height > new_height: + print("id:", row["id"], "pageNumber:", row["pageNumber"]) + db.execute( + "UPDATE comic_thumbnails SET image=? WHERE id=? AND pageNumber=?", + [resize_image(image, new_width, new_height).make_blob(), row["id"], row["pageNumber"]], + ) + db.commit() fix_thumbnails() diff --git a/templates/base.html b/templates/base.html index f8a9c69..b86e1ce 100644 --- a/templates/base.html +++ b/templates/base.html @@ -5,10 +5,12 @@ rpi - {{ title }} - + + + {% block head %} {% endblock %}