-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
229 lines (211 loc) · 7.6 KB
/
api.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
from fastapi import FastAPI, Request, Form
from fastapi.responses import PlainTextResponse
from fastapi.responses import FileResponse, RedirectResponse, HTMLResponse
from fastapi.staticfiles import StaticFiles
from playhouse.postgres_ext import *
from typing import Annotated
app = FastAPI()
import os
import json
from playhouse.shortcuts import model_to_dict, dict_to_model
import random
import time
import requests
from pydantic import BaseModel as otherBaseModel
app.mount("/static", StaticFiles(directory="img"), name="static")
db = PostgresqlExtDatabase('files', user=os.environ['DB_USER'], password=os.environ['DB_PASS'], host=os.environ['DB_HOST'], port=os.environ['DB_PORT'])
class BaseModel(Model):
class Meta:
database = db
class Image(BaseModel):
guid = BigIntegerField()
tags = JSONField()
filename = TextField()
nsfw = BooleanField(null=True)
nsfl = BooleanField(null=True)
political = BooleanField(null=True)
lgbt = BooleanField(null=True)
unsafe = BooleanField(null=True)
type = TextField()
category = TextField(null=True)
db.connect()
db.create_tables([Image], safe=True)
@app.get("/search")
async def search(tags: str="", nsfw: bool=False, unsafe: bool=False, nsfl:bool=False, category: str="meme,image,vex,art,other", lgbt: bool=True, political: bool=True,pos: int=1):
split_tags = tags.lower().split(",")
_nsfw = [False]
_nsfl = [False]
_unsafe = [False]
_lgbt = [False]
_politcal = [False]
split_category = category.split(",")
if nsfw:
_nsfw.append(True)
if nsfl:
_nsfl.append(True)
if unsafe:
_unsafe.append(True)
if lgbt:
_lgbt.append(True)
if political:
_politcal.append(True)
files = Image.select().where(Image.nsfw.in_(_nsfw) & Image.nsfl.in_(_nsfl) & Image.unsafe.in_(_unsafe) & Image.lgbt.in_(_lgbt) & Image.political.in_(_politcal) & Image.category.in_(split_category))
filtered = list()
for file in files:
matches = 0
for i in file.tags:
matches += split_tags.count(i)
if matches > 0:
filtered.append([file.filename, matches])
if len(filtered) < 1:
return {'error': 'no matching files'}
ranked = sorted(filtered, key=lambda x:x[1], reverse=True)
if pos > 0:
pos -= 1
else:
pos = 0
if pos+1 > len(filtered):
return {'error': 'no matching files'}
response_file = ranked[pos][0]
return FileResponse(f"img/{response_file}")
@app.get("/random")
async def random_img(tags: str="", nsfw: bool=False, unsafe: bool=False, nsfl:bool=False, category: str="meme,image,vex,art,other", lgbt: bool=True, political: bool=True, onlynsfw: bool=False):
split_tags = tags.lower().split(",")
if onlynsfw == False:
_nsfw = [False]
else:
_nsfw = []
_nsfl = [False]
_unsafe = [False]
_lgbt = [False]
_politcal = [False]
split_category = category.split(",")
if nsfw:
_nsfw.append(True)
if nsfl:
_nsfl.append(True)
if unsafe:
_unsafe.append(True)
if lgbt:
_lgbt.append(True)
if political:
_politcal.append(True)
files = Image.select().where(Image.nsfw.in_(_nsfw) & Image.nsfl.in_(_nsfl) & Image.unsafe.in_(_unsafe) & Image.lgbt.in_(_lgbt) & Image.political.in_(_politcal) & Image.category.in_(split_category))
filtered = list()
for file in files:
matches = 0
for i in file.tags:
matches += split_tags.count(i)
if matches > 0 or tags=="":
filtered.append([file.filename, matches])
if len(filtered) < 1:
return {'error': 'no matching files'}
random.shuffle(filtered)
response_file = filtered[0][0]
return FileResponse(f"img/{response_file}")
@app.get("/json")
async def get_json(nsfw: bool=False, unsafe: bool=False, nsfl:bool=False, category: str="meme,image,vex,art,other", lgbt: bool=True, political: bool=True, onlynsfw: bool=False):
if onlynsfw == False:
_nsfw = [False]
else:
_nsfw = []
_nsfl = [False]
_unsafe = [False]
_lgbt = [False]
_politcal = [False]
split_category = category.split(",")
if nsfw:
_nsfw.append(True)
if nsfl:
_nsfl.append(True)
if unsafe:
_unsafe.append(True)
if lgbt:
_lgbt.append(True)
if political:
_politcal.append(True)
files = Image.select().where(Image.nsfw.in_(_nsfw) & Image.nsfl.in_(_nsfl) & Image.unsafe.in_(_unsafe) & Image.lgbt.in_(_lgbt) & Image.political.in_(_politcal) & Image.category.in_(split_category))
json_data = []
for file in files:
json_data.append(model_to_dict(file))
return json_data
@app.get("/stats")
async def stats(nsfw: bool=False, unsafe: bool=False, nsfl:bool=False, category: str="meme,image,vex,art,other", lgbt: bool=True, political: bool=True, onlynsfw: bool=False):
if onlynsfw == False:
_nsfw = [False]
else:
_nsfw = []
_nsfl = [False]
_unsafe = [False]
_lgbt = [False]
_politcal = [False]
split_category = category.split(",")
if nsfw:
_nsfw.append(True)
if nsfl:
_nsfl.append(True)
if unsafe:
_unsafe.append(True)
if lgbt:
_lgbt.append(True)
if political:
_politcal.append(True)
files = Image.select().where(Image.nsfw.in_(_nsfw) & Image.nsfl.in_(_nsfl) & Image.unsafe.in_(_unsafe) & Image.lgbt.in_(_lgbt) & Image.political.in_(_politcal) & Image.category.in_(split_category))
return {'matching query': files.count()}
class Login(otherBaseModel):
auth_bearer: str
auth_token: str
device_name: str
device_uuid: str
email: str
rdm_endpoint: str
@app.get("/atlas/agent", response_class=PlainTextResponse)
def proxy_agent(request: Request):
app_ver = request.headers.get('App-Version-Code')
if app_ver is None:
app_ver = "22071801"
proxy_headers = {
'User-Agent': 'khttp/1.0.0-SNAPSHOT',
'App-Version-Code': app_ver
}
r = requests.get("https://discovery.pokemod.dev/atlas/agent", headers=proxy_headers)
response_text = r.text
print(app_ver)
print(r.raw)
return response_text
device_tokens = dict()
@app.post("/atlas/auth/device/login")
def fake_login(login: Login):
snowflake = str(int(time.time()))+str(random.randint(11111,99999))
device_tokens[snowflake] = {
"auth_bearer": login.auth_bearer,
"auth_token": login.auth_token ,
"device_name": login.device_name,
"device_uuid": login.device_uuid ,
"email": login.email,
"rdm_endpoint": login.rdm_endpoint,
"snowflake": snowflake
}
return {
"auth_token": snowflake,
"refresh_token": snowflake,
"config": {
"auth_bearer": login.auth_bearer,
"device_name": login.device_name,
"rdm_endpoint": login.rdm_endpoint
}
}
@app.get("/pokemod_tutorials/ios")
def ios():
return RedirectResponse("https://www.youtube.com/watch?v=dQw4w9WgXcQ")
@app.get("/products/pride-collection-martingale-necklace")
def necklace():
return FileResponse("img/necklace.html")
# with open("img/necklace.html") as f:
# content = f.read()
# return HTMLResponse(content=content, status_code=200)
@app.post("/trafficlight")
def trafficlight(data: Annotated[str, Form()]):
data_json = json.loads(data)
r = requests.post("http://5.78.81.243:3335", json=data_json)
print(r.status_code, r.text)