# pylint: disable = missing-class-docstring # pylint: disable = missing-function-docstring """ 動画履歴の情報を取得し,JSON 形式で出力する. """ from __future__ import annotations import json import sys from datetime import date, datetime from typing import cast from db.config import DB from db.models import Video, VideoHistory DB def main ( views_counts: list[int], base_date: date, ) -> None: kiriban_list: list[tuple[int, str, str]] = [] latest_fetched_at = cast (date | None, (VideoHistory .where ('fetched_at', '<=', base_date) .max ('fetched_at'))) if latest_fetched_at is None: print ('[]') return for views_count in views_counts: targets = { vh.video.code for vh in ( VideoHistory .where ('fetched_at', latest_fetched_at) .where ('views_count', '>=', views_count) .get ()) } for code in targets: if code in [kiriban[1] for kiriban in kiriban_list]: continue previous_views_count: int | None = ( VideoHistory .where_has ('video', lambda q, code = code: q.where ('code', code)) .where ('fetched_at', '<', latest_fetched_at) .max ('views_count')) if previous_views_count is None: previous_views_count = 0 if previous_views_count >= views_count: continue kiriban_list.append ((views_count, code, (cast (Video, Video.where ('code', code).first ()) .uploaded_at))) print (json.dumps (kiriban_list, default = str)) if __name__ == '__main__': main (list (map (int, sys.argv[2:])), datetime.strptime (sys.argv[1], '%Y-%m-%d').date ())