rpiwebapp-public/app.py

149 lines
3.7 KiB
Python
Raw Normal View History

2019-07-06 23:00:00 -07:00
from flask import Flask
from flask import render_template, request, g, redirect, url_for
2019-07-06 23:00:00 -07:00
from urllib import parse
import threading
import base64
2019-07-06 23:00:00 -07:00
import scripts.func as func
from scripts import database
app = Flask(__name__)
def get_comics():
with app.app_context():
func.get_comics()
def verify_paths():
with app.app_context():
database.verify_paths()
with app.app_context():
database.initialize_db()
thread = threading.Thread(target=get_comics, args=())
thread.daemon = True
thread.start()
thread2 = threading.Thread(target=verify_paths, 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"])
2019-07-06 23:00:00 -07:00
except Exception as e:
print(e)
func.comic_loaded.connect(update_comic_db)
@app.route('/')
def hello_world():
return render_template("index.html", title="Hello World")
@app.route("/movies")
def movies():
return "No Movies"
@app.route("/music")
def music():
return "No music"
@app.route("/games")
def games():
return "No Games"
@app.route("/comics")
def comics():
polling = request.args.get("polling")
if polling:
try:
return render_template("publisherList.html", comics=database.get_publishers())
except Exception as e:
print(e)
return e
2019-07-06 23:00:00 -07:00
try:
return render_template("publisherView.html", title="Comics", comics=database.get_publishers())
except Exception as e:
print(e)
return e
@app.route("/comics/")
def comic_reroute():
return redirect(url_for("comics"))
2019-07-06 23:00:00 -07:00
@app.route("/comics/<publisher>")
def comics_publisher(publisher):
publisher = parse.unquote(publisher)
series = request.args.get("series")
series_year = request.args.get("seriesYear")
comic_path = request.args.get("path")
2019-07-06 23:00:00 -07:00
if series:
if comic_path:
comic_path = parse.unquote(comic_path)
return comic_viewer(comic_path, publisher, series, series_year)
return render_template("seriesView.html", title="Comics", publisher=publisher, seriesYear=series_year, comics=database.db_get_comics_in_series(series, publisher, series_year))
return render_template("publisherSeriesView.html", title="Comics", comics=database.db_get_series_by_publisher(publisher))
def comic_viewer(comic_path, publisher, series, series_year):
try:
comic_path_parsed = parse.quote(comic_path)
publisher_parsed = parse.quote(publisher)
series_parsed = parse.quote(series)
page_number = int(request.args.get("pageNumber"))
comic = func.open_comic(comic_path)
page_count = comic.getNumberOfPages()
prev_page = page_number - 1
next_page = page_number + 1
if next_page >= page_count:
next_page = 0
if prev_page < 0:
prev_page = page_count - 1
page = comic.getPage(page_number)
page = str(base64.b64encode(page))[2:-1]
prev_url = "/comics/{}?series={}&seriesYear={}&path={}&pageNumber={}".format(publisher_parsed, series_parsed, series_year, comic_path_parsed, prev_page)
next_url = "/comics/{}?series={}&seriesYear={}&path={}&pageNumber={}".format(publisher_parsed, series_parsed, series_year, comic_path_parsed, next_page)
return render_template("comicView.html", title="Comics", page=page, page_count=page_count, prev_url=prev_url, next_url=next_url)
except Exception as e:
print(e)
return e
@app.route("/comics/getPage")
def get_comic_page():
path = parse.unquote(request.args.get("path"))
page_number = int(request.args.get("pageNumber"))
comic = func.open_comic(path)
if page_number > comic.getNumberOfPages()-1:
page_number = 1
if page_number <= 0:
page_number = comic.getNumberOfPages()-1
page = comic.getPage(page_number)
page = base64.b64encode(page)
return page
2019-07-06 23:00:00 -07:00
if __name__ == '__main__':
app.run()