-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
323 lines (286 loc) · 13.7 KB
/
Copy pathapp.py
File metadata and controls
323 lines (286 loc) · 13.7 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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
from flask import Flask, render_template_string, session, request
from get_data import get_data
from dotenv import load_dotenv
import latest_backup
import datetime
import time
import os
load_dotenv() # load env
app = Flask(__name__)
app.secret_key = os.getenv("FLASK_SECRET_KEY")
BM_ITEMS = get_data(regular=False)
RG_ITEMS = get_data(regular=True) # regular shop
images = BM_ITEMS[2] # dict of images
warn = BM_ITEMS[1]
BM_ITEMS = BM_ITEMS[0]
RG_ITEMS = RG_ITEMS[0]
allowed_regions = {"US", "EU", "IN", "CA", "AU", "XX"}
allowed_shops = {"regular", "blackMarket"}
project_root = os.path.abspath(os.path.join(os.path.dirname(__file__))) # for file operations (i.e. images)
@app.route("/.env")
@app.route("/.git/config")
@app.route("/js/lkk_ch.js")
@app.route("/config.php")
@app.route("/config.xml")
@app.route("/.ssh/id_ed25519")
@app.route("/database.sql")
def fu():
return "flip you bot :D"
@app.route("/")
def home():
# session stuff
session.permanent = True
session.modified = True
card_html = ""
region = request.args.get('region', session.get("region", "XX")).upper()
shop = request.args.get('shop', session.get("shop", "blackMarket")).lower()
backup = request.args.get('backup', session.get("backup", "latest"))
options_visible = session.get('options_visible')
school = request.args.get('school', type=bool, default=False)
if options_visible is None:
options_visible = False
session['options_visible'] = False
print(f"SESSION: {options_visible}")
# get all backups for dropdown
all_backups = latest_backup.get_all_backups()
backup_options = [("latest", "Latest")] + [(b, format_backup_date(b)) for b in all_backups]
if region not in allowed_regions:
region = "XX"
if shop not in allowed_shops:
shop = "blackMarket"
backup_file = None
use_backup = False
backup_warn = False
if backup != "latest" and backup in all_backups:
backup_file = latest_backup.get_backup_path(backup)
backup_BM_ITEMS = get_data(regular=False, backup=backup_file)
backup_RG_ITEMS = get_data(regular=True, backup=backup_file)
backup_images = backup_BM_ITEMS[2]
backup_warn = backup_BM_ITEMS[1]
backup_BM_ITEMS = backup_BM_ITEMS[0]
backup_RG_ITEMS = backup_RG_ITEMS[0]
use_backup = True
else:
use_backup = False
session["region"] = region
session["shop"] = shop
session["backup"] = backup
warning_html = ""
current_warn = backup_warn if use_backup else warn
if current_warn == True:
warning_html = """
<div class="card">
<img src="/static/noo.png" loading="lazy" class="item_image"/>
<div class="card-content">
<h2 class="item-title"><span style="color: orange;">Warning:</span> This is a backup!</h2>
<p class="item_description">right now, you are viewing a backup! This can either be because you selected a backup or the api I use is down D: It might be a bit outdated, so sorry for any inconvenience!</p>
</div>
</div>
"""
shop_list = [] # used to carry either black market or regular shop items
if not use_backup:
if shop == "blackMarket":
shop_list = BM_ITEMS
else:
shop_list = RG_ITEMS
else:
if shop == "blackMarket":
shop_list = backup_BM_ITEMS
else:
shop_list = backup_RG_ITEMS
current_images = backup_images if use_backup else images
for item in shop_list:
region_in_store = False
if region in item["prices"]:
price = item["prices"].get(region)
region_in_store = True
elif "XX" in item["prices"]:
price = item["prices"].get("XX")
region_in_store = True
else:
region_in_store = False
if region_in_store == True:
item_id = item.get("id")
title = item.get("title")
description = item.get("description")
# temporary for school lol
if school:
description = description.replace("fuc", "duc")
item_images = current_images.get(str(item_id), {})
backup_date = item_images.get("date", "")
image_filename = item_images.get("localImage", "")
image = os.path.join("static", "backups", backup_date, image_filename) if image_filename else item.get("imageUrl")
full_image_path = os.path.join(project_root, image)
if not os.path.isfile(full_image_path):
image = item.get("imageUrl")
buy_url = item.get("purchaseUrl")
card_html += f"""
<div class="card" id="card_{item_id}">
<img src="{image}" loading="lazy" class="item_image" id="img_{item_id}"/>
<div class="card-content">
<h2 class="item_title" id="title_{item_id}">{title}</h2>
<p class="item_description" id="desc_{item_id}">{description}</p>
<a href="{buy_url}" class="buy-link">
<button type="button" class="buy_button">
<img class="shell-icon" style="padding: 0.2rem" src="https://hc-cdn.hel1.your-objectstorage.com/s/v3/6c0178740fa623a059182d076f44031600d079d5_shell.png"/>
<span>{price} needed</span>
</button>
</a>
</div>
</div>
"""
html = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="a replica of Hack Club's Summer of Making Heidimarket, with accurate items and details!">
<meta property="og:title" content="orpheusmarket">
<meta property="og:description" content="a replica of Hack Club's Summer of Making Heidimarket, with accurate items and details!">
<meta property="og:image" content="https://orpheus.olive.hackclub.app/static/orpheus.png">
<meta property="og:url" content="https://orpheus.olive.hackclub.app/">
<meta property="og:type" content="website">
<link rel="canonical" href="https://orpheus.olive.hackclub.app/">
<link rel="stylesheet" href="static/style.css">
<link rel="apple-touch-icon" sizes="180x180" href="/static/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/static/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/static/favicon-16x16.png">
<link rel="manifest" href="/static/site.webmanifest">
<title>orpheusmarket</title>
</head>
<body>
<div class="bar">
<a href="https://summer.hackclub.com/shop/black_market"><h1 class="orpheusmarket">orpheusmarket</h1><img src="/static/orpheus.png" class="dino"/></a>
</div>
<div class="card option">
<button id="toggle-btn" onclick="toggleDropdowns()">{{ 'Hide Options' if options_visible else 'Show Options'}}</button>
</div>
<div class="region" style="display: {{ 'flex' if options_visible else 'none'}};">
<div>
<h3>Choose your region</h3>
<p>Prices and availability vary by region</p>
<div class="region-container">
<form method="get" action="/">
<select id="region-selector" class="region-selector" name="region" onchange="this.form.submit()">
<option value="US" {% if region == 'US' %}selected{% endif %}> United States </option>
<option value="EU" {% if region == 'EU' %}selected{% endif %}> EU + UK </option>
<option value="IN" {% if region == 'IN' %}selected{% endif %}> India </option>
<option value="CA" {% if region == 'CA' %}selected{% endif %}> Canada </option>
<option value="AU" {% if region == 'AU' %}selected{% endif %}> Australia </option>
<option value="XX" {% if region == 'XX' %}selected{% endif %}> Rest of World </option>
</select>
</form>
</div>
</div>
<div>
<h3>Choose your shop</h3>
<p>You can view items for both the regular shop and orpheusmarket!</p>
<div class="region-container">
<form method="get" action="/">
<select id="shop-selector" class="region-selector" name="shop" onchange="this.form.submit()">
<option value="regular" {% if shop == 'regular' %}selected{% endif %}> regular shop </option>
<option value="blackMarket" {% if shop == 'blackMarket' %}selected{% endif %}> orpheusmarket </option>
</select>
</form>
</div>
</div>
<div>
<h3>Choose a date</h3>
<p>You can view what the shop looked like at a specific date in time (all in your timezone!)</p>
<div class="region-container">
<form method="get" action="/">
<select id="date-selector" class="region-selector" name="backup" onchange="this.form.submit()">
{% for backup_value, backup_label in backup_options %}
<option value="{{backup_value}}" {% if backup == backup_value %}selected{% endif %}>{{backup_label}}</option>
{% endfor %}
</select>
</form>
</div>
</div>
</div>
<script>
// this basically converts dates from utc to ur time zone :D
document.addEventListener('DOMContentLoaded', function(){
const dateSelector = document.getElementById('date-selector');
if (!dateSelector) return;
for (const option of dateSelector.options) {
const isoString = option.textContent;
if (isoString === 'Latest' || !isoString.includes('T')) { // all dates have T in them
continue;
}
try {
const date = new Date(isoString)
const formattedLocal = date.toLocaleString(undefined, {
month: 'short',
day: 'numeric',
year: 'numeric',
hour: 'numeric',
minute: '2-digit',
hour12: true,
timeZoneName: 'short'
});
option.textContent = formattedLocal;
} catch (e){
console.error("couldn't parse date:", isoString, e);
}
}
})
function toggleDropdowns() {
var regionDiv = document.querySelector('.region');
var btn = document.querySelector('#toggle-btn');
var isVisible = regionDiv.style.display !== 'none';
var newVisible = !isVisible;
regionDiv.style.display = newVisible ? 'flex' : 'none';
btn.textContent = newVisible ? 'Hide Options' : 'Show Options';
fetch('/toggle_options', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({visible: newVisible})
}); // update session
}
</script>
{{warning_html|safe}}
<div class="card">
<img src="/static/orpheus.png" loading="lazy" class="item_image"/>
<div class="card-content">
<h2 class="item-title">Follow me on SOM!</h2>
<p class="item_description">this was a project made for Hack Club's Summer of Making!</p>
<a href="https://summer.hackclub.com/projects/12114" class="buy-link">
<button type="button" class="buy_button">
<span>Follow!</span>
</button>
</a>
</div>
</div>
<div class="card_container">
{{card_html|safe}}
</div>
<div class="footer"><a href="https://github.com/DaBlower">Made with ❤️ by obob!</a></div>
</body>
</html>
"""
return render_template_string(html, card_html=card_html, region=region, shop=shop, warning_html=warning_html, backup_options=backup_options, backup=backup, options_visible=options_visible)
@app.route("/toggle_options", methods=["POST"])
def toggle_options():
data = request.get_json(silent=True)
if data and 'visible' in data:
session['options_visible'] = data['visible']
print(f"Explicitly set to {data['visible']}")
else:
current_state = session.get('options_visible', False)
session['options_visible'] = not current_state
print(f"Toggled to {session['options_visible']}")
session.modified = True
return '', 204
def format_backup_date(date):
try:
dt = datetime.datetime.strptime(date, '%Y-%m-%d_%H-%M-%S')
local_tz = datetime.datetime.now().astimezone().tzinfo
dt_local = dt.replace(tzinfo=local_tz) # assume the backup was created on the server's local time
return dt_local.astimezone(datetime.timezone.utc).isoformat()
except Exception as e:
print(f"Failed to convert date!: {e}")
return date
if __name__ == "__main__":
app.run(port=38015)