ぼざろクリーチャーシリーズ DB 兼 API(自分用)
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.

update_db.py 35 KiB

2 months ago
2 months ago
2 months ago
1 month ago
1 month ago
2 months ago
2 months ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
2 months ago
2 months ago
2 months ago
2 months ago
1 month ago
2 months ago
1 month ago
2 months ago
1 month ago
2 months ago
1 month ago
2 months ago
2 months ago
2 months ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
2 months ago
1 month ago
2 months ago
1 month ago
2 months ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
2 months ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
2 months ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072
  1. """
  2. 日次で実行し,ぼざクリ DB を最新に更新する.
  3. """
  4. from __future__ import annotations
  5. import json
  6. import os
  7. import random
  8. import string
  9. import time
  10. from dataclasses import dataclass
  11. from datetime import date, datetime, timedelta
  12. from typing import Any, Type, TypedDict, cast
  13. import mysql.connector
  14. import requests
  15. from mysql.connector.connection import MySQLConnectionAbstract
  16. class DbNull:
  17. def __new__ (
  18. cls,
  19. ):
  20. delattr (cls, '__init__')
  21. DbNullType = Type[DbNull]
  22. class VideoSearchParam (TypedDict):
  23. q: str
  24. targets: str
  25. _sort: str
  26. fields: str
  27. _limit: int
  28. jsonFilter: str
  29. class VideoResult (TypedDict):
  30. contentId: str
  31. title: str
  32. tags: str
  33. description: str | None
  34. viewCounter: int
  35. startTime: str
  36. class CommentResult (TypedDict):
  37. id: str
  38. no: int
  39. vposMs: int
  40. body: str
  41. commands: list[str]
  42. userId: str
  43. isPremium: bool
  44. score: int
  45. postedAt: str
  46. nicoruCount: int
  47. nicoruId: Any
  48. source: str
  49. isMyPost: bool
  50. class CommentRow (TypedDict):
  51. id: int
  52. video_id: int
  53. comment_no: int
  54. user_id: int
  55. content: str
  56. posted_at: datetime
  57. nico_count: int
  58. vpos_ms: int | None
  59. class TagRow (TypedDict):
  60. id: int
  61. name: str
  62. class UserRow (TypedDict):
  63. id: int
  64. code: str
  65. class VideoRow (TypedDict):
  66. id: int
  67. code: str
  68. title: str
  69. description: str
  70. uploaded_at: datetime
  71. deleted_at: datetime | None
  72. class VideoHistoryRow (TypedDict):
  73. id: int
  74. video_id: int
  75. fetched_at: date
  76. views_count: int
  77. class VideoTagRow (TypedDict):
  78. id: int
  79. video_id: int
  80. tag_id: int
  81. tagged_at: date
  82. untagged_at: date | None
  83. def main (
  84. ) -> None:
  85. conn = mysql.connector.connect (user = os.environ['MYSQL_USER'],
  86. password = os.environ['MYSQL_PASS'],
  87. database = 'nizika_nico')
  88. if not isinstance (conn, MySQLConnectionAbstract):
  89. raise TypeError
  90. now = datetime.now ()
  91. video_dao = VideoDao (conn)
  92. tag_dao = TagDao (conn)
  93. video_tag_dao = VideoTagDao (conn)
  94. video_history_dao = VideoHistoryDao (conn)
  95. comment_dao = CommentDao (conn)
  96. user_dao = UserDao (conn)
  97. api_data = search_nico_by_tags (['伊地知ニジカ', 'ぼざろクリーチャーシリーズ'])
  98. update_tables (video_dao, tag_dao, video_tag_dao, video_history_dao, comment_dao, user_dao,
  99. api_data, now)
  100. conn.commit ()
  101. conn.close ()
  102. def update_tables (
  103. video_dao: VideoDao,
  104. tag_dao: TagDao,
  105. video_tag_dao: VideoTagDao,
  106. video_history_dao: VideoHistoryDao,
  107. comment_dao: CommentDao,
  108. user_dao: UserDao,
  109. api_data: list[VideoResult],
  110. now: datetime,
  111. ) -> None:
  112. video_ids: list[int] = []
  113. for datum in api_data:
  114. tag_names: list[str] = datum['tags'].split ()
  115. video = VideoDto (code = datum['contentId'],
  116. title = datum['title'],
  117. description = datum['description'] or '',
  118. uploaded_at = datetime.fromisoformat (datum['startTime']))
  119. video_dao.upsert (video, False)
  120. if video.id_ is not None:
  121. video_ids.append (video.id_)
  122. video_history = VideoHistoryDto (video_id = video.id_,
  123. fetched_at = now,
  124. views_count = datum['viewCounter'])
  125. video_history_dao.insert (video_history)
  126. tag_ids: list[int] = []
  127. video_tags = video_tag_dao.fetch_alive_by_video_id (video.id_, False)
  128. for vt in video_tags:
  129. tag = tag_dao.find (vt.tag_id)
  130. if (tag is not None
  131. and (tag.name.upper () not in [tn.upper () for tn in tag_names])
  132. and (tag.id_ is not None)):
  133. tag_ids.append (tag.id_)
  134. video_tag_dao.untag_all (video.id_, tag_ids, now)
  135. tags: list[TagDto] = []
  136. for tag_name in tag_names:
  137. tag = tag_dao.fetch_by_name (tag_name)
  138. if tag is None:
  139. tag = TagDto (name = tag_name)
  140. tag_dao.insert (tag)
  141. if video.id_ is not None and tag.id_ is not None:
  142. video_tag = video_tag_dao.fetch_alive_by_ids (video.id_, tag.id_, False)
  143. if video_tag is None:
  144. video_tag = VideoTagDto (video_id = video.id_,
  145. tag_id = tag.id_,
  146. tagged_at = now)
  147. video_tag_dao.insert (video_tag, False)
  148. for com in fetch_comments (video.code):
  149. user = user_dao.fetch_by_code (com['userId'])
  150. if user is None:
  151. user = UserDto (code = com['userId'])
  152. user_dao.insert (user)
  153. if video.id_ is not None and user.id_ is not None:
  154. comment = CommentDto (video_id = video.id_,
  155. comment_no = com['no'],
  156. user_id = user.id_,
  157. content = com['body'],
  158. posted_at = datetime.fromisoformat (com['postedAt']),
  159. nico_count = com['nicoruCount'],
  160. vpos_ms = com['vposMs'])
  161. comment_dao.upsert (comment, False)
  162. alive_video_codes = [d['contentId'] for d in api_data]
  163. lost_video_ids: list[int] = []
  164. videos = video_dao.fetch_alive ()
  165. for video in videos:
  166. if video.id_ is not None and video.code not in alive_video_codes:
  167. lost_video_ids.append (video.id_)
  168. video_dao.delete (lost_video_ids, now)
  169. def fetch_comments (
  170. video_code: str,
  171. ) -> list[CommentResult]:
  172. time.sleep (1.2)
  173. headers = { 'X-Frontend-Id': '6',
  174. 'X-Frontend-Version': '0' }
  175. action_track_id = (
  176. ''.join (random.choice (string.ascii_letters + string.digits)
  177. for _ in range (10))
  178. + '_'
  179. + str (random.randrange (10 ** 12, 10 ** 13)))
  180. url = (f"https://www.nicovideo.jp/api/watch/v3_guest/{ video_code }"
  181. + f"?actionTrackId={ action_track_id }")
  182. res = requests.post (url, headers = headers, timeout = 60).json ()
  183. try:
  184. nv_comment = res['data']['comment']['nvComment']
  185. except KeyError:
  186. return []
  187. if nv_comment is None:
  188. return []
  189. headers = { 'X-Frontend-Id': '6',
  190. 'X-Frontend-Version': '0',
  191. 'Content-Type': 'application/json' }
  192. params = { 'params': nv_comment['params'],
  193. 'additionals': { },
  194. 'threadKey': nv_comment['threadKey'] }
  195. url = nv_comment['server'] + '/v1/threads'
  196. res = (requests.post (url, json.dumps (params),
  197. headers = headers,
  198. timeout = 60)
  199. .json ())
  200. try:
  201. return res['data']['threads'][1]['comments']
  202. except (IndexError, KeyError):
  203. return []
  204. def search_nico_by_tag (
  205. tag: str,
  206. ) -> list[VideoResult]:
  207. return search_nico_by_tags ([tag])
  208. def search_nico_by_tags (
  209. tags: list[str],
  210. ) -> list[VideoResult]:
  211. today = datetime.now ()
  212. url = ('https://snapshot.search.nicovideo.jp'
  213. + '/api/v2/snapshot/video/contents/search')
  214. result_data: list[VideoResult] = []
  215. to = datetime (2022, 12, 3)
  216. while to <= today:
  217. time.sleep (1.2)
  218. until = to + timedelta (days = 14)
  219. query_filter = json.dumps ({ 'type': 'or',
  220. 'filters': [
  221. { 'type': 'range',
  222. 'field': 'startTime',
  223. 'from': '%04d-%02d-%02dT00:00:00+09:00' % (to.year, to.month, to.day),
  224. 'to': '%04d-%02d-%02dT23:59:59+09:00' % (until.year, until.month, until.day),
  225. 'include_lower': True }] })
  226. params: VideoSearchParam = { 'q': ' OR '.join (tags),
  227. 'targets': 'tagsExact',
  228. '_sort': '-viewCounter',
  229. 'fields': ('contentId,'
  230. 'title,'
  231. 'tags,'
  232. 'description,'
  233. 'viewCounter,'
  234. 'startTime'),
  235. '_limit': 100,
  236. 'jsonFilter': query_filter }
  237. res = requests.get (url, params = cast (dict[str, int | str], params), timeout = 60).json ()
  238. try:
  239. result_data += res['data']
  240. except KeyError:
  241. pass
  242. to = until + timedelta (days = 1)
  243. return result_data
  244. class VideoDao:
  245. def __init__ (
  246. self,
  247. conn: MySQLConnectionAbstract,
  248. ):
  249. self.conn = conn
  250. def find (
  251. self,
  252. video_id: int,
  253. with_relation_tables: bool,
  254. ) -> VideoDto | None:
  255. with self.conn.cursor (dictionary = True) as c:
  256. c.execute ("""
  257. SELECT
  258. id,
  259. code,
  260. title,
  261. description,
  262. uploaded_at,
  263. deleted_at
  264. FROM
  265. videos
  266. WHERE
  267. id = %s
  268. ORDER BY
  269. id""", (video_id,))
  270. print (c._executed)
  271. row = cast (VideoRow | None, c.fetchone ())
  272. if row is None:
  273. return None
  274. return self._create_dto_from_row (row, with_relation_tables)
  275. def fetch_all (
  276. self,
  277. with_relation_tables: bool,
  278. ) -> list[VideoDto]:
  279. with self.conn.cursor (dictionary = True) as c:
  280. c.execute ("""
  281. SELECT
  282. id,
  283. code,
  284. title,
  285. description,
  286. uploaded_at,
  287. deleted_at
  288. FROM
  289. videos
  290. ORDER BY
  291. id""")
  292. print (c._executed)
  293. videos: list[VideoDto] = []
  294. for row in cast (list[VideoRow], c.fetchall ()):
  295. videos.append (self._create_dto_from_row (row, with_relation_tables))
  296. return videos
  297. def fetch_alive (
  298. self,
  299. ) -> list[VideoDto]:
  300. with self.conn.cursor (dictionary = True) as c:
  301. c.execute ("""
  302. SELECT
  303. id,
  304. code,
  305. title,
  306. description,
  307. uploaded_at,
  308. deleted_at
  309. FROM
  310. videos
  311. WHERE
  312. deleted_at IS NULL""")
  313. print (c._executed)
  314. videos: list[VideoDto] = []
  315. for row in cast (list[VideoRow], c.fetchall ()):
  316. videos.append (self._create_dto_from_row (row, False))
  317. return videos
  318. def upsert (
  319. self,
  320. video: VideoDto,
  321. with_relation_tables: bool,
  322. ) -> None:
  323. deleted_at: datetime | DbNullType | None = video.deleted_at
  324. if deleted_at is None:
  325. raise TypeError ('未実装')
  326. if deleted_at is DbNull:
  327. deleted_at = None
  328. deleted_at = cast (datetime | None, deleted_at)
  329. with self.conn.cursor (dictionary = True) as c:
  330. c.execute ("""
  331. INSERT INTO
  332. videos(
  333. code,
  334. title,
  335. description,
  336. uploaded_at,
  337. deleted_at)
  338. VALUES
  339. (
  340. %s,
  341. %s,
  342. %s,
  343. %s,
  344. %s)
  345. ON DUPLICATE KEY UPDATE
  346. id = LAST_INSERT_ID(id),
  347. code = VALUES(code),
  348. title = VALUES(title),
  349. description = VALUES(description),
  350. uploaded_at = VALUES(uploaded_at),
  351. deleted_at = VALUES(deleted_at)""", (video.code,
  352. video.title,
  353. video.description,
  354. video.uploaded_at,
  355. deleted_at))
  356. print (c._executed)
  357. video.id_ = c.lastrowid
  358. if with_relation_tables:
  359. if video.video_tags is not None:
  360. VideoTagDao (self.conn).upsert_all (video.video_tags, False)
  361. if video.comments is not None:
  362. CommentDao (self.conn).upsert_all (video.comments, False)
  363. if video.video_histories is not None:
  364. VideoHistoryDao (self.conn).upsert_all (video.video_histories)
  365. def upsert_all (
  366. self,
  367. videos: list[VideoDto],
  368. with_relation_tables: bool,
  369. ) -> None:
  370. for video in videos:
  371. self.upsert (video, with_relation_tables)
  372. def delete (
  373. self,
  374. video_ids: list[int],
  375. at: datetime,
  376. ) -> None:
  377. if not video_ids:
  378. return
  379. with self.conn.cursor (dictionary = True) as c:
  380. c.execute ("""
  381. UPDATE
  382. videos
  383. SET
  384. deleted_at = %%s
  385. WHERE
  386. id IN (%s)""" % ', '.join (['%s'] * len (video_ids)), (at, *video_ids))
  387. print (c._executed)
  388. def _create_dto_from_row (
  389. self,
  390. row: VideoRow,
  391. with_relation_tables: bool,
  392. ) -> VideoDto:
  393. video = VideoDto (id_ = row['id'],
  394. code = row['code'],
  395. title = row['title'],
  396. description = row['description'],
  397. uploaded_at = row['uploaded_at'],
  398. deleted_at = row['deleted_at'] or DbNull)
  399. if with_relation_tables and video.id_ is not None:
  400. video.video_tags = VideoTagDao (self.conn).fetch_by_video_id (video.id_, False)
  401. for i in range (len (video.video_tags)):
  402. video.video_tags[i].video = video
  403. video.comments = CommentDao (self.conn).fetch_by_video_id (video.id_, False)
  404. for i in range (len (video.comments)):
  405. video.comments[i].video = video
  406. video.video_histories = VideoHistoryDao (self.conn).fetch_by_video_id (video.id_, False)
  407. for i in range (len (video.video_histories)):
  408. video.video_histories[i].video = video
  409. return video
  410. @dataclass (slots = True)
  411. class VideoDto:
  412. code: str
  413. title: str
  414. description: str
  415. uploaded_at: datetime
  416. id_: int | None = None
  417. deleted_at: datetime | DbNullType = DbNull
  418. video_tags: list[VideoTagDto] | None = None
  419. comments: list[CommentDto] | None = None
  420. video_histories: list[VideoHistoryDto] | None = None
  421. class VideoTagDao:
  422. def __init__ (
  423. self,
  424. conn: MySQLConnectionAbstract,
  425. ):
  426. self.conn = conn
  427. def fetch_by_video_id (
  428. self,
  429. video_id: int,
  430. with_relation_tables: bool,
  431. ) -> list[VideoTagDto]:
  432. with self.conn.cursor (dictionary = True) as c:
  433. c.execute ("""
  434. SELECT
  435. id,
  436. video_id,
  437. tag_id,
  438. tagged_at,
  439. untagged_at
  440. FROM
  441. video_tags
  442. WHERE
  443. video_id = %s
  444. ORDER BY
  445. id""", (video_id,))
  446. print (c._executed)
  447. video_tags: list[VideoTagDto] = []
  448. for row in cast (list[VideoTagRow], c.fetchall ()):
  449. video_tags.append (self._create_dto_from_row (row, with_relation_tables))
  450. return video_tags
  451. def fetch_alive_by_video_id (
  452. self,
  453. video_id: int,
  454. with_relation_tables: bool,
  455. ) -> list[VideoTagDto]:
  456. with self.conn.cursor (dictionary = True) as c:
  457. c.execute ("""
  458. SELECT
  459. id,
  460. video_id,
  461. tag_id,
  462. tagged_at,
  463. untagged_at
  464. FROM
  465. video_tags
  466. WHERE
  467. video_id = %s
  468. AND (untagged_at IS NULL)
  469. ORDER BY
  470. id""", (video_id,))
  471. print (c._executed)
  472. video_tags: list[VideoTagDto] = []
  473. for row in cast (list[VideoTagRow], c.fetchall ()):
  474. video_tags.append (self._create_dto_from_row (row, with_relation_tables))
  475. return video_tags
  476. def fetch_alive_by_ids (
  477. self,
  478. video_id: int,
  479. tag_id: int,
  480. with_relation_tables: bool,
  481. ) -> VideoTagDto | None:
  482. with self.conn.cursor (dictionary = True) as c:
  483. c.execute ("""
  484. SELECT
  485. id,
  486. video_id,
  487. tag_id,
  488. tagged_at,
  489. untagged_at
  490. FROM
  491. video_tags
  492. WHERE
  493. video_id = %s
  494. AND tag_id = %s""", (video_id, tag_id))
  495. print (c._executed)
  496. row = cast (VideoTagRow, c.fetchone ())
  497. if row is None:
  498. return None
  499. return self._create_dto_from_row (row, with_relation_tables)
  500. def insert (
  501. self,
  502. video_tag: VideoTagDto,
  503. with_relation_tables: bool,
  504. ) -> None:
  505. untagged_at: date | DbNullType | None = video_tag.untagged_at
  506. if untagged_at is None:
  507. raise TypeError ('未実装')
  508. if untagged_at is DbNull:
  509. untagged_at = None
  510. untagged_at = cast (date | None, untagged_at)
  511. with self.conn.cursor (dictionary = True) as c:
  512. c.execute ("""
  513. INSERT INTO
  514. video_tags(
  515. video_id,
  516. tag_id,
  517. tagged_at,
  518. untagged_at)
  519. VALUES
  520. (
  521. %s,
  522. %s,
  523. %s,
  524. %s)""", (video_tag.video_id, video_tag.tag_id,
  525. video_tag.tagged_at, untagged_at))
  526. print (c._executed)
  527. video_tag.id_ = c.lastrowid
  528. if with_relation_tables:
  529. if video_tag.video is not None:
  530. VideoDao (self.conn).upsert (video_tag.video, True)
  531. if video_tag.tag is not None:
  532. TagDao (self.conn).upsert (video_tag.tag)
  533. def upsert (
  534. self,
  535. video_tag: VideoTagDto,
  536. with_relation_tables: bool,
  537. ) -> None:
  538. untagged_at: date | DbNullType | None = video_tag.untagged_at
  539. if untagged_at is None:
  540. raise TypeError ('未実装')
  541. if untagged_at is DbNull:
  542. untagged_at = None
  543. untagged_at = cast (date | None, untagged_at)
  544. with self.conn.cursor (dictionary = True) as c:
  545. c.execute ("""
  546. INSERT INTO
  547. video_tags(
  548. video_id,
  549. tag_id,
  550. tagged_at,
  551. untagged_at)
  552. VALUES
  553. (
  554. %s,
  555. %s,
  556. %s,
  557. %s)
  558. ON DUPLICATE KEY UPDATE
  559. id = LAST_INSERT_ID(id),
  560. video_id = VALUES(video_id),
  561. tag_id = VALUES(tag_id),
  562. tagged_at = VALUES(tagged_at),
  563. untagged_at = VALUES(untagged_at)""", (video_tag.video_id,
  564. video_tag.tag_id,
  565. video_tag.tagged_at,
  566. untagged_at))
  567. print (c._executed)
  568. video_tag.id_ = c.lastrowid
  569. if with_relation_tables:
  570. if video_tag.video is not None:
  571. VideoDao (self.conn).upsert (video_tag.video, True)
  572. if video_tag.tag is not None:
  573. TagDao (self.conn).upsert (video_tag.tag)
  574. def upsert_all (
  575. self,
  576. video_tags: list[VideoTagDto],
  577. with_relation_tables: bool,
  578. ) -> None:
  579. for video_tag in video_tags:
  580. self.upsert (video_tag, with_relation_tables)
  581. def untag_all (
  582. self,
  583. video_id: int,
  584. tag_ids: list[int],
  585. now: datetime,
  586. ) -> None:
  587. if not tag_ids:
  588. return
  589. with self.conn.cursor (dictionary = True) as c:
  590. c.execute ("""
  591. UPDATE
  592. video_tags
  593. SET
  594. untagged_at = %%s
  595. WHERE
  596. video_id = %%s
  597. AND tag_id IN (%s)""" % ', '.join (['%s'] * len (tag_ids)), (now, video_id, *tag_ids))
  598. print (c._executed)
  599. def _create_dto_from_row (
  600. self,
  601. row: VideoTagRow,
  602. with_relation_tables: bool,
  603. ) -> VideoTagDto:
  604. video_tag = VideoTagDto (id_ = row['id'],
  605. video_id = row['video_id'],
  606. tag_id = row['tag_id'],
  607. tagged_at = row['tagged_at'],
  608. untagged_at = row['untagged_at'] or DbNull)
  609. if with_relation_tables:
  610. video_tag.video = VideoDao (self.conn).find (video_tag.video_id, True)
  611. video_tag.tag = TagDao (self.conn).find (video_tag.tag_id)
  612. return video_tag
  613. @dataclass (slots = True)
  614. class VideoTagDto:
  615. video_id: int
  616. tag_id: int
  617. tagged_at: date
  618. id_: int | None = None
  619. untagged_at: date | DbNullType = DbNull
  620. video: VideoDto | None = None
  621. tag: TagDto | None = None
  622. class TagDao:
  623. def __init__ (
  624. self,
  625. conn: MySQLConnectionAbstract,
  626. ):
  627. self.conn = conn
  628. def find (
  629. self,
  630. tag_id: int,
  631. ) -> TagDto | None:
  632. with self.conn.cursor (dictionary = True) as c:
  633. c.execute ("""
  634. SELECT
  635. id,
  636. name
  637. FROM
  638. tags
  639. WHERE
  640. id = %s""", (tag_id,))
  641. print (c._executed)
  642. row = cast (TagRow | None, c.fetchone ())
  643. if row is None:
  644. return None
  645. return self._create_dto_from_row (row)
  646. def fetch_by_name (
  647. self,
  648. tag_name: str,
  649. ) -> TagDto | None:
  650. with self.conn.cursor (dictionary = True) as c:
  651. c.execute ("""
  652. SELECT
  653. id,
  654. name
  655. FROM
  656. tags
  657. WHERE
  658. name = %s""", (tag_name,))
  659. print (c._executed)
  660. row = cast (TagRow | None, c.fetchone ())
  661. if row is None:
  662. return None
  663. return self._create_dto_from_row (row)
  664. def insert (
  665. self,
  666. tag: TagDto,
  667. ) -> None:
  668. with self.conn.cursor (dictionary = True) as c:
  669. c.execute ("""
  670. INSERT INTO
  671. tags(name)
  672. VALUES
  673. (%s)""", (tag.name,))
  674. print (c._executed)
  675. tag.id_ = c.lastrowid
  676. def upsert (
  677. self,
  678. tag: TagDto,
  679. ) -> None:
  680. with self.conn.cursor (dictionary = True) as c:
  681. c.execute ("""
  682. INSERT INTO
  683. tags(name)
  684. VALUES
  685. (%s)
  686. ON DUPLICATE KEY UPDATE
  687. id = LAST_INSERT_ID(id),
  688. name = VALUES(name)""", (tag.name,))
  689. print (c._executed)
  690. tag.id_ = c.lastrowid
  691. def _create_dto_from_row (
  692. self,
  693. row: TagRow,
  694. ) -> TagDto:
  695. return TagDto (id_ = row['id'],
  696. name = row['name'])
  697. @dataclass (slots = True)
  698. class TagDto:
  699. name: str
  700. id_: int | None = None
  701. class VideoHistoryDao:
  702. def __init__ (
  703. self,
  704. conn: MySQLConnectionAbstract,
  705. ):
  706. self.conn = conn
  707. def fetch_by_video_id (
  708. self,
  709. video_id: int,
  710. with_relation_tables: bool,
  711. ) -> list[VideoHistoryDto]:
  712. with self.conn.cursor (dictionary = True) as c:
  713. c.execute ("""
  714. SELECT
  715. id,
  716. video_id,
  717. fetched_at,
  718. views_count
  719. FROM
  720. video_histories
  721. WHERE
  722. video_id = %s""", (video_id,))
  723. print (c._executed)
  724. video_histories: list[VideoHistoryDto] = []
  725. for row in cast (list[VideoHistoryRow], c.fetchall ()):
  726. video_histories.append (self._create_dto_from_row (row, with_relation_tables))
  727. return video_histories
  728. def insert (
  729. self,
  730. video_history: VideoHistoryDto,
  731. ) -> None:
  732. with self.conn.cursor (dictionary = True) as c:
  733. c.execute ("""
  734. INSERT INTO
  735. video_histories(
  736. video_id,
  737. fetched_at,
  738. views_count)
  739. VALUES
  740. (
  741. %s,
  742. %s,
  743. %s)""", (video_history.video_id,
  744. video_history.fetched_at,
  745. video_history.views_count))
  746. print (c._executed)
  747. def upsert (
  748. self,
  749. video_history: VideoHistoryDto,
  750. ) -> None:
  751. with self.conn.cursor (dictionary = True) as c:
  752. c.execute ("""
  753. INSERT INTO
  754. video_histories(
  755. video_id,
  756. fetched_at,
  757. views_count)
  758. VALUES
  759. (
  760. %s,
  761. %s,
  762. %s)
  763. ON DUPLICATE KEY UPDATE
  764. id = LAST_INSERT_ID(id),
  765. video_id = VALUES(video_id),
  766. fetched_at = VALUES(fetched_at),
  767. views_count = VALUES(views_count)""", (video_history.video_id,
  768. video_history.fetched_at,
  769. video_history.views_count))
  770. print (c._executed)
  771. def upsert_all (
  772. self,
  773. video_histories: list[VideoHistoryDto],
  774. ) -> None:
  775. for video_history in video_histories:
  776. self.upsert (video_history)
  777. def _create_dto_from_row (
  778. self,
  779. row: VideoHistoryRow,
  780. with_relation_tables: bool,
  781. ) -> VideoHistoryDto:
  782. video_history = VideoHistoryDto (id_ = row['id'],
  783. video_id = row['video_id'],
  784. fetched_at = row['fetched_at'],
  785. views_count = row['views_count'])
  786. if with_relation_tables:
  787. video_history.video = VideoDao (self.conn).find (video_history.video_id, True)
  788. return video_history
  789. @dataclass (slots = True)
  790. class VideoHistoryDto:
  791. video_id: int
  792. fetched_at: date
  793. views_count: int
  794. id_: int | None = None
  795. video: VideoDto | None = None
  796. class CommentDao:
  797. def __init__ (
  798. self,
  799. conn: MySQLConnectionAbstract,
  800. ):
  801. self.conn = conn
  802. def fetch_by_video_id (
  803. self,
  804. video_id: int,
  805. with_relation_tables: bool,
  806. ) -> list[CommentDto]:
  807. with self.conn.cursor (dictionary = True) as c:
  808. c.execute ("""
  809. SELECT
  810. id,
  811. video_id,
  812. comment_no,
  813. user_id,
  814. content,
  815. posted_at,
  816. nico_count,
  817. vpos_ms
  818. FROM
  819. comments
  820. WHERE
  821. video_id = %s""", (video_id,))
  822. print (c._executed)
  823. comments: list[CommentDto] = []
  824. for row in cast (list[CommentRow], c.fetchall ()):
  825. comments.append (self._create_dto_from_row (row, with_relation_tables))
  826. return comments
  827. def upsert (
  828. self,
  829. comment: CommentDto,
  830. with_relation_tables: bool,
  831. ) -> None:
  832. vpos_ms: int | DbNullType | None = comment.vpos_ms
  833. if vpos_ms is None:
  834. raise TypeError ('未実装')
  835. if vpos_ms is DbNull:
  836. vpos_ms = None
  837. vpos_ms = cast (int | None, vpos_ms)
  838. with self.conn.cursor (dictionary = True) as c:
  839. c.execute ("""
  840. INSERT INTO
  841. comments(
  842. video_id,
  843. comment_no,
  844. user_id,
  845. content,
  846. posted_at,
  847. nico_count,
  848. vpos_ms)
  849. VALUES
  850. (
  851. %s,
  852. %s,
  853. %s,
  854. %s,
  855. %s,
  856. %s,
  857. %s)
  858. ON DUPLICATE KEY UPDATE
  859. id = LAST_INSERT_ID(id),
  860. video_id = VALUES(video_id),
  861. comment_no = VALUES(comment_no),
  862. user_id = VALUES(user_id),
  863. content = VALUES(content),
  864. posted_at = VALUES(posted_at),
  865. nico_count = VALUES(nico_count),
  866. vpos_ms = VALUES(vpos_ms)""", (comment.video_id,
  867. comment.comment_no,
  868. comment.user_id,
  869. comment.content,
  870. comment.posted_at,
  871. comment.nico_count,
  872. vpos_ms))
  873. print (c._executed)
  874. def upsert_all (
  875. self,
  876. comments: list[CommentDto],
  877. with_relation_tables: bool,
  878. ) -> None:
  879. for comment in comments:
  880. self.upsert (comment, with_relation_tables)
  881. def _create_dto_from_row (
  882. self,
  883. row: CommentRow,
  884. with_relation_tables: bool,
  885. ) -> CommentDto:
  886. comment = CommentDto (id_ = row['id'],
  887. video_id = row['video_id'],
  888. comment_no = row['comment_no'],
  889. user_id = row['user_id'],
  890. content = row['content'],
  891. posted_at = row['posted_at'],
  892. nico_count = row['nico_count'],
  893. vpos_ms = row['vpos_ms'] or DbNull)
  894. if with_relation_tables:
  895. comment.video = VideoDao (self.conn).find (comment.video_id, True)
  896. return comment
  897. @dataclass (slots = True)
  898. class CommentDto:
  899. video_id: int
  900. comment_no: int
  901. user_id: int
  902. content: str
  903. posted_at: datetime
  904. id_: int | None = None
  905. nico_count: int = 0
  906. vpos_ms: int | DbNullType = DbNull
  907. video: VideoDto | None = None
  908. user: UserDto | None = None
  909. class UserDao:
  910. def __init__ (
  911. self,
  912. conn: MySQLConnectionAbstract,
  913. ):
  914. self.conn = conn
  915. def fetch_by_code (
  916. self,
  917. user_code: str
  918. ) -> UserDto | None:
  919. with self.conn.cursor (dictionary = True) as c:
  920. c.execute ("""
  921. SELECT
  922. id,
  923. code
  924. FROM
  925. users
  926. WHERE
  927. code = %s""", (user_code,))
  928. print (c._executed)
  929. row = cast (UserRow | None, c.fetchone ())
  930. if row is None:
  931. return None
  932. return self._create_dto_from_row (row)
  933. def insert (
  934. self,
  935. user: UserDto,
  936. ) -> None:
  937. with self.conn.cursor (dictionary = True) as c:
  938. c.execute ("""
  939. INSERT INTO
  940. users(code)
  941. VALUES
  942. (%s)""", (user.code,))
  943. print (c.execute)
  944. user.id_ = c.lastrowid
  945. def _create_dto_from_row (
  946. self,
  947. row: UserRow,
  948. ) -> UserDto:
  949. return UserDto (id_ = row['id'],
  950. code = row['code'])
  951. @dataclass (slots = True)
  952. class UserDto:
  953. code: str
  954. id_: int | None = None
  955. if __name__ == '__main__':
  956. main ()