Skip to content

Commit

Permalink
python: #292
Browse files Browse the repository at this point in the history
  • Loading branch information
misodengaku committed Nov 22, 2023
1 parent f7d2553 commit 16e2c7c
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions webapp/python/app.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python

import hashlib
import io
import json
import os
Expand Down Expand Up @@ -47,6 +48,8 @@ class Settings(object):
DEFAULT_USER_ID_KEY = "USERID"
DEFAULT_USER_NAME_KEY = "USERNAME"

FALLBACK_IMAGE_PATH = "../img/NoImage.jpg"


app = Flask(__name__)

Expand Down Expand Up @@ -1281,7 +1284,7 @@ def get_icon_handler(username: str) -> Response:

if not image:
return send_file(
"../img/NoImage.jpg", mimetype="image/jpeg", as_attachment=True
Settings.FALLBACK_IMAGE_PATH, mimetype="image/jpeg", as_attachment=True
)
return send_file(
io.BytesIO(image["image"]),
Expand Down Expand Up @@ -1372,11 +1375,12 @@ def get_livestream_statistics_handler(livestream_id: int) -> tuple[dict[str, Any

score = reactions + total_tips
ranking.append(
{
"livestream_id": livestream["id"],
"title": livestream["title"],
"score": score,
}
asdict(
models.LiveStreamRankingEntry(
livestream_id=livestream["id"],
score=score,
)
)
)
ranking = sorted(ranking, key=lambda x: x["score"])

Expand Down Expand Up @@ -1636,30 +1640,26 @@ def fill_user_response(
raise HttpException("not found", NOT_FOUND)
theme_model = models.ThemeModel(**row)

sql = "SELECT image FROM icons WHERE user_id = %s"
c.execute(sql, [user_model.id])
image_row = c.fetchone()
if not image_row:
image = open(Settings.FALLBACK_IMAGE_PATH, "rb").read()
else:
image = io.BytesIO(image_row["image"]).getvalue()
icon_hash = hashlib.sha256(image).hexdigest()

user = models.User(
id=user_model.id,
name=user_model.name,
display_name=user_model.display_name,
description=user_model.description,
theme=models.Theme(id=theme_model.id, dark_mode=theme_model.dark_mode),
icon_hash=icon_hash,
)

return user

# if not hasattr(g, "db"):
# app.logger.info("create new DB connection")

# g.db = pymysql.connect(
# host=Settings.DB_HOST,
# port=Settings.DB_PORT,
# user=Settings.DB_USER,
# password=Settings.DB_PASSWORD,
# db=Settings.DB_NAME,
# charset="utf8mb4",
# cursorclass=DictCursor,
# autocommit=False,
# )


# Content-Type付けてこないクライアントからのJSONリクエストボディを
# いい感じに受け取れるようにするやつ
Expand Down

0 comments on commit 16e2c7c

Please sign in to comment.