129 行
3.3 KiB
Python
129 行
3.3 KiB
Python
"""
|
|
ニコニコのニジカ動画取得モヂュール
|
|
"""
|
|
|
|
from typing import TypedDict
|
|
|
|
import requests
|
|
from bs4 import BeautifulSoup
|
|
from requests.exceptions import Timeout
|
|
|
|
|
|
class VideoInfo (TypedDict):
|
|
contentId: str
|
|
title: str
|
|
tags: list[str]
|
|
description: str
|
|
|
|
|
|
def get_nico_deerjika (
|
|
) -> list:
|
|
URL = ('https://snapshot.search.nicovideo.jp/api/v2/snapshot/video'
|
|
'/contents/search')
|
|
|
|
now = datetime.now ()
|
|
base = now - timedelta (hours = 24)
|
|
|
|
params = { 'q': '伊地知ニジカ OR ぼざろクリーチャーシリーズ',
|
|
'targets': 'tags',
|
|
'_sort': '-startTime',
|
|
'fields': 'contentId,title,description,tags,startTime',
|
|
'_limit': 20,
|
|
'jsonFilter': json.dumps ({ 'type': 'or',
|
|
'filters': [{
|
|
'type': 'range',
|
|
'field': 'startTime',
|
|
'from': ('%04d-%02d-%02dT05:00:00+09:00'
|
|
% (base.year, base.month, base.day,
|
|
base.hour, base.minute)),
|
|
'to': ('%04d-%02d-%02dT05:00:00+09:00'
|
|
% (now.year, now.month, now.day)),
|
|
'include_lower': True }] }) }
|
|
|
|
try:
|
|
res = requests.get (URL, params = params, timeout = 60).json ()
|
|
except Timeout:
|
|
return []
|
|
|
|
data = []
|
|
for datum in res['data']:
|
|
datum['tags'] = datum['tags'].split ()
|
|
data.append (datum)
|
|
|
|
return data
|
|
|
|
pass
|
|
|
|
|
|
def get_latest_deerjika (
|
|
) -> VideoInfo | None:
|
|
tag = '伊地知ニジカ'
|
|
url = f"https://www.nicovideo.jp/tag/{ tag }"
|
|
|
|
params = { 'sort': 'f',
|
|
'order': 'd' }
|
|
|
|
video_info = { }
|
|
|
|
bs = get_bs_from_url (url, params)
|
|
if bs is None:
|
|
return None
|
|
|
|
try:
|
|
video = (bs.find_all ('ul', class_ = 'videoListInner')[1]
|
|
.find ('li', class_ = 'item'))
|
|
|
|
video_info['contentId'] = video['data-video-id']
|
|
except Exception:
|
|
return None
|
|
|
|
bs = get_bs_from_url ('https://www.nicovideo.jp/watch/'
|
|
+ video_info['contentId'])
|
|
if bs is None:
|
|
return None
|
|
|
|
try:
|
|
video_info['title'] = '-'.join (bs.find ('title').text.split ('-')[:(-1)])[:(-1)]
|
|
|
|
tags = bs.find ('meta', attrs = { 'name': 'keywords' }).get ('content')
|
|
video_info['tags'] = tags.split (',')
|
|
|
|
video_info['description'] = bs.find ('meta', attrs = { 'name': 'description' }).get ('content')
|
|
except Exception:
|
|
return None
|
|
|
|
return video_info
|
|
|
|
|
|
def get_bs_from_url (
|
|
url: str,
|
|
params: dict = { },
|
|
) -> BeautifulSoup | None:
|
|
"""
|
|
URL から BeautifulSoup インスタンス生成
|
|
|
|
Parameters
|
|
----------
|
|
url: str
|
|
捜査する URL
|
|
params: dict
|
|
パラメータ
|
|
|
|
Return
|
|
------
|
|
BeautifulSoup | None
|
|
BeautifulSoup オブゼクト(失敗したら None)
|
|
"""
|
|
|
|
try:
|
|
req = requests.get (url, params = params, timeout = 60)
|
|
except Timeout:
|
|
return None
|
|
|
|
if req.status_code != 200:
|
|
return None
|
|
|
|
req.encoding = req.apparent_encoding
|
|
|
|
return BeautifulSoup (req.text, 'html.parser')
|