ニジカのスカトロ,ニジカトロ. https://bsky.app/profile/deerjika-bot.bsky.social
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.

nico.py 2.0 KiB

2 months ago
2 months ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. """
  2. ニコニコのニジカ動画取得モヂュール
  3. """
  4. from typing import TypedDict
  5. import requests
  6. from bs4 import BeautifulSoup
  7. from requests.exceptions import Timeout
  8. class VideoInfo (TypedDict):
  9. contentId: str
  10. title: str
  11. tags: list[str]
  12. description: str
  13. def get_latest_deerjika (
  14. ) -> VideoInfo | None:
  15. tag = '伊地知ニジカ OR ぼざろクリーチャーシリーズ'
  16. url = f"https://www.nicovideo.jp/tag/{ tag }"
  17. params = { 'sort': 'f',
  18. 'order': 'd' }
  19. video_info = { }
  20. bs = get_bs_from_url (url, params)
  21. if bs is None:
  22. return None
  23. try:
  24. video = (bs.find_all ('ul', class_ = 'videoListInner')[1]
  25. .find ('li', class_ = 'item'))
  26. video_info['contentId'] = video['data-video-id']
  27. except Exception:
  28. return None
  29. bs = get_bs_from_url ('https://www.nicovideo.jp/watch/'
  30. + video_info['contentId'])
  31. if bs is None:
  32. return None
  33. try:
  34. video_info['title'] = '-'.join (bs.find ('title').text.split ('-')[:(-1)])[:(-1)]
  35. tags = bs.find ('meta', attrs = { 'name': 'keywords' }).get ('content')
  36. video_info['tags'] = tags.split (',')
  37. video_info['description'] = bs.find ('meta', attrs = { 'name': 'description' }).get ('content')
  38. except Exception:
  39. return None
  40. return video_info
  41. def get_bs_from_url (
  42. url: str,
  43. params: dict = { },
  44. ) -> BeautifulSoup | None:
  45. """
  46. URL から BeautifulSoup インスタンス生成
  47. Parameters
  48. ----------
  49. url: str
  50. 捜査する URL
  51. params: dict
  52. パラメータ
  53. Return
  54. ------
  55. BeautifulSoup | None
  56. BeautifulSoup オブゼクト(失敗したら None)
  57. """
  58. try:
  59. req = requests.get (url, params = params, timeout = 60)
  60. except Timeout:
  61. return None
  62. if req.status_code != 200:
  63. return None
  64. req.encoding = req.apparent_encoding
  65. return BeautifulSoup (req.text, 'html.parser')