ぼざろクリーチャーシリーズ DB 兼 API(自分用)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

68 lines
2.0 KiB

  1. # pylint: disable = missing-class-docstring
  2. # pylint: disable = missing-function-docstring
  3. """
  4. 動画履歴の情報を取得し,JSON 形式で出力する.
  5. """
  6. from __future__ import annotations
  7. import json
  8. import os
  9. import sys
  10. from datetime import date, datetime
  11. from typing import TypedDict, cast
  12. from eloquent import DatabaseManager, Model
  13. from db.config import DB
  14. from db.models import Video, VideoHistory
  15. DB
  16. def main (
  17. views_counts: list[int],
  18. base_date: date,
  19. ) -> None:
  20. if not base_date:
  21. base_date = datetime.now ().date ()
  22. kiriban_list: list[tuple[int, str, str]] = []
  23. latest_fetched_at = cast (date, (VideoHistory
  24. .where ('fetched_at', '<=', base_date)
  25. .max ('fetched_at')))
  26. for views_count in views_counts:
  27. targets = { vh.video.code for vh in (
  28. VideoHistory
  29. .where ('fetched_at', latest_fetched_at)
  30. .where ('views_count', '>=', views_count)
  31. .get ()) }
  32. for code in targets:
  33. if code in [kiriban[1] for kiriban in kiriban_list]:
  34. continue
  35. previous_views_count: int | None = (
  36. VideoHistory
  37. .where_has ('video', lambda q, code = code: q.where ('code', code))
  38. .where ('fetched_at', '<', latest_fetched_at)
  39. .max ('views_count'))
  40. if previous_views_count is None:
  41. previous_views_count = 0
  42. if previous_views_count >= views_count:
  43. continue
  44. kiriban_list.append ((views_count, code,
  45. (cast (Video, Video.where ('code', code).first ())
  46. .uploaded_at)))
  47. print (json.dumps (kiriban_list, default = str))
  48. if __name__ == '__main__':
  49. main (map (int, sys.argv[2:]),
  50. datetime.strptime (sys.argv[1], '%Y-%m-%d').date ())