-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.py
1363 lines (1094 loc) · 42.5 KB
/
app.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
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
If you plan on using the /user endpoint then you need to have "discord_token"
as environment variable.
"""
from os import getenv
token = getenv("discord_token")
# region [imports]
import anyio
import datetime
import json as jsonlib
import random
from akinator import Akinator
import aiohttp
from tetris.impl import custom
from tetris.impl import scorer
import tetris
from core.base_models import *
from core.game_functions import *
from core.image_gen import *
import uuid
import aiohttp
import fastapi
import secrets
import xmltodict
from aioify import aioify
from bson.objectid import ObjectId
from fastapi.responses import FileResponse, HTMLResponse, RedirectResponse, JSONResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from slowapi import Limiter, _rate_limit_exceeded_handler
from slowapi.errors import RateLimitExceeded
from slowapi.util import get_remote_address
from extras import (art_of_war, edn, emojify_string, morse_decode,
morse_encode, text_to_owo, user_info, premium_user_checker)
# mysql connection
limiter = Limiter(key_func=get_remote_address)
app = fastapi.FastAPI(redoc_url=None)
app.state.limiter = limiter
app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)
app.mount("/static", StaticFiles(directory="static"), name="static")
templates = Jinja2Templates(directory="templates")
# endregion
# region [html templates]
@app.get("/", response_class=HTMLResponse, include_in_schema=False)
@limiter.limit("100/minute")
async def homepage(request: fastapi.Request):
resp = edn(app)
return templates.TemplateResponse("index.html", {
"request": request,
"endpoints": resp
})
class Parser:
thumbnail:str
title:str
views:str
link:str
date:str
viewcount:str
description:str
sub:str
joined:str
totalviews:str
channelicon:str
channelbanner:str
name:str
def __init__(self, data, channeldata):
try:
self.thumbnail = data.split('{"thumbnails":[{"url":"')[1].split("?")[0]
except:
self.thumbnail = "none"
try:
self.title = data.split('"title":{"runs":[{"text":"')[1].split('"}]')[0]
except:
self.title = "none"
try:
self.views = data.split('"viewCountText":{"simpleText":"')[1].split('"},"')[0]
except:
self.views = "none"
try:
self.link = "https://www.youtube.com/watch?v=" + self.thumbnail.split("/")[-2]
except:
self.link = "none"
try:
self.date = data.split('"publishedTimeText":{"simpleText":"')[1].split('"},')[0]
except:
self.date = "none"
try:
self.viewcount = data.split('"viewCountText":{"simpleText":"')[1].split('"},')[0]
except:
self.viewcount = "none"
try:
self.description = channeldata.split('{"description":{"simpleText":"')[1].split('"},')[0]
except:
self.description = "none"
try:
self.joined = channeldata.split('"joinedDateText":{"runs":[{"text":"Joined "},{"text":"')[1].split('"}]')[0]
except:
self.joined = "none"
try:
self.totalviews = channeldata.split(',"viewCountText":{"simpleText":"')[1].split('"},')[0]
except:
self.totalviews = "none"
try:
self.channelicon = channeldata.split('"height":88},{"url":"')[1].split('"')[0]
except:
self.channelicon = "none"
try:
self.name = channeldata.split('{"channelMetadataRenderer":{"title":"')[1].split('","')[0]
except:
self.name = "none"
try:
self.sub = channeldata.split('"subscriberCountText":{"accessibility":{"accessibilityData":{"label":"')[1].split('"}},')[0]
except:
self.sub = "none"
try:
self.channelbanner = channeldata.split('"tvBanner":{"thumbnails":[{"url":"')[1].split('"')[0]
except:
self.channelbanner = "none"
@app.get("/youtube", tags=["json endpoints"], summary="youtube infos")
async def latest_video_yt(
channel_link="https://www.youtube.com/c/DailyDoseOfInternet"):
"""
[GET]
get the latest youtube video information
# parameters
channel_link: link to channel (eg. https://www.youtube.com/c/DailyDoseOfInternet)
# returns
```json
{
"thumbnail": "https://i.ytimg.com/vi/sIzBhYBsTdA/hqdefault.jpg",
"title": "Horse Doesn't Realize It Was Just Born",
"link": "https://www.youtube.com/watch?v=sIzBhYBsTdA",
"date": "16 hours ago"
}
```
"""
useragent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:104.0) Gecko/20100101 Firefox/104.0"
async with aiohttp.ClientSession(
headers={"User-Agent": useragent}) as session:
async with session.get(channel_link + "/videos") as resp:
data1 = await resp.text()
async with session.get(channel_link + "/about") as resp:
data2 = await resp.text()
a = Parser(data1, data2)
return {
"channelbanner":
a.channelbanner.replace("w320",
"w1280").replace("00000000ffffffff",
"32b75a57cd48a5a8"),
"channelicon":
a.channelicon,
"channelname":
a.name,
"joined":
a.joined,
"totalsubs":
a.sub,
"thumbnail":
a.thumbnail,
"title":
a.title,
"link":
a.link,
"date":
a.date,
"viewcount":
a.viewcount,
"description":
a.description
}
@app.get("/support", response_class=HTMLResponse, include_in_schema=False)
@limiter.limit("100/minute")
def homepage(request: fastapi.Request):
return RedirectResponse("https://www.buymeacoffee.com/resetxd")
# endregion
# region [game endpoints][tetris]
tetris_current_playing = {}
@app.post("/game/tetris/create",tags=["games endpoints"],summary="create tetris game")
async def tetris_create_game(request: fastapi.Request):
"""
[POST] method
create a new tetris game
endpoint - /game/tetris/create
# returns
```json
{
"game_id": game-token,
board: "ready to use board",
"next": "next piece"
}
```
"""
game_id = str(uuid.uuid4())
global tetris_current_playing
game = tetris.BaseGame(custom.CustomEngine,board_size=(15,10), scorer=scorer.GuidelineScorer)
game.engine.parts["gravity"] = PerMoveGravity
game.reset()
tetris_current_playing[str(game_id)] = {"game":game}
return {"game_id":str(game_id), "board":tetris_render_board(game), "next": tetris_next_piece(game)}
@app.post("/game/tetris/action",tags=["games endpoints"],summary="tetris game actions")
async def tetris_action(request: fastapi.Request, action:tetris_action):
"""
[POST] method
perform an action on a tetris game
endpoint - /game/tetris/action
# parameters
- **game_id** - game token
- **action** - action to perform (left, right, rotate, soft_drop, hard_drop, swap, hold)
# returns
```json
{
"win": true or false if the game still playing,
"board":"updated board",
"next":"next piece"
}
```
"""
global tetris_current_playing
if action.action == "left":
tetris_current_playing[action.game_id]["game"].left()
tetris_current_playing[action.game_id]["game"].tick()
return {"win":str(tetris_current_playing[action.game_id]["game"].playing) ,"board":tetris_render_board(tetris_current_playing[action.game_id]["game"]),"score":tetris_current_playing[action.game_id]["game"].score, "next": tetris_next_piece(tetris_current_playing[action.game_id]["game"])}
elif action.action == "right":
tetris_current_playing[action.game_id]["game"].right()
tetris_current_playing[action.game_id]["game"].tick()
return {"win":str(tetris_current_playing[action.game_id]["game"].playing),"board":tetris_render_board(tetris_current_playing[action.game_id]["game"]),"score":tetris_current_playing[action.game_id]["game"].score, "next": tetris_next_piece(tetris_current_playing[action.game_id]["game"])}
elif action.action == "rotate":
tetris_current_playing[action.game_id]["game"].rotate()
tetris_current_playing[action.game_id]["game"].tick()
return {"win":str(tetris_current_playing[action.game_id]["game"].playing),"board":tetris_render_board(tetris_current_playing[action.game_id]["game"]),"score":tetris_current_playing[action.game_id]["game"].score, "next": tetris_next_piece(tetris_current_playing[action.game_id]["game"])}
elif action.action == "soft_drop":
tetris_current_playing[action.game_id]["game"].soft_drop()
tetris_current_playing[action.game_id]["game"].tick()
return {"win":str(tetris_current_playing[action.game_id]["game"].playing),"board":tetris_render_board(tetris_current_playing[action.game_id]["game"]),"score":tetris_current_playing[action.game_id]["game"].score, "next": tetris_next_piece(tetris_current_playing[action.game_id]["game"])}
elif action.action == "hard_drop":
tetris_current_playing[action.game_id]["game"].hard_drop()
tetris_current_playing[action.game_id]["game"].tick()
return {"win":str(tetris_current_playing[action.game_id]["game"].playing),"board":tetris_render_board(tetris_current_playing[action.game_id]["game"]),"score":tetris_current_playing[action.game_id]["game"].score, "next": tetris_next_piece(tetris_current_playing[action.game_id]["game"])}
elif action.action == "swap":
tetris_current_playing[action.game_id]["game"].swap()
tetris_current_playing[action.game_id]["game"].tick()
return {"win":str(tetris_current_playing[action.game_id]["game"].playing),"board":tetris_render_board(tetris_current_playing[action.game_id]["game"]),"score":tetris_current_playing[action.game_id]["game"].score, "next": tetris_next_piece(tetris_current_playing[action.game_id]["game"])}
elif action.action == "hold":
tetris_current_playing[action.game_id]["game"].swap()
tetris_current_playing[action.game_id]["game"].tick()
return {"win":str(tetris_current_playing[action.game_id]["game"].playing),"board":tetris_render_board(tetris_current_playing[action.game_id]["game"]),"score":tetris_current_playing[action.game_id]["game"].score, "next": tetris_next_piece(tetris_current_playing[action.game_id]["game"])}
# endregion
# region [UnixTime]
@app.get("/unixtime/fromunix",tags=["unixtime"],summary="unixtime")
async def unixtime(timestamp):
"""
convert epos to simple string<br>/unixtime/fromunix?timestamp=1549892280<br>
get unixtime
endpoint - /unixtime/fromunix
# parameters
- **timestamp**: timestamp (1549892280)
# returns
```json
{
"Datetime": "2019-02-11 13:38:00"
}
```
"""
return {"Datetime": datetime.datetime.fromtimestamp(int(timestamp)).strftime('%Y-%m-%d %H:%M:%S')}
@app.get("/unixtime/tounix",tags=["unixtime"],summary="unixtime")
async def unixtime(date):
"""
convert epos to simple string<br>/unixtime/tounix?date=2019%2F02%2F11+13%3A38%3A00<br>
get unixtime
endpoint - /unixtime/tounix
# parameters
- **date**: date (2019/02/11 13:38:01)(2019-02-11 13:38:01)
# returns
```json
{
"UnixTimeStamp": "1549892280"
}
```
"""
if date == "now":
return {"UnixTimeStamp": int(datetime.datetime.now().timestamp())}
date = date.replace("-","/")
data = {"Datetime": str(int(datetime.datetime.strptime(date, '%Y/%m/%d %H:%M:%S').timestamp()))}
return data
# endregion
# region [internal endpoints]
def get_redoc_html(
*,
openapi_url: str,
title: str,
redoc_js_url:
str = "https://cdn.jsdelivr.net/npm/redoc@next/bundles/redoc.standalone.js",
redoc_favicon_url: str = "https://fastapi.tiangolo.com/img/favicon.png",
with_google_fonts: bool = True,
) -> HTMLResponse:
html = f"""
<!DOCTYPE html>
<html>
<head>
<title>{title}</title>
<!-- needed for adaptive design -->
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1">
"""
if with_google_fonts:
html += """
<link href="https://fonts.googleapis.com/css?family=Montserrat:300,400,700|Roboto:300,400,700" rel="stylesheet">
"""
html += f"""
<link rel="shortcut icon" href="{redoc_favicon_url}">
<!--
ReDoc doesn't change outer page styles
-->
<style>
body {{
margin: 0;
padding: 0;
}}
</style>
</head>
<body>
<redoc spec-url="{openapi_url}"></redoc>
<script src="{redoc_js_url}"> </script>
<script data-name="BMC-Widget" data-cfasync="false" src="https://cdnjs.buymeacoffee.com/1.0.0/widget.prod.min.js" data-id="resetxd" data-description="Support me on Buy me a coffee!" data-message="" data-color="#FFDD00" data-position="Right" data-x_margin="18" data-y_margin="18"></script>
</body>
</html>
"""
return HTMLResponse(html)
@app.get("/redoc", include_in_schema=False)
async def redoc_html():
return get_redoc_html(openapi_url=app.openapi_url,
title="resapi docs",
redoc_favicon_url="/static/avatar1.png")
@app.get("/internal/endpoints", include_in_schema=False)
@limiter.limit("5/minute")
async def end(request: fastapi.Request):
async with aiohttp.ClientSession() as client:
response = await client.get(f"{request.url._url[:22]}openapi.json")
json = await response.json()
json = json["paths"]
ret = ""
for x in json:
try:
resp = json[x]["get"]["description"].split("<br>")
ret += f"""
<div class="col-md-6" style="margin-bottom: 8px;">
<div class="card" style="background: rgba(255,255,255,0);border-radius: 22px;margin-top: 0px;border: 3px solid #8a00ff;box-shadow: 5px 5px 20px #8a00ff, -5px -5px 20px #8a00ff;">
<div class="card-body">
<h4 class="card-title" style="color: #8a00ff;">{x[1:]}</h4>
<p class="card-text" style="font-size: 23px;color: #bf00ff;">{resp[0]}</p><a class="card-link" href="{resp[1]}" style="color: #df00ff;">test endpoint</a>
</div>
</div>
</div>
"""
except:
pass
return ret
@app.get("/asset/{file_path}", include_in_schema=False)
async def asset(requests: fastapi.Request, file_path: str):
return FileResponse(f"./asset/{file_path}")
# endregion
# region [json endpoints]
@app.get("/art-of-war", tags=["json endpoints"], summary="art of war")
@limiter.limit("100/minute")
def artOfWar(request: fastapi.Request):
"""
sun tzu the art of war book related quotes<br>/art-of-war<br>
"""
return {"quote": random.choice(art_of_war)}
@app.get("/art-of-war.json", include_in_schema=False)
@limiter.limit("100/minute")
def artOfWarJson(request: fastapi.Request):
return {"quote": art_of_war}
@app.get("/emojify", tags=["json endpoints"], summary="emojify")
@limiter.limit("100/minute")
def emojify(request: fastapi.Request, text: str):
"""
emojify a string<br>/emojify?text=omg+reset+is+the+best<br>
creates a discord friendly string
# parameters
- **text**: text to emojify
# return
```json
{
"emojified": "text"
}
```
"""
return {"emojified": emojify_string(text)}
@app.get("/news", tags=["json endpoints"], summary="myanimelist news")
@limiter.limit("100/minute")
async def news(request: fastapi.Request):
"""
gives anime news in json format<br>/news<br>
# return
```json
[
{"guid":"", "title":"", "description":"", "media:thumbnail":"", "pubDate":"", "link":""},
{...},
{...}
]
```
"""
async with aiohttp.ClientSession() as session:
async with session.get("https://myanimelist.net/rss/news.xml") as repo:
resultJson = await repo.text()
returnJson = jsonlib.dumps(xmltodict.parse(resultJson))
return jsonlib.loads(returnJson)["rss"]["channel"]["item"]
@app.get("/user", tags=["json endpoints"], summary="user info")
@limiter.limit("100/minute")
async def user(request: fastapi.Request, userid: str):
"""
user info<br>/user?userid=424133185123647488<br>
# parameters
- **userid**: discord user ID (not user name)
# return
```json
{
"name":"reset",
"avatar":"https://cdn.discordapp.com/avatars/424133185123647488/26fc2ba791f1fcd2f678d5761e2cdab2.png?size=1024",
"banner":null,
"discriminator":"8278",
"id":"424133185123647488",
"banner_color":"#18191c",
"accent-color":1579292
}
```
"""
ret = await user_info(token, userid)
return ret
@app.get("/avatar/{user_id}.png", tags=["image endpoints"], summary="user avatar", response_class=RedirectResponse)
@limiter.limit("100/minute")
async def user(request: fastapi.Request, user_id: str):
"""
user avatar url<br>/avatar/424133185123647488.png<br>
# return
Image
"""
ret = await user_info(token, user_id)
return RedirectResponse(ret["avatar"])
@app.get("/strings", tags=["json endpoints"], summary="encoding info")
@limiter.limit("100/minute")
async def strings(request: fastapi.Request,
text: str,
from_: str = None,
to: str = None):
"""
strings info<br>/strings?text=reset+api+is+the+best&from_=text&to=owo<br>
# parameters
- **text**: text to encode
- **from_**: can be **text** or **morse**
- **to**: can be **text** or **owo** or **morse**
# return
```json
{
"text": "weset api is teh best"
}
```
"""
if from_ == "text" and to == "owo":
return {"text": text_to_owo(text)}
elif from_ == "text" and to == "morse":
return {"text": morse_encode(text)}
elif from_ == "morse" and to == "text":
return {"text": morse_decode(text)}
else:
return {"text": text}
# endregion
# region [games endpoint]
# region [game endpoint][connect4]
current_playing = {}
@app.post("/game/connect-4/create",
tags=["games endpoints"],
summary="create c4 game")
async def create_game(request: fastapi.Request, game: c4_creator):
"""
[POST] method
create a new connect 4 game
endpoint - /game/connect-4/create
# returns
```json
{
"game_id": game-token
}
```
"""
game_id = uuid.uuid4()
global current_playing
current_playing[str(game_id)] = {
"board": create_board(),
"player1": game.player1,
"player2": game.player2,
"empty": game.empty
}
return {"game_id": str(game_id)}
@app.get("/game/connect-4/get-all-games", include_in_schema=False)
async def allGames():
return {"data": current_playing}
@app.post("/game/connect-4/get-board",
tags=["games endpoints"],
summary="get c4 board")
async def get_board(request: fastapi.Request, game_id: c4_get_board):
"""
[POST] method
returns the current game board
endpoint - /game/connect-4/get-board
# parameters
- **game_id** - game token
# returns
```json
{
"board": "ready to use board"
}
```
"""
try:
global current_board
return {
"board":
current_board(current_playing[game_id.game_id]["board"],
current_playing[game_id.game_id]["player1"],
current_playing[game_id.game_id]["player2"],
current_playing[game_id.game_id]["empty"])
}
except:
return {"eror": "game not found"}
@app.post("/game/connect-4/drop",
tags=["games endpoints"],
summary="c4 game drop")
async def droper(request: fastapi.Request, drop: c4_drop):
"""
[POST] method
drop a piece at a given column
endpoint - /game/connect-4/drop
# parameters
- **game_id** - game token
- **column** - column to drop piece at
- **player** - player to drop piece as (1 or 2 only)
# returns
```json
{
"winner":"if game is over",
"board":"updated board"
}
```
"""
global current_playing
board = current_playing[drop.game_id]["board"]
col = int(drop.column)
if is_valid_location(board, col):
row = get_next_open_row(board, col)
drop_piece(current_playing[drop.game_id]["board"], row, col,
int(drop.player))
if winning_move(board, int(drop.player)):
return {
"winner":
drop.player,
"board":
current_board(current_playing[drop.game_id]["board"],
current_playing[drop.game_id]["player1"],
current_playing[drop.game_id]["player2"],
current_playing[drop.game_id]["empty"])
}
else:
return {
"winner":
"none",
"board":
current_board(current_playing[drop.game_id]["board"],
current_playing[drop.game_id]["player1"],
current_playing[drop.game_id]["player2"],
current_playing[drop.game_id]["empty"])
}
# endregion
# endregion
# region [games endpoints][akinator]
akinator_currently_playing = {}
@app.post("/game/akinator/create",
tags=["games endpoints"],
summary="create akinator game")
async def akinator_create_game(request: fastapi.Request, le: AkinatorCreate):
"""
[POST] method.
create your akinator game.
# parameter
- **language** - language of the game (en,en_animals,en_objects,ar,cn,de,de_animals,fr,jp,etc)
# return
```json
{
"game_id": game-token,
"question": "question asked by akinator",
"confidence": "confidence level of akinator"
}
```
"""
game_id = str(uuid.uuid4())
global akinator_currently_playing
akii = create_aki(le.language)
akinator_currently_playing[str(game_id)] = {"game": akii}
return {
"game_id": str(game_id),
"question": akii.question,
"confidence": akii.progression
}
@app.post("/game/akinator/close",
tags=["games endpoints"],
summary="akinator close")
async def akinator_game_close(request: fastapi.Request,
gam: AkinatorGameClose):
"""
[POST] method.
Close your game after completion.
# parameter
- **game_id** - game token
# return
```json
{
"message": "thank you for playing!"
}
```
"""
akii: Akinator = akinator_currently_playing[gam.game_id]["game"]
akii.close()
return {"message": "thanks for playing!"}
@app.post("/game/akinator/action",
tags=["games endpoints"],
summary="akinator action")
async def akinator_game_action(request: fastapi.Request,
gam: AkinatorGameAction):
"""
[POST] method.
interact with your akinator game.
# parameter
- **game_id** - game token
- **action** - action to do (**y**[yes], **n**[no] ,**idk**[i dont know],**p** [probably],**pn** [probably not])
# return
```json
{
"question": "question",
"step": "question number",
"confidence":"percentage of game completion",
"image": "if game complete",
"description": "if game complete",
"name": "if game complete",
"pseudo": "if game complete",
"ranking": "if game complete"
}
```
"""
if gam.action not in ["y", "n", "idk", "p", "pn"]:
return {"message": "wrong input"}
akii: akinator.Akinator = akinator_currently_playing[gam.game_id]["game"]
q = akii.answer(gam.action)
data = {"question": q, "step": akii.step, "confidence": akii.progression}
if akii.progression >= 80:
akii.win()
data["image"] = akii.first_guess["absolute_picture_path"]
data["description"] = akii.first_guess["description"]
data["name"] = akii.first_guess["name"]
data["pseudo"] = akii.first_guess["pseudo"]
data["ranking"] = akii.first_guess["ranking"]
return data
# endregion
# region [image endpoints]
@app.get("/welcome",
response_class=FileResponse,
tags=["image endpoints"],
summary="welcome card")
@limiter.limit("100/minute")
async def welcome_endpoint(request: fastapi.Request,
background,
message,
text,
avatar=None,
username=None,
userid=None):
"""
welcome card<br>/welcome?background=https://cdn.discordapp.com/attachments/907213435358547968/974989177642950656/unknown.png&avatar=https://cdn.discordapp.com/attachments/907213435358547968/974989329858461766/unknown.png&message=welcome%20to%20my%20server&username=komi%20san&text=1000%20members%20now%20OWO<br>
endpoint - /welcome
# parameters
- **background** - background image url
- **avatar** - avatar image url
- **message** - message to display
- **text** - message to display in 3rd line
- **username** - username to display
- **userid** - userid to display (optional, use this and dont give username and avatar)
# returns
<img src="/asset/welcome.bmp">
"""
if userid:
info = await user_info(token, userid)
avatar = info["avatar"]
username = info["name"]
a = await anyio.to_thread.run_sync(
welcomer, {
"background": background,
"avatar": avatar,
'username': username,
'message': message,
'text': text
})
return FileResponse(f"./trash/{a}.png")
@app.get("/level",
response_class=FileResponse,
tags=["image endpoints"],
summary="level card")
@limiter.limit("100/minute")
async def level_endpoint(request: fastapi.Request,
background,
level: int,
current_exp: int,
max_exp: int,
avatar=None,
username=None,
userid=None,
bar_color="red",
text_color="white"):
"""
level card<br>/level?background=https://media.discordapp.net/attachments/992703788865572874/993888491756859423/20220705_223748.jpg&avatar=https://cdn.discordapp.com/attachments/907213435358547968/992384949401432094/fddf655fb40613d2.png&username=Reset&discriminator=1000¤t_exp=276&max_exp=454&level=8&bar_color=%23FFA0D0&text_color=%23c2648d<br>
endpoint - /level
# parameters
- **background** - background image url
- **avatar** - avatar image url
- **username** - username to display
- **current_exp** - current exp
- **max_exp** - max exp
- **level** - level
- **bar_color** - bar color (color name or hex)
- **text_color** - text color (color name or hex)
# returns
<img src="https://cdn.discordapp.com/attachments/907213435358547968/994620579816681572/unknown.png">
"""
if userid:
info = await user_info(token, userid)
avatar = info["avatar"]
username = info["name"]
a = await anyio.to_thread.run_sync(
level_maker, {
'background': background,
'level': level,
'avatar': avatar,
'username': username,
'current_exp': current_exp,
'max_exp': max_exp,
'bar_color': bar_color,
'text_color': text_color
})
return FileResponse(f"./trash/{a}.png")
@app.get("/rip",
response_class=FileResponse,
tags=["image endpoints"],
summary="rip my g")
@limiter.limit("100/minute")
async def rip_endpoint(request: fastapi.Request, avatar):
"""
rip avatar<br>/rip?avatar=https://cdn.discordapp.com/attachments/907213435358547968/974990788972916766/unknown.png<br>
endpoint - /rip
# parameters
- **avatar** - avatar image url
# returns
<img src="/asset/rip.bmp">
"""
a = await anyio.to_thread.run_sync(rip_maker, avatar)
return FileResponse(f"./trash/{a}.png")
@app.get("/wap",
response_class=FileResponse,
tags=["image endpoints"],
summary="spongebob pray")
@limiter.limit("100/minute")
async def wap_endpoint(request: fastapi.Request, avatar):
"""
spongebob pray<br>/wap?avatar=https://cdn.discordapp.com/attachments/907213435358547968/974989177642950656/unknown.png<br>
endpoint - /wap
# parameters
- **avatar** - avatar image url
# returns
<img src="/asset/wap.bmp">
"""
a = await anyio.to_thread.run_sync(spongebobWAP, avatar)
return FileResponse(f"./trash/{a}.png")
@app.get("/throwthechild",
response_class=FileResponse,
tags=["image endpoints"],
summary="throw child")
@limiter.limit("100/minute")
async def throwchild_endpoint(request: fastapi.Request, avatar):
"""
THROW THE CHILD!<br>/throwthechild?avatar=https://cdn.discordapp.com/attachments/907213435358547968/974990788972916766/unknown.png<br>
endpoint - /throwthechild
# parameters
- **avatar** - avatar image url
# returns
<img src="/asset/throw.bmp">
"""
a = await anyio.to_thread.run_sync(throwthechild, avatar)
return FileResponse(f"./trash/{a}.png")
burn = aioify(obj=burning)
@app.get("/burn",
response_class=FileResponse,
tags=["image endpoints"],
summary="burn avatar")
@limiter.limit("100/minute")
async def burnchild(request: fastapi.Request, avatar):
"""
BURN THE CHILD!<br>/burn?avatar=https://cdn.discordapp.com/attachments/907213435358547968/974990788972916766/unknown.png<br>
endpoint - /burn
# parameters
- **avatar** - avatar image url