-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWebServer.py
More file actions
60 lines (51 loc) · 2.58 KB
/
Copy pathWebServer.py
File metadata and controls
60 lines (51 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
try:
# Try to import all modules
try:
from flask import Flask, request, render_template # Web-management engine
import json
from waitress import serve
import os.path
import sys
except ImportError:
print("[WARN] You are missing one or more libraries. This script cannot continue.")
print("Try running in terminal >> python3 -m pip install -r requirements.txt")
quit()
# Set port number and name according to script arguments
if len(sys.argv) > 2:
webport = int(sys.argv[-2])
name = str(sys.argv[-1])
# Determine main program directory
maindirectory = os.path.dirname(os.path.abspath(__file__)) # The absolute path to this file
# Custom Functions
def suggestion_dump(link):
data = {}
data["Suggestion"] = []
data["Suggestion"].append({"Link": str(link)})
with open(str(maindirectory) + "/SuggestionDump" + str(name) + ".json", "w") as jsonfile:
json.dump(data, jsonfile)
# Init Flask
app = Flask(__name__)
print(f"[INFO] WebServer is now running. View station info and suggest songs here with port {webport}.")
@app.route("/")
def index():
# Open JSON file
try:
with open(str(maindirectory) + "/VariableDump" + str(name) + ".json", "r") as json_file:
statistics_dict_parent = json.load(json_file)
statistics_dict = statistics_dict_parent["Statistics"][0]
json_file.close()
return render_template("index.html", PlaylistURL=statistics_dict["PlaylistURL"], SongsPlayedNum=statistics_dict["SongsPlayedNum"], SongTitle=statistics_dict["SongTitle"], EmbedLink=statistics_dict["EmbedLink"], SongLink=statistics_dict["SongLink"], WeatherDecimal=statistics_dict["WeatherDecimal"], PSADecimal=statistics_dict["PSADecimal"], WelcomeDecimal=statistics_dict["WelcomeDecimal"], WeekdayDecimal=statistics_dict["WeekdayDecimal"], TimeDecimal=statistics_dict["TimeDecimal"])
except FileNotFoundError:
return render_template("index.html")
@app.route("/", methods=["POST"])
def echo():
command = request.form["videosuggestion"]
suggestion_dump(command)
print("[INFO] Suggestion received!")
return index()
if __name__ == "__main__":
# app.run(debug="False", port=4024, host="0.0.0.0") # [No longer used. Run app through Flask]
serve(app, host="0.0.0.0", port=webport) # Start app with waitress
except OSError:
print("[INFO] The webserver is already running. This instance will exit.", end="\n\n")
quit()