from flask import Flask from flask import render_template, request, g, redirect, url_for, flash from flask_login import LoginManager, current_user, login_user, logout_user, login_required from flask_log import Logging import threading import logging import inotify.adapters, inotify.constants import scripts.func as func from scripts import database from admin import admin from movies import movies from comics import comics class NullHandler(logging.Handler): def emit(self, record=None): pass def debug(self, *arg): pass nullLog = NullHandler() inotify.adapters._LOGGER = nullLog app = Flask(__name__) app.register_blueprint(comics.Comics) app.register_blueprint(admin.Admin) app.register_blueprint(movies.Movies) app.config["SECRET_KEY"] = "***REMOVED***" app.config["FLASK_LOG_LEVEL"] = "DEBUG" flask_log = Logging(app) login_manager = LoginManager(app) login_manager.login_view = "login" MOBILE_DEVICES = ["android", "blackberry", "ipad", "iphone"] def get_comics(): with app.app_context(): func.get_comics() database.verify_paths() i = inotify.adapters.InotifyTree(func.COMICS_DIRECTORY) for event in i.event_gen(yield_nones=False): (header, type_names, path, filename) = event file_path = path+"/"+filename if "IN_CLOSE_WRITE" in type_names: func.get_comic(file_path) if "IN_DELETE" in type_names: database.verify_path(file_path) def get_movies(): with app.app_context(): func.get_movies() i = inotify.adapters.InotifyTree(func.MOVIES_DIRECTORY) for event in i.event_gen(yield_nones=False): (header, type_names, path, filename) = event if "IN_CLOSE_WRITE" in type_names: func.get_movies() with app.app_context(): database.initialize_db() thread = threading.Thread(target=get_comics, args=()) thread.daemon = True thread.start() thread2 = threading.Thread(target=get_movies, args=()) thread2.daemon = True thread2.start() @app.teardown_appcontext def close_connection(exception): db = getattr(g, '_database', None) if db is not None: db.close() def update_comic_db(sender, **kw): try: database.add_comics(kw["meta"], kw["thumbnails"]) except Exception as e: print(e) def update_movie_db(sender, **kw): try: database.add_movies(kw["movies"]) except Exception as e: print(e) func.comic_loaded.connect(update_comic_db) func.movie_loaded.connect(update_movie_db) @login_manager.user_loader def load_user(username): return database.get_user(username) @app.route("/login", methods=["GET", "POST"]) def login(): try: if request.method == "POST": username = request.form.get("username") password = request.form.get("password") user = database.get_user(username) if user is None or not user.check_password(password): flash("invalid username or password") return redirect(url_for("login")) login_user(user) next_page = request.args.get("next") if not next_page: next_page = url_for("home") return redirect(next_page) return render_template("login.html", title="login") except Exception as e: return str(e) @app.route("/logout") def logout(): try: logout_user() return redirect(url_for("login")) except Exception as e: return str(e) @app.route("/") def root(): return redirect(url_for("home")) @app.route("/home") @login_required def home(): try: return render_template("home.html", title="Hello World", current_user=current_user) except Exception as e: return str(e) @app.route("/music") @login_required def music(): return "No music" @app.route("/games") @login_required def games(): return "No Games" if __name__ == '__main__': app.run()