ぼざろクリーチャーシリーズ 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.
 
 
 

66 lines
1.9 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 sys
  9. from datetime import date, datetime
  10. from typing import cast
  11. from db.config import DB
  12. from db.models import Video, VideoHistory
  13. DB
  14. def main (
  15. views_counts: list[int],
  16. base_date: date,
  17. ) -> None:
  18. kiriban_list: list[tuple[int, str, str]] = []
  19. latest_fetched_at = cast (date | None,
  20. (VideoHistory
  21. .where ('fetched_at', '<=', base_date)
  22. .max ('fetched_at')))
  23. if latest_fetched_at is None:
  24. print ('[]')
  25. return
  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 (list (map (int, sys.argv[2:])),
  50. datetime.strptime (sys.argv[1], '%Y-%m-%d').date ())