-
Notifications
You must be signed in to change notification settings - Fork 722
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #96 from Tkd-Alex/analytics
Show analytics of your points mining
- Loading branch information
Showing
12 changed files
with
448 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -148,4 +148,5 @@ chromedriver* | |
cookies/* | ||
logs/* | ||
screenshots/* | ||
htmls/* | ||
htmls/* | ||
analytics/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import logging | ||
import os | ||
from pathlib import Path | ||
from threading import Thread | ||
|
||
from flask import Flask, Response, cli, render_template | ||
|
||
from TwitchChannelPointsMiner.classes.Settings import Settings | ||
|
||
cli.show_server_banner = lambda *_: None | ||
logger = logging.getLogger(__name__) | ||
|
||
|
||
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(refresh=5): | ||
return render_template( | ||
"charts.html", | ||
refresh=(refresh * 60 * 1000), | ||
streamers=",".join(streamers_available()), | ||
) | ||
|
||
|
||
class AnalyticsServer(Thread): | ||
def __init__(self, host: str = "127.0.0.1", port: int = 5000, refresh: int = 5): | ||
super(AnalyticsServer, self).__init__() | ||
|
||
self.host = host | ||
self.port = port | ||
self.refresh = refresh | ||
|
||
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, defaults={"refresh": refresh}) | ||
self.app.add_url_rule("/json/<string:streamer>", "json", read_json) | ||
|
||
def run(self): | ||
logger.info( | ||
f"Analytics running on http://{self.host}:{self.port}/", | ||
extra={"emoji": ":globe_with_meridians:"}, | ||
) | ||
self.app.run(host=self.host, port=self.port, threaded=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.