Files
Twitch-Drops-Miner/TwitchChannelPointsMiner/classes/AnalyticsServer.py
2021-02-02 17:33:30 +01:00

50 lines
1.3 KiB
Python

import os
from multiprocessing import Process
from pathlib import Path
from flask import Flask, Response, render_template # , cli
from TwitchChannelPointsMiner.classes.Settings import Settings
def streamers_available():
path = Settings.analytics_path
return [
f
for f in os.listdir(path)
if os.path.isfile(os.path.join(path, f)) and f.endswith(".json")
]
def read_json(streamer):
path = Settings.analytics_path
streamer = streamer if streamer.endswith(".json") else f"{streamer}.json"
return Response(
open(os.path.join(path, streamer)) if streamer in streamers_available() else [],
status=200,
mimetype="application/json",
)
def index():
return render_template("charts.html", streamers=",".join(streamers_available()))
# cli.show_server_banner = lambda *_: None
class AnalyticsServer(Process):
def __init__(self):
super(AnalyticsServer, self).__init__()
self.app = Flask(
__name__,
template_folder=os.path.join(Path().absolute(), "assets"),
static_folder=os.path.join(Path().absolute(), "assets"),
)
self.app.add_url_rule("/", "index", index)
self.app.add_url_rule("/json/<string:streamer>", "json", read_json)
def run(self):
self.app.run()