diff --git a/update_db.py b/update_db.py index 7bd57e9..cf96f56 100644 --- a/update_db.py +++ b/update_db.py @@ -1,16 +1,17 @@ +from __future__ import annotations + import json import os import random import string import time from dataclasses import dataclass -from datetime import datetime +from datetime import datetime, timedelta from typing import TypedDict, cast import mysql.connector import requests - DbNull = (None,) DbNullType = tuple[None] @@ -69,7 +70,7 @@ def update_tables ( video = VideoDto (code = datum['contentId'], title = datum['title'], description = datum['description'], - uploaded_at = datum['startTime']) + uploaded_at = datetime.fromisoformat (datum['startTime'])) video_dao.upsert (video, False) if video.id_ is not None: video_history = VideoHistoryDto (video_id = video.id_, @@ -160,30 +161,42 @@ def search_nico_by_tag ( def search_nico_by_tags ( tags: list[str], ) -> list[VideoResult]: + today = datetime.now () + url = ('https://snapshot.search.nicovideo.jp' + '/api/v2/snapshot/video/contents/search') - # TODO: 年月日の設定ができてゐなぃのと,100 件までしか取得できなぃので何とかすること - - query_filter = json.dumps ({ 'type': 'or', - 'filters': [ - { 'type': 'range', - 'field': 'startTime', - 'from': f"{year}-{start}T00:00:00+09:00", - 'to': f"{year}-{end}T23:59:59+09:00", - 'include_lower': True }] }) - - params: VideoSearchParam - params = { 'q': ' OR '.join (tags), - 'targets': 'tagsExact', - '_sort': '-viewCounter', - 'fields': 'contentId,title,tags,description,viewCounter,startTime', - '_limit': 100, - 'jsonFilter': query_filter } - - res = requests.get (url, params = cast (dict[str, int | str], params), timeout = 60).json () - - return res['data'] + result_data: list[VideoResult] = [] + to = datetime (2022, 12, 3) + while to <= today: + time.sleep (1.2) + until = to + timedelta (days = 14) + query_filter = json.dumps ({ 'type': 'or', + 'filters': [ + { 'type': 'range', + 'field': 'startTime', + 'from': '%04d-%02d-%02dT00:00:00+09:00' % (to.year, to.month, to.day), + 'to': '%04d-%02d-%02dT23:59:59+09:00' % (until.year, until.month, until.day), + 'include_lower': True }] }) + params: VideoSearchParam = { 'q': ' OR '.join (tags), + 'targets': 'tagsExact', + '_sort': '-viewCounter', + 'fields': ('contentId,' + 'title,' + 'tags,' + 'description,' + 'viewCounter,' + 'startTime'), + '_limit': 100, + 'jsonFilter': query_filter } + res = requests.get (url, params = cast (dict[str, int | str], params), timeout = 60).json () + try: + result_data += res['data'] + except KeyError: + pass + to = until + timedelta (days = 1) + + return result_data class VideoDao: