From 9261d0c68df8da1adb9d61d95b113588c2fce26a Mon Sep 17 00:00:00 2001 From: miteruzo Date: Mon, 7 Oct 2024 01:49:37 +0900 Subject: [PATCH] =?UTF-8?q?100=20=E4=BB=B6=E3=81=97=E3=81=8B=E6=A4=9C?= =?UTF-8?q?=E7=B4=A2=E3=81=A7=E3=81=8D=E3=81=AA=E3=81=83=E5=95=8F=E9=A1=8C?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- update_db.py | 61 +++++++++++++++++++++++++++++++--------------------- 1 file changed, 37 insertions(+), 24 deletions(-) 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: