Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TimeoutErrorの送出を阻止し、async関数から通常関数へ乗り換える (Fix #6) #7

Merged
merged 1 commit into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions modules/discord.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@

from . import savedata

presence_rpc = pypresence.Presence(savedata.DISCORD_CLIENT_ID)
presence_rpc = pypresence.Presence(savedata.DISCORD_CLIENT_ID, connection_timeout=300, response_timeout=300)
presence_rpc.connect()

@functools.cache
def _get_video_with_cache(video_id: str):
return nicovideo.video.get_metadata(video_id)

async def update_video(videoid: str, time: datetime.timedelta, now: datetime.datetime):
def update_video(videoid: str, time: datetime.timedelta, now: datetime.datetime):
""" Rich Presenceの再生時更新 """
timesecs = 0
for record in savedata.records:
Expand All @@ -36,7 +36,7 @@ async def update_video(videoid: str, time: datetime.timedelta, now: datetime.dat
{"label": "ニコニコ動画トップ", "url": "https://www.nicovideo.jp/video_top"}
])

async def clear():
def clear():
""" Rich Presenceのクリア """
presence_rpc.clear(pid=os.getpid())

Expand Down
12 changes: 6 additions & 6 deletions modules/httpserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,31 @@

@app.post("/api/v1/update_watch", response_model=schemas.ServerResponse.ResponseMessage,
status_code=200, response_class=fastapi.responses.ORJSONResponse)
async def api_v1_update_watch(videodata: schemas.ClientRequest.APIv1UpdateWatch):
def api_v1_update_watch(videodata: schemas.ClientRequest.APIv1UpdateWatch):
""" /api/v1/update_watch: Update video."""
if savedata.now_playing:
await savedata.append_record(
savedata.append_record(
f"{savedata.now_playing[0]},"
f"{savedata.now_playing[1].isoformat()},"
f"{datetime.datetime.now().isoformat()}"
)
savedata.now_playing = (videodata.video_id, datetime.datetime.now())
savedata.now_playing_time_update = (videodata.time, datetime.datetime.now())
await discord.update_video(savedata.now_playing[0], savedata.now_playing_time_update[0],
discord.update_video(savedata.now_playing[0], savedata.now_playing_time_update[0],
savedata.now_playing_time_update[1])
return fastapi.responses.ORJSONResponse({"message": "ok"}, status_code=200)

@app.post("/api/v1/stop_watch", response_model=schemas.ServerResponse.ResponseMessage,
status_code=200, response_class=fastapi.responses.ORJSONResponse)
async def api_v1_stop_watch():
def api_v1_stop_watch():
""" /api/v1/stop_watch: Stop watching video. """
if savedata.now_playing:
await savedata.append_record(
savedata.append_record(
f"{savedata.now_playing[0]},"
f"{savedata.now_playing[1].isoformat()},"
f"{datetime.datetime.now().isoformat()}"
)
savedata.now_playing = None
savedata.now_playing_time_update = None
await discord.clear()
discord.clear()
return fastapi.responses.ORJSONResponse({"message": "ok"}, status_code=200)
2 changes: 1 addition & 1 deletion modules/savedata.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
for record in savedata.split("\n")[3:]:
records.append(record)

async def append_record(value: str):
def append_record(value: str):
""" recordsリストにvalueを追加した後、更にSAVEDATA_FILEに記録する。 """
with open(SAVEDATA_FILE, encoding="utf-8", mode="a") as h:
h.write("\n" + value)
Expand Down