-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.py
885 lines (777 loc) · 30.2 KB
/
server.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
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
"""Copyright 2023 Poney!
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the “Software”), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE."""
import ast
import base64
import math
import os
import re
from urllib.parse import quote, urlencode
import requests
import uvicorn
from dotenv import load_dotenv
from fastapi import FastAPI, File, UploadFile
from fastapi.middleware.gzip import GZipMiddleware
from fastapi.middleware.httpsredirect import HTTPSRedirectMiddleware
from fastapi.middleware.trustedhost import TrustedHostMiddleware
from fastapi.responses import PlainTextResponse
from starlette.middleware.sessions import SessionMiddleware
from starlette.requests import Request
from starlette.responses import FileResponse, RedirectResponse, Response
from starlette.staticfiles import StaticFiles
from starlette.templating import Jinja2Templates
from starlette_discord.client import DiscordOAuthClient
from api_engine import extract_tags_v2
from db import ShipImageDatabase
from png_upload import upload_image_to_imgbb, upload_image_to_cloudinary
from sitemap import generate_sitemap, generate_url_tags, generate_url_authors
load_dotenv()
print("loading")
MAX_SHIPS_PER_PAGE = 24
db_manager = ShipImageDatabase()
client_id = os.getenv("discord_id")
client_secret = os.getenv("discord_secret")
redirect_uri = os.getenv("discord_redirect")
client = DiscordOAuthClient(client_id, client_secret, redirect_uri, ("identify", "guilds"))
api_uri = os.getenv("api_uri")
trusted_host = os.getenv("trusted_host")
app = FastAPI()
app.mount("/static", StaticFiles(directory="static"), name="static")
templates = Jinja2Templates(directory="templates")
db_manager.init_db()
modlist = os.getenv("mods_list")
modlist = ast.literal_eval(modlist)
@app.get("/robots.txt", response_class=PlainTextResponse)
def robots():
"""
A function that returns the content for a robots.txt file.
"""
data = """User-agent: *\nDisallow: \nCrawl-delay: 5"""
return data
@app.get("/sitemap.xml")
async def get_sitemap():
"""
A function that generates the sitemap.xml file by writing the output
of generate_sitemap() to 'static/sitemap.xml'.
If an exception occurs during the file write operation, it serves the static file.
Returns a FileResponse object for the sitemap.xml file.
"""
try:
with open("static/sitemap.xml", "w", encoding="utf-8") as f:
f.write(generate_sitemap())
except Exception as e:
# not writable access, serve static file
print(e)
# xmldata = generate_sitemap()
return FileResponse("static/sitemap.xml", media_type="application/xml")
@app.get("/ship/{ship_id}")
async def get_image(ship_id: int, request: Request):
"""
A function that handles the retrieval of an image based on the provided ID.
It checks user session data, manages favorites, updates session information,
retrieves image data from the database, handles API requests for analysis,
and returns a template response with relevant data for the ship image page.
Parameters:
id (int): The ID of the image to retrieve.
request (Request): The request object containing session and query data.
Returns:
templates.TemplateResponse: The response containing data for rendering the ship image page.
"""
user = request.session.get("discord_user")
if not request.session.get("shipidsession"):
shipidsession = []
request.session["shipidsession"] = shipidsession
else:
shipidsession = request.session.get("shipidsession")
fav = 0
if user:
litsid = db_manager.get_my_favorite(user)
ids = [item[0] for item in litsid]
if ship_id in ids:
fav = 1
if not user:
user = "Guest"
if ship_id not in shipidsession:
request.session["shipidsession"].append(ship_id)
db_manager.update_downloads(ship_id)
image_data = db_manager.get_image_data(ship_id)
url_png = image_data[0][2] # change to send the url instead of the image
datadata = {}
return templates.TemplateResponse(
"ship.html",
{
"request": request,
"image": image_data,
"user": user,
"url_png": url_png,
"modlist": modlist,
"fav": fav,
"datadata": datadata,
},
)
@app.get("/delete/{ship_id}")
async def delete_image(ship_id: int, request: Request):
"""
Delete image information from the database based on the provided ID.
Parameters:
id (int): The ID of the image to be deleted.
request (Request): The HTTP request object.
Returns:
RedirectResponse: A redirect response to the home page if the image is successfully deleted.
"""
user = request.session.get("discord_user")
check = db_manager.delete_ship(ship_id, user)
if check is not None:
return RedirectResponse("/")
return RedirectResponse("/")
@app.get("/favorite/{ship_id}")
async def favorite(ship_id: int, request: Request):
"""
Adds a ship to the user's favorites and redirects to the ship page.
Parameters:
ship_id (int): The ID of the ship to be added to favorites.
request (Request): The HTTP request object.
Returns:
RedirectResponse: A redirect response to the ship page if the user is logged in.
"""
user = request.session.get("discord_user")
if not user:
return RedirectResponse("/login")
user = request.session.get("discord_user")
db_manager.add_to_favorites(user, ship_id)
db_manager.add_fav(ship_id)
url = "/ship/" + str(ship_id)
return RedirectResponse(url)
@app.get("/rmfavorite/{ship_id}")
async def rmfavorite(ship_id: int, request: Request):
"""
Remove a ship from the user's favorites and redirect to the ship page.
Parameters:
ship_id (int): The ID of the ship to be removed from favorites.
request (Request): The HTTP request object.
Returns:
RedirectResponse: A redirect response to the ship page if the user is logged in.
"""
user = request.session.get("discord_user")
if not user:
return RedirectResponse("/login")
user = request.session.get("discord_user")
db_manager.delete_from_favorites(user, ship_id)
db_manager.remove_fav(ship_id)
url = "/ship/" + str(ship_id)
return RedirectResponse(url)
@app.get("/myfavorite")
async def myfavorite(request: Request):
"""
A function to handle the "/myfavorite" route.
Parameters:
request (Request): The HTTP request object.
Returns:
TemplateResponse: A response based on user's favorite images and brand.
"""
user = request.session.get("discord_user")
if not user:
return RedirectResponse("/login?button=myfavorite")
images = db_manager.get_my_favorite(user)
rows = db_manager.get_my_favorite_pages(user)
pages = math.ceil(rows[0][0] / MAX_SHIPS_PER_PAGE)
return templates.TemplateResponse(
"indexpop.html", {"request": request, "images": images, "user": user, "maxpage": pages}
)
@app.get("/edit/{ship_id}")
async def edit_image(ship_id: int, request: Request):
"""
Replace image information from the database based on the provided ID
"""
user = request.session.get("discord_user")
check = db_manager.edit_ship(ship_id, user)
if check == "ko":
return RedirectResponse("/")
brand = request.session.get("brand")
if not brand:
brand = request.session.get("discord_server")
return templates.TemplateResponse(
"edit.html", {"request": request, "image": check, "user": user, "brand": brand}
)
@app.post("/edit/{ship_id}")
async def edit_image_post(ship_id: int, request: Request):
"""
Edit an image in the database based on the provided ship ID.
Parameters:
ship_id (int): The ID of the ship to be edited.
request (Request): The request object containing the session and form data.
Returns:
RedirectResponse: A redirect response to the home page if the edit is successful,
or to the home page if the edit fails.
Raises:
None.
"""
user = request.session.get("discord_user")
form_data = await request.form()
check = db_manager.post_edit_ship(ship_id, form_data, user)
if check == "ko":
return RedirectResponse("/")
return RedirectResponse(url="/", status_code=303)
@app.get("/update/{ship_id}")
async def update_image(ship_id: int, request: Request):
"""
Update image information from the database based on the provided ID
Parameters:
ship_id (int): The ID of the ship to be updated.
request (Request): The HTTP request object containing user information.
Returns:
TemplateResponse: A response containing the update page with relevant data.
"""
user = request.session.get("discord_user")
check = db_manager.edit_ship(ship_id, user)
if check == "ko":
return RedirectResponse("/") # this should be an "you dont have the rights" page
# brand = request.session.get("brand")
# if not brand:
# brand = request.session.get("discord_server")
if not user:
return RedirectResponse("/login")
return templates.TemplateResponse(
"update.html",
{
"request": request,
"image": check,
"user": user,
}, # "brand": brand}
)
@app.post("/update/{ship_id}")
async def upload_update(ship_id: int, request: Request, file: UploadFile = File(...)):
"""
A function to handle the upload of an image update for a ship.
Parameters:
ship_id (int): The ID of the ship to be updated.
request (Request): The HTTP request object containing user information.
file (UploadFile): The file object containing the image to be uploaded.
Returns:
RedirectResponse: Redirects to the edit page of the ship with status code 303.
"""
user = request.session.get("discord_user")
check = db_manager.edit_ship(ship_id, user)
if check == "ko":
return RedirectResponse("/") # this should be an "you dont have the rights" page
contents = await file.read()
encoded_data = base64.b64encode(contents).decode("utf-8")
# url_png = upload_image_to_imgbb(encoded_data)
url_png = upload_image_to_cloudinary(encoded_data)
if url_png == "ko":
error = "Upload servers are down, try again later"
return templates.TemplateResponse("badfile.html", {"request": request, "error": error})
try:
tags, author, crew, price = extract_tags_v2(url_png)
except Exception:
error = "API tag extractor is down, try again later"
return templates.TemplateResponse("badfile.html", {"request": request, "error": error})
if price == "unknown" or crew == "unknown":
error = "unable to decode file provided, check upload guide below"
return templates.TemplateResponse("badfile.html", {"request": request, "error": error})
file_name = file.filename
authorized_chars = re.sub(r"[^\w\-_.]", "_", file_name)
shipname = authorized_chars
if ".png" in shipname:
shipname = authorized_chars.replace(".png", "")
if ".ship" in shipname:
shipname = shipname.replace(".ship", "")
data = {
"id": ship_id,
"url_png": url_png,
"price": price,
"crew": crew,
"tags": tags,
}
db_manager.upload_update(data)
return RedirectResponse(url="/edit/" + str(ship_id), status_code=303)
@app.route("/login")
async def start_login(request: Request):
"""
Function that handles the login route based on the button clicked,
storing the button state in the session
and redirecting the user accordingly. Returns different redirects based on the button clicked.
"""
button_clicked = request.query_params.get("button")
if button_clicked == "upload":
request.session["button_clicked"] = "upload" # Store button state in the session
user = request.session.get("discord_user")
if not user:
return client.redirect(request)
return RedirectResponse("/initupload") # Skip the login and redirect to initupload
if button_clicked == "myships":
request.session["button_clicked"] = "myships" # Store button state in the session
user = request.session.get("discord_user")
if not user:
return client.redirect(request)
return RedirectResponse("/myships") # Redirect to the same login route
if button_clicked == "myfavorite":
request.session["button_clicked"] = "myfavorite" # Store button state in the session
user = request.session.get("discord_user")
if not user:
return client.redirect(request)
return RedirectResponse("/myfavorite") # Redirect to the same login route
request.session["button_clicked"] = "login" # Store button state in the session
return client.redirect("/login") # Redirect to the same login route
@app.get("/callback")
async def finish_login(request: Request):
"""
Function that handles the callback route after the user has logged in.
"""
code = request.query_params.get("code")
async with client.session(code) as session:
user = await session.identify()
guilds = await session.guilds()
if not user:
return RedirectResponse("/login")
request.session["discord_user"] = str(user)
desired_id = (
546229904488923141 # Excelsior server 546229904488923141 / Cosmoteer 314103695568666625
)
second_id = 314103695568666625
for guild in guilds:
if guild.id == desired_id:
request.session["discord_server"] = "exl"
request.session["brand"] = "exl"
redirect_url = "/"
button_clicked = request.session.pop(
"button_clicked", None
) # Retrieve button state from the session
if button_clicked == "upload":
redirect_url = "/initupload"
elif button_clicked == "myships":
redirect_url = "/myships"
elif button_clicked == "myfavorite":
redirect_url = "/myfavorite"
return RedirectResponse(redirect_url)
for guild in guilds: # to ensure higher privilege
if guild.id == second_id:
request.session["discord_server"] = "gen"
redirect_url = "/"
button_clicked = request.session.pop(
"button_clicked", None
) # Retrieve button state from the session
if button_clicked == "upload":
redirect_url = "/initupload"
elif button_clicked == "myships":
redirect_url = "/myships"
elif button_clicked == "myfavorite":
redirect_url = "/myfavorite"
return RedirectResponse(redirect_url)
return templates.TemplateResponse("auth.html", {"request": request, "user": None})
@app.get("/logoff")
async def logoff(request: Request):
"""
Logs off the user by removing the "discord_user" key from the session.
Parameters:
request (Request): The HTTP request object.
Returns:
RedirectResponse: A redirect response to the root URL ("/").
"""
request.session.pop("discord_user", None)
return RedirectResponse("/")
@app.get("/initupload", response_class=FileResponse) # DONE
async def upload_page(request: Request):
"""
A function to handle the "/initupload" route.
Retrieves user and brand information from the request's session,
checks and assigns the brand if not present, redirects to the login page
if the user is not logged in, and returns a TemplateResponse with the "initupload.html"
template along with the request, user, and brand data.
"""
user = request.session.get("discord_user")
if not user:
return RedirectResponse("/login?button=upload")
brand = request.session.get("brand")
if not brand:
brand = request.session.get("discord_server")
return templates.TemplateResponse(
"initupload.html", {"request": request, "user": user, "brand": brand}
)
@app.post("/initupload") # DONE
async def init_upload(request: Request, file: UploadFile = File(...)):
"""
A function to handle the initial upload of an image.
Parameters:
request (Request): The HTTP request object.
file (UploadFile): The file object containing the image to be uploaded.
Returns:
TemplateResponse: A response containing the uploaded image data and metadata.
"""
contents = await file.read()
encoded_data = base64.b64encode(contents).decode("utf-8")
# url_png = upload_image_to_imgbb(encoded_data)
url_png = upload_image_to_cloudinary(encoded_data)
if url_png == "ko":
error = "Upload servers are down, try again later"
return templates.TemplateResponse("badfile.html", {"request": request, "error": error})
try:
tags, author, crew, price = extract_tags_v2(url_png)
except Exception:
error = "API tag extractor is down, try again later"
return templates.TemplateResponse("badfile.html", {"request": request, "error": error})
if price == "unknown" or crew == "unknown":
error = "unable to decode file provided, check upload guide below"
return templates.TemplateResponse("badfile.html", {"request": request, "error": error})
file_name = file.filename
authorized_chars = re.sub(r"[^\w\-_.]", "_", file_name)
shipname = authorized_chars
if ".png" in shipname:
shipname = authorized_chars.replace(".png", "")
if ".ship" in shipname:
shipname = shipname.replace(".ship", "")
# brand check
brand = request.session.get("brand")
if not brand:
brand = request.session.get("discord_server")
data = {
"name": authorized_chars,
"url_png": url_png,
"author": author,
"shipname": shipname,
"price": price,
"crew": crew,
"brand": brand,
}
request.session["upload_data"] = data
return templates.TemplateResponse(
"upload.html",
{"request": request, "data": data, "tags": tags, "crew": crew, "brand": brand},
)
@app.post("/upload") # DONE
async def upload(request: Request):
"""
Uploads an image to the server.
Parameters:
request (Request): The HTTP request object.
Returns:
RedirectResponse: A redirect response to the root URL ("/").
"""
user = request.session.get("discord_user")
form_data = await request.form()
db_manager.upload_image(form_data, user)
return RedirectResponse(url="/", status_code=303)
@app.get("/download/{image_id}")
async def download_ship(image_id: str):
"""
Downloads a ship image based on the given image ID.
Args:
image_id (str): The ID of the image to download.
Returns:
Response: The downloaded image as a response with the appropriate content type and filename.
str: If the image ID is not found or the image cannot be fetched from the URL.
Raises:
None
"""
result = db_manager.download_ship_png(image_id)
if result:
image_url, filename = result
response = requests.get(image_url, timeout=30)
if response.status_code == 200:
content_type = response.headers.get("content-type", "application/octet-stream")
encoded_filename = quote(filename.encode("utf-8"))
headers = {"Content-Disposition": f"attachment; filename={encoded_filename}"}
return Response(content=response.content, media_type=content_type, headers=headers)
return "Failed to fetch the image from the URL"
return "Image not found"
@app.get("/") # DONE
async def index(request: Request):
"""
Handle the root route ("/") and render the appropriate template based on the user's brand.
Parameters:
request (Request): The HTTP request object.
Returns:
TemplateResponse: The rendered template with the appropriate images and user information.
"""
user = request.session.get("discord_user")
if not user:
user = "Guest"
images = db_manager.get_index()
rows = db_manager.get_pages()
pages = math.ceil(rows[0][0] / MAX_SHIPS_PER_PAGE)
return templates.TemplateResponse(
"indexpop.html", {"request": request, "images": images, "user": user, "maxpage": pages}
)
@app.get("/myships")
async def index_get(request: Request):
"""
A function to handle the "/myships" route, retrieves user information, images, and brand to
render the appropriate template.
"""
user = request.session.get("discord_user")
if not user:
return RedirectResponse("/login?button=myships")
images = db_manager.get_my_ships(user)
rows = db_manager.get_my_ships_pages(user)
pages = math.ceil(rows[0][0] / MAX_SHIPS_PER_PAGE)
return templates.TemplateResponse(
"indexpop.html", {"request": request, "images": images, "user": user, "maxpage": pages}
)
@app.post("/")
async def home(request: Request):
"""
A function to handle the "/" route, processes form input to build a SQL query
based on search tags, constructs a redirect URL with query parameters, and redirects
the user to the search route.
Parameters:
- request: Request object containing information about the incoming request.
Return Type:
- RedirectResponse: Redirects the user to the search route with the constructed
query parameters.
"""
user = request.session.get("discord_user")
fulltext = None
ftauthor = None
exlstrip = None
if not user:
user = "Guest"
tags_list = [
"cannon",
"deck_cannon",
"emp_missiles",
"flak_battery",
"he_missiles",
"large_cannon",
"mines",
"nukes",
"railgun",
"ammo_factory",
"emp_factory",
"he_factory",
"mine_factory",
"nuke_factory",
"disruptors",
"heavy_laser",
"ion_beam",
"ion_prism",
"laser",
"mining_laser",
"point_defense",
"boost_thruster",
"airlock",
"campaign_factories",
"explosive_charges",
"fire_extinguisher",
"no_fire_extinguishers",
"chaingun",
"large_reactor",
"large_shield",
"medium_reactor",
"sensor",
"small_hyperdrive",
"large_hyperdrive",
"rocket_thruster",
"small_reactor",
"small_shield",
"tractor_beams",
"hyperdrive_relay",
"bidirectional_thrust",
"mono_thrust",
"multi_thrust",
"omni_thrust",
"no_thrust",
"armor_defenses",
"mixed_defenses",
"shield_defenses",
"no_defenses",
"kiter",
"diagonal",
"avoider",
"mixed_weapons",
"painted",
"unpainted",
"splitter",
"utility_weapons",
"rammer",
"orbiter",
"campaign_ship",
"builtin",
"elimination_ship",
"domination_ship",
"scout/racer",
"broadsider",
"waste_ship",
"debugging_tool",
"sundiver",
"cargo_ship",
"spinner",
]
form_input = await request.form()
query: str = form_input.get("query").strip()
authorstrip: str = form_input.get("author").strip()
orderstrip: str = form_input.get("order").strip()
words = query.lower().split(" ")
descstrip: str = form_input.get("desc").strip()
minstrip: int = form_input.get("min-price").strip()
maxstrip: int = form_input.get("max-price").strip()
crewstrip: int = form_input.get("max-crew").strip()
if form_input.get("exl-only"):
exlstrip = form_input.get("exl-only").strip()
query_tags = []
for word in words:
if word.startswith("-"):
tag = word[1:]
value = 0
if tag in tags_list:
query_tags.append((tag, value))
elif word.startswith("fulltext="):
fulltext = word[9:]
elif word.startswith("ftauthor="):
ftauthor = word[9:]
else:
tag = word
value = 1
if tag in tags_list:
query_tags.append((tag, value))
if fulltext:
query_tags.append(("fulltext", fulltext))
if ftauthor:
query_tags.append(("ftauthor", ftauthor))
if authorstrip:
query_tags.append(("author", authorstrip))
if descstrip:
query_tags.append(("desc", descstrip))
if orderstrip:
query_tags.append(("order", orderstrip))
if not orderstrip:
query_tags.append(("order", "new"))
if crewstrip:
query_tags.append(("max-crew", crewstrip))
if exlstrip:
query_tags.append(("brand", "exl"))
query_tags.append(("minprice", minstrip))
query_tags.append(("maxprice", maxstrip))
query_params = {}
for tag, value in query_tags:
query_params[tag] = str(value)
base_url = request.url_for("search")
redirect_url = f"{base_url}?"
redirect_url += urlencode(query_params)
redirect_url += "#results"
return RedirectResponse(redirect_url, status_code=307)
@app.get("/search")
async def search(request: Request):
"""
Handle the "/search" route to retrieve and display search results based on query parameters.
Parameters:
request (Request): The HTTP request object containing session and query parameters.
Returns:
TemplateResponse: The rendered template with search results and user information.
"""
user = request.session.get("discord_user")
if not user:
user = "Guest"
query_params = request.query_params
images = db_manager.get_search(query_params)
rows = db_manager.get_pages_search(query_params)
pages = math.ceil(rows[0][0] / MAX_SHIPS_PER_PAGE)
return templates.TemplateResponse(
"indexpop.html", {"request": request, "images": images, "user": user, "maxpage": pages}
)
@app.post("/search")
async def search_post(request: Request):
"""
Handle the "/search" route to retrieve and display search results based on query parameters.
Parameters:
request (Request): The HTTP request object containing session and query parameters.
Returns:
TemplateResponse: The rendered template with search results and user information.
"""
user = request.session.get("discord_user")
if not user:
user = "Guest"
query_params = request.query_params
images = db_manager.get_search(query_params)
rows = db_manager.get_pages_search(query_params)
# pages is number of row / 60 int up
pages = math.ceil(rows[0][0] / MAX_SHIPS_PER_PAGE)
return templates.TemplateResponse(
"indexpop.html", {"request": request, "images": images, "user": user, "maxpage": pages}
)
@app.get("/seo_tags")
async def get_seo_tags(request: Request):
"""display seo tags"""
user = request.session.get("discord_user")
if not user:
user = "Guest"
tags = generate_url_tags()
authors = generate_url_authors()
return templates.TemplateResponse(
"seo_tags.html", {"request": request, "user": user, "tags": tags, "authors": authors}
)
@app.get("/seo_about")
async def get_seo_about(request: Request):
"""display seo about page"""
user = request.session.get("discord_user")
if not user:
user = "Guest"
return templates.TemplateResponse("seo_about.html", {"request": request, "user": user})
@app.get("/authors")
async def get_authors():
"""
Retrieves a list of authors from the database.
Returns:
dict: A dictionary containing the list of authors.
Example:
>>> await get_authors()
{'authors': ['John Doe', 'Jane Smith', 'Alice Johnson']}
"""
query_result = db_manager.get_authors()
authors = [author for (author,) in query_result["authors"]]
return {"authors": authors}
@app.get("/analyze")
async def get_analyze(request: Request):
"""
A route to analyze a URL and return the extracted tags.
Parameters:
request (Request): The HTTP request object.
Returns:
dict: A dictionary containing the extracted tags from the provided URL.
"""
try:
query_params = request.query_params
print("query_params = ", query_params)
url = query_params.get("url")
datadata = extract_tags_v2(url, analyze=True)
return {"datadata": datadata}
except Exception:
return {"datadata": "Error"}
@app.get("/{catchall:path}")
async def serve_files(request: Request):
"""
Redirects the user to the root URL ("/") if they are not authenticated.
Parameters:
request (Request): The HTTP request object.
Returns:
RedirectResponse: A redirect response to the root URL ("/").
"""
user = request.session.get("discord_user")
if not user:
user = "Guest"
return RedirectResponse(url="/", status_code=303)
# session settings
app.add_middleware(SessionMiddleware, secret_key=os.getenv("secret_session"))
app.add_middleware(GZipMiddleware, minimum_size=1000)
app.add_middleware(TrustedHostMiddleware, allowed_hosts=["*"])
app.add_middleware(HTTPSRedirectMiddleware)
# start server
if __name__ == "__main__":
# uvicorn.run(app, host="127.0.0.1", port=8000, proxy_headers=True, forwarded_allow_ips="*")
uvicorn.run(
"server:app",
host="0.0.0.0",
port=8000,
proxy_headers=True,
forwarded_allow_ips="*",
workers=3,
)