This repository has been archived by the owner on Jun 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
289 lines (229 loc) · 8.11 KB
/
main.py
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
"""
(C) whsmith 2023 All Rights Reserved.
This code may not be used, copied, distributed, or reproduced in part or in whole
for commercial or personal purposes without the express written consent of the owner.
"""
# Import necessary libraries
import json
import os
from datetime import datetime
import pytz
import spotipy
from flask import (
Flask,
jsonify,
make_response,
redirect,
render_template,
request,
session,
url_for,
)
from flask_restful import Api, Resource
from pytube import YouTube
from spotipy.oauth2 import SpotifyClientCredentials
from youtubesearchpython import VideosSearch
from dotenv import load_dotenv
from flask_session import Session
# Import custom modules
from platform_detect import reg_b, reg_v
from redirect_config import redirects
# Load the local .env file into the environment
load_dotenv()
# Global Config
ADMIN_USERNAME = os.getenv("admin_username")
ADMIN_PASSWORD = os.getenv("admin_password")
REDIRECT_MOBILE = os.getenv("redirect_mobile").lower() in ["on", "yes", "true"]
MOBILE_VERSION_URL = os.getenv("mobile_version_url")
# Initialize Spotify API client
sp = spotipy.Spotify(
auth_manager=SpotifyClientCredentials(
client_id=os.getenv("spotify_client_id"),
client_secret=os.getenv("spotify_client_secret"),
)
)
# Create Flask application
app = Flask(__name__)
api = Api(app)
# Flask Configuration
flask_config = {
"CACHE": True, # Set flask asset caching to True
"SECRET_KEY": os.urandom(24), # Flask secret key for encryption
"SESSION_TYPE": "filesystem", # Flask Session local session data config
}
app.config.from_mapping(flask_config)
# Flask Extensions
Session(app)
# Helper function to detect mobile browsers
def detect_mobile_browser(req):
user_agent = req.headers.get("User-Agent")
if user_agent:
b = reg_b.search(user_agent)
v = reg_v.search(user_agent[0:4])
if b or v:
return True
return False
# Route Helpers
def serve_default_template(template: str, request):
if detect_mobile_browser(request) and REDIRECT_MOBILE:
return redirect(MOBILE_VERSION_URL)
# Load data from a JSON file
with open("static/custom_content/song_of_the_week.json") as f:
song_of_the_week = json.load(f)
return render_template(
template,
song_of_the_week=song_of_the_week,
signed_in=session.get("admin") if True else False,
)
# Define Routes
@app.route("/", methods=["GET"])
def _home():
return serve_default_template("home.html", request)
@app.route("/work", methods=["GET"])
def _work():
return serve_default_template("work.html", request)
@app.route("/socials", methods=["GET"])
def _socials():
return serve_default_template("socials.html", request)
@app.route("/admin", methods=["GET", "POST"])
def _admin():
# Check if the user is using a mobile browser
if detect_mobile_browser(request) and REDIRECT_MOBILE:
return redirect("https://m.whsmith.me?redirect=from_desktop")
if request.method == "POST":
username = request.form.get("username")
password = request.form.get("password")
if username == ADMIN_USERNAME and password == ADMIN_PASSWORD:
session["admin"] = True
print("success")
return redirect("/admin")
else:
print("Wrong")
for i in request.form.items():
print(i)
return render_template(
"login.html", error="Incorrect Username and or Password."
)
if session.get("admin"):
desired_timezone = "Europe/London"
current_time = datetime.now(pytz.timezone(desired_timezone))
greeting = (
"Good morning"
if current_time.hour < 12
else "Good afternoon"
if current_time.hour < 17
else "Good evening"
)
return render_template(
"admin.html",
signed_in=session.get("admin") if True else False,
greeting=greeting,
username=ADMIN_USERNAME.capitalize(),
)
else:
return render_template("login.html")
@app.route("/redirect/<name>")
def _redirect(name):
for entry in redirects:
if entry["name"] == name:
# Log the redirect details
with open("redirects_log.json") as f:
redirects_log = json.load(f)
redirects_log.append(
{
"date": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
"ip": request.remote_addr,
"user_agent": request.headers.get("User-Agent", default=None),
"redirect": name,
}
)
with open("redirects_log.json", "w+") as f:
json.dump(redirects_log, f, indent=4)
return redirect(entry["redirects_to"])
return "404 - Redirect not found", 404
# Define API resources
# Song of the week static resources and statistics
class SongOfTheWeek(Resource):
def get(self):
with open("static/custom_content/song_of_the_week.json") as f:
song_of_the_week = json.load(f)
return jsonify(song_of_the_week)
def post(self):
with open("static/custom_content/song_of_the_week_stats.json") as f:
song_of_the_week_stats = json.load(f)
song_of_the_week_stats["plays"] += 1
with open("static/custom_content/song_of_the_week_stats.json", "w+") as f:
json.dump(song_of_the_week_stats, f)
# Song of the week updating logic
class ManageSongOfTheWeek(Resource):
def put(self):
if not session.get("admin"):
return make_response(
jsonify(
{
"ok": False,
"error": "403 Forbidden - Not authorized to manage song of the week, please log in.",
}
),
403,
)
song_url = request.args.get("song_url")
if (
"https://www.youtube.com/watch?v=" not in song_url
and "https://open.spotify.com/track/" not in song_url
and "https://youtube.com/watch?v=" not in song_url
):
return make_response(
jsonify(
{
"ok": False,
"error": "Invalid 'song_url' parameter",
"song_url": song_url,
}
),
400,
)
else:
if "https://open.spotify.com/track/" in song_url:
spotify = True
else:
spotify = False
if spotify:
json_data = json.loads(json.dumps(sp.track(song_url)))
query = f'{json_data["name"]} - {json_data["artists"][0]["name"]}'
search = VideosSearch(query, limit=1)
url = (
f"https://www.youtube.com/watch?v={search.result()['result'][0]['id']}"
)
else:
url = song_url
yt = YouTube(url)
s = yt.streams.filter(only_audio=True).first()
s.download(
output_path="static/custom_content",
filename="song_of_the_week.mp3",
max_retries=3,
)
song_of_the_week_data_path = os.path.join(
"static", "custom_content", "song_of_the_week.json"
)
song_of_the_week_data = {
"track_name": json_data["name"],
"track_artists": "".join(
artist["name"] + " " for artist in json_data["artists"]
),
"url": song_url,
"avalible_at": "/static/custom_content/song_of_the_week.mp3",
}
with open(song_of_the_week_data_path, "w+") as f:
json.dump(song_of_the_week_data, f)
return make_response(
jsonify({"ok": True, "message": "Song of the Week updated successfully."}),
201,
)
# Add API resources to the API
api.add_resource(SongOfTheWeek, "/api/v1/song_of_the_week")
api.add_resource(ManageSongOfTheWeek, "/api/v1/song_of_the_week/")
# Run the Flask application
if __name__ == "__main__":
app.run(port=5000, debug=True)