Browse Source

100 件しか検索できなぃ問題修正

feature/query
みてるぞ 1 month ago
parent
commit
9261d0c68d
1 changed files with 37 additions and 24 deletions
  1. +37
    -24
      update_db.py

+ 37
- 24
update_db.py View File

@@ -1,16 +1,17 @@
from __future__ import annotations

import json import json
import os import os
import random import random
import string import string
import time import time
from dataclasses import dataclass from dataclasses import dataclass
from datetime import datetime
from datetime import datetime, timedelta
from typing import TypedDict, cast from typing import TypedDict, cast


import mysql.connector import mysql.connector
import requests import requests



DbNull = (None,) DbNull = (None,)
DbNullType = tuple[None] DbNullType = tuple[None]


@@ -69,7 +70,7 @@ def update_tables (
video = VideoDto (code = datum['contentId'], video = VideoDto (code = datum['contentId'],
title = datum['title'], title = datum['title'],
description = datum['description'], description = datum['description'],
uploaded_at = datum['startTime'])
uploaded_at = datetime.fromisoformat (datum['startTime']))
video_dao.upsert (video, False) video_dao.upsert (video, False)
if video.id_ is not None: if video.id_ is not None:
video_history = VideoHistoryDto (video_id = video.id_, video_history = VideoHistoryDto (video_id = video.id_,
@@ -160,30 +161,42 @@ def search_nico_by_tag (
def search_nico_by_tags ( def search_nico_by_tags (
tags: list[str], tags: list[str],
) -> list[VideoResult]: ) -> list[VideoResult]:
today = datetime.now ()

url = ('https://snapshot.search.nicovideo.jp' url = ('https://snapshot.search.nicovideo.jp'
+ '/api/v2/snapshot/video/contents/search') + '/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: class VideoDao:


Loading…
Cancel
Save