Change formatting
This commit is contained in:
parent
0b68a1c4b4
commit
4487003558
3 changed files with 81 additions and 56 deletions
|
@ -1,2 +1 @@
|
||||||
__version__ = '0.2.0'
|
__version__ = "0.2.0"
|
||||||
|
|
||||||
|
|
|
@ -10,43 +10,52 @@ import os
|
||||||
import re
|
import re
|
||||||
from . import utils
|
from . import utils
|
||||||
from . import __version__
|
from . import __version__
|
||||||
|
|
||||||
basedir = os.path.dirname(__file__)
|
basedir = os.path.dirname(__file__)
|
||||||
|
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
app.mount("/static", StaticFiles(directory=basedir + "/static"), name="static")
|
app.mount("/static", StaticFiles(directory=basedir + "/static"), name="static")
|
||||||
|
|
||||||
template_env = jinja2.Environment(
|
template_env = jinja2.Environment(
|
||||||
loader=jinja2.PackageLoader("iwm_browser", "templates"),
|
loader=jinja2.PackageLoader("iwm_browser", "templates"), auto_reload=True
|
||||||
auto_reload=True
|
|
||||||
)
|
)
|
||||||
|
|
||||||
template_env.globals['convert_times'] = utils.convert_times
|
error_template = template_env.get_template("error.html")
|
||||||
template_env.globals['generate_thumbnail_url'] = utils.generate_thumbnail_url
|
|
||||||
template_env.globals['__version__'] = __version__
|
template_env.globals["convert_times"] = utils.convert_times
|
||||||
|
template_env.globals["generate_thumbnail_url"] = utils.generate_thumbnail_url
|
||||||
|
template_env.globals["__version__"] = __version__
|
||||||
|
|
||||||
BASE_URL = "http://make.fangam.es"
|
BASE_URL = "http://make.fangam.es"
|
||||||
THUMB_URL = "https://images.make.fangam.es"
|
THUMB_URL = "https://images.make.fangam.es"
|
||||||
utils.global_imgproxy_url = "http://127.0.0.1:8080" # Set it to None to disable the proxy
|
utils.global_imgproxy_url = (
|
||||||
utils.global_imgproxy_params = {"extension": "webp", "advanced": ["q:50"]} # Set it to None to disable the proxy params
|
"http://127.0.0.1:8080" # Set it to None to disable the proxy
|
||||||
|
)
|
||||||
|
utils.global_imgproxy_params = {
|
||||||
|
"extension": "webp",
|
||||||
|
"advanced": ["q:50"],
|
||||||
|
} # Set it to None to disable the proxy params
|
||||||
|
|
||||||
# Matches level code.
|
# Matches level code.
|
||||||
# \S[A-Z0-9]{4}\-[A-Z0-9]{4} = With dash, no whitespace
|
# \S[A-Z0-9]{4}\-[A-Z0-9]{4} = With dash, no whitespace
|
||||||
# \S[A-Z0-9]{8} = without dash, no whitespace
|
# \S[A-Z0-9]{8} = without dash, no whitespace
|
||||||
level_code_regex = re.compile("[A-Z0-9]{4}\-[A-Z0-9]{4}|[A-Z0-9]{8}")
|
level_code_regex = re.compile("[A-Z0-9]{4}\-[A-Z0-9]{4}|[A-Z0-9]{8}")
|
||||||
|
|
||||||
error_template = template_env.get_template("error.html")
|
|
||||||
@app.get("/", response_class=HTMLResponse)
|
@app.get("/", response_class=HTMLResponse)
|
||||||
async def root():
|
async def root():
|
||||||
template = template_env.get_template("home.html")
|
template = template_env.get_template("home.html")
|
||||||
return template.render()
|
return template.render()
|
||||||
|
|
||||||
|
|
||||||
@app.get("/search", response_class=HTMLResponse)
|
@app.get("/search", response_class=HTMLResponse)
|
||||||
async def search(
|
async def search(
|
||||||
q: Union[str, None] = None,
|
q: Union[str, None] = None,
|
||||||
p: int = 0,
|
p: int = 0,
|
||||||
sort: str = "average_rating",
|
sort: str = "average_rating",
|
||||||
dir: str = "desc",
|
dir: str = "desc",
|
||||||
date: int = -1
|
date: int = -1,
|
||||||
):
|
):
|
||||||
template = template_env.get_template("search.html")
|
template = template_env.get_template("search.html")
|
||||||
limit = 10
|
limit = 10
|
||||||
if p is None:
|
if p is None:
|
||||||
|
@ -62,9 +71,13 @@ async def search(
|
||||||
searchValue = q
|
searchValue = q
|
||||||
entryNumber = -1
|
entryNumber = -1
|
||||||
try:
|
try:
|
||||||
rq = httpx.get(BASE_URL + "/api/v1/map", params={
|
rq = httpx.get(
|
||||||
|
BASE_URL + "/api/v1/map",
|
||||||
|
params={
|
||||||
"code": levelCode,
|
"code": levelCode,
|
||||||
}, timeout=10)
|
},
|
||||||
|
timeout=10,
|
||||||
|
)
|
||||||
searchResults = rq.json()
|
searchResults = rq.json()
|
||||||
print(rq.url, p * limit)
|
print(rq.url, p * limit)
|
||||||
except httpx.ReadTimeout:
|
except httpx.ReadTimeout:
|
||||||
|
@ -77,7 +90,7 @@ async def search(
|
||||||
params = re.findall("[a-z\-\_]*:[a-zA-Z0-9._\-]*", q)
|
params = re.findall("[a-z\-\_]*:[a-zA-Z0-9._\-]*", q)
|
||||||
search = re.sub("\S[a-z\-\_]*:[a-zA-Z0-9._\-]*", "", q).lstrip().rstrip()
|
search = re.sub("\S[a-z\-\_]*:[a-zA-Z0-9._\-]*", "", q).lstrip().rstrip()
|
||||||
for i in params:
|
for i in params:
|
||||||
split = i.split(':')
|
split = i.split(":")
|
||||||
if split[0] == "author":
|
if split[0] == "author":
|
||||||
author = {"author": split[1]}
|
author = {"author": split[1]}
|
||||||
|
|
||||||
|
@ -95,30 +108,38 @@ async def search(
|
||||||
|
|
||||||
searchValue = q
|
searchValue = q
|
||||||
try:
|
try:
|
||||||
rq = httpx.get(BASE_URL + "/api/v1/map", params={
|
rq = httpx.get(
|
||||||
|
BASE_URL + "/api/v1/map",
|
||||||
|
params={
|
||||||
"start": p * limit,
|
"start": p * limit,
|
||||||
"limit": limit,
|
"limit": limit,
|
||||||
"min_diff": min_diff,
|
"min_diff": min_diff,
|
||||||
"max_diff": max_diff,
|
"max_diff": max_diff,
|
||||||
"order": json.dumps([order]),
|
"order": json.dumps([order]),
|
||||||
"name": search,
|
"name": search,
|
||||||
**last_x_hours, **author
|
**last_x_hours,
|
||||||
}, timeout=10)
|
**author,
|
||||||
|
},
|
||||||
|
timeout=10,
|
||||||
|
)
|
||||||
if rq.status_code == 503:
|
if rq.status_code == 503:
|
||||||
return error_template.render(reason="Server is unavailable right now.")
|
return error_template.render(
|
||||||
|
reason="Server is unavailable right now."
|
||||||
|
)
|
||||||
searchResults = rq.json()
|
searchResults = rq.json()
|
||||||
print(rq.url, p * limit)
|
print(rq.url, p * limit)
|
||||||
except httpx.ReadTimeout:
|
except httpx.ReadTimeout:
|
||||||
return error_template.render(reason="Server timed out")
|
return error_template.render(reason="Server timed out")
|
||||||
except json.decoder.JSONDecodeError:
|
except json.decoder.JSONDecodeError:
|
||||||
return error_template.render(reason="Failed to parse server response.", details=rq.text)
|
return error_template.render(
|
||||||
|
reason="Failed to parse server response.", details=rq.text
|
||||||
|
)
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
return error_template.render(reason="Uncaught exception", details=exc)
|
return error_template.render(reason="Uncaught exception", details=exc)
|
||||||
entryNumber=len(searchResults)
|
entryNumber = len(searchResults)
|
||||||
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
searchValue = ''
|
searchValue = ""
|
||||||
entryNumber = None
|
entryNumber = None
|
||||||
|
|
||||||
return template.render(
|
return template.render(
|
||||||
|
@ -126,9 +147,10 @@ async def search(
|
||||||
THUMB_URL=THUMB_URL,
|
THUMB_URL=THUMB_URL,
|
||||||
entryLimit=limit,
|
entryLimit=limit,
|
||||||
entryNumber=entryNumber,
|
entryNumber=entryNumber,
|
||||||
QueryValues=QueryValues
|
QueryValues=QueryValues,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@app.get("/level/{level_id}", response_class=HTMLResponse)
|
@app.get("/level/{level_id}", response_class=HTMLResponse)
|
||||||
async def showLevel(level_id: int):
|
async def showLevel(level_id: int):
|
||||||
template = template_env.get_template("levelInfo.html")
|
template = template_env.get_template("levelInfo.html")
|
||||||
|
@ -142,17 +164,18 @@ async def showLevel(level_id: int):
|
||||||
except httpx.ReadTimeout:
|
except httpx.ReadTimeout:
|
||||||
return error_template.render(reason="Server timed out")
|
return error_template.render(reason="Server timed out")
|
||||||
except json.decoder.JSONDecodeError:
|
except json.decoder.JSONDecodeError:
|
||||||
return error_template.render(reason="Failed to parse server response.", details=rq.text)
|
return error_template.render(
|
||||||
|
reason="Failed to parse server response.", details=rq.text
|
||||||
|
)
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
return error_template.render(reason="Uncaught exception", details=exc)
|
return error_template.render(reason="Uncaught exception", details=exc)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return template.render(
|
return template.render(
|
||||||
map=searchResults,
|
map=searchResults,
|
||||||
THUMB_URL=THUMB_URL,
|
THUMB_URL=THUMB_URL,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@app.get("/user/{user_id}", response_class=HTMLResponse)
|
@app.get("/user/{user_id}", response_class=HTMLResponse)
|
||||||
async def showUser(user_id: int):
|
async def showUser(user_id: int):
|
||||||
template = template_env.get_template("userInfo.html")
|
template = template_env.get_template("userInfo.html")
|
||||||
|
@ -166,22 +189,19 @@ async def showUser(user_id: int):
|
||||||
except httpx.ReadTimeout:
|
except httpx.ReadTimeout:
|
||||||
return error_template.render(reason="Server timed out")
|
return error_template.render(reason="Server timed out")
|
||||||
except json.decoder.JSONDecodeError:
|
except json.decoder.JSONDecodeError:
|
||||||
return error_template.render(reason="Failed to parse server response.", details=rq.text)
|
return error_template.render(
|
||||||
|
reason="Failed to parse server response.", details=rq.text
|
||||||
|
)
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
print(exc)
|
print(exc)
|
||||||
return error_template.render(reason="Uncaught exception", details=exc)
|
return error_template.render(reason="Uncaught exception", details=exc)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return template.render(
|
return template.render(
|
||||||
user=searchResults,
|
user=searchResults,
|
||||||
THUMB_URL=THUMB_URL,
|
THUMB_URL=THUMB_URL,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def start():
|
def start():
|
||||||
"""Launched with `poetry run start` at root level"""
|
"""Launched with `poetry run start` at root level"""
|
||||||
uvicorn.run("iwm_browser.main:app", host="0.0.0.0", port=7775, reload=True)
|
uvicorn.run("iwm_browser.main:app", host="0.0.0.0", port=7775, reload=True)
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
from imgproxy import ImgProxy
|
from imgproxy import ImgProxy
|
||||||
|
|
||||||
|
|
||||||
def convert_times(time):
|
def convert_times(time):
|
||||||
# Not sure if it's the right way to do that, but it seems to work.
|
# Not sure if it's the right way to do that, but it seems to work.
|
||||||
time_seconds = ((time % 60000) / 100) * 2
|
time_seconds = ((time % 60000) / 100) * 2
|
||||||
|
@ -12,12 +13,17 @@ def convert_times(time):
|
||||||
|
|
||||||
seconds = "{0:,.2f}".format(seconds).zfill(5)
|
seconds = "{0:,.2f}".format(seconds).zfill(5)
|
||||||
|
|
||||||
return (hours,minutes,seconds)
|
return (hours, minutes, seconds)
|
||||||
|
|
||||||
|
|
||||||
# Specify default imgproxy instance.
|
# Specify default imgproxy instance.
|
||||||
global_imgproxy_url = None
|
global_imgproxy_url = None
|
||||||
global_imgproxy_params = None
|
global_imgproxy_params = None
|
||||||
def generate_thumbnail_url(level_id, thumbnail_url, imgproxy_url=None, imgproxy_params={}):
|
|
||||||
|
|
||||||
|
def generate_thumbnail_url(
|
||||||
|
level_id, thumbnail_url, imgproxy_url=None, imgproxy_params={}
|
||||||
|
):
|
||||||
"""Generates thumbnail url."""
|
"""Generates thumbnail url."""
|
||||||
global global_imgproxy_url
|
global global_imgproxy_url
|
||||||
global global_imgproxy_params
|
global global_imgproxy_params
|
||||||
|
|
Loading…
Reference in a new issue