acfun.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. from .common import InfoExtractor
  2. from ..utils import (
  3. float_or_none,
  4. format_field,
  5. int_or_none,
  6. traverse_obj,
  7. parse_codecs,
  8. parse_qs,
  9. )
  10. class AcFunVideoBaseIE(InfoExtractor):
  11. def _extract_metadata(self, video_id, video_info):
  12. playjson = self._parse_json(video_info['ksPlayJson'], video_id)
  13. formats, subtitles = [], {}
  14. for video in traverse_obj(playjson, ('adaptationSet', 0, 'representation')):
  15. fmts, subs = self._extract_m3u8_formats_and_subtitles(video['url'], video_id, 'mp4', fatal=False)
  16. formats.extend(fmts)
  17. self._merge_subtitles(subs, target=subtitles)
  18. for f in fmts:
  19. f.update({
  20. 'fps': float_or_none(video.get('frameRate')),
  21. 'width': int_or_none(video.get('width')),
  22. 'height': int_or_none(video.get('height')),
  23. 'tbr': float_or_none(video.get('avgBitrate')),
  24. **parse_codecs(video.get('codecs', ''))
  25. })
  26. return {
  27. 'id': video_id,
  28. 'formats': formats,
  29. 'subtitles': subtitles,
  30. 'duration': float_or_none(video_info.get('durationMillis'), 1000),
  31. 'timestamp': int_or_none(video_info.get('uploadTime'), 1000),
  32. 'http_headers': {'Referer': 'https://www.acfun.cn/'},
  33. }
  34. class AcFunVideoIE(AcFunVideoBaseIE):
  35. _VALID_URL = r'https?://www\.acfun\.cn/v/ac(?P<id>[_\d]+)'
  36. _TESTS = [{
  37. 'url': 'https://www.acfun.cn/v/ac35457073',
  38. 'info_dict': {
  39. 'id': '35457073',
  40. 'ext': 'mp4',
  41. 'duration': 174.208,
  42. 'timestamp': 1656403967,
  43. 'title': '1 8 岁 现 状',
  44. 'description': '“赶紧回去!班主任查班了!”',
  45. 'uploader': '锤子game',
  46. 'uploader_id': '51246077',
  47. 'thumbnail': r're:^https?://.*\.(jpg|jpeg)',
  48. 'upload_date': '20220628',
  49. 'like_count': int,
  50. 'view_count': int,
  51. 'comment_count': int,
  52. 'tags': list,
  53. },
  54. }, {
  55. # example for len(video_list) > 1
  56. 'url': 'https://www.acfun.cn/v/ac35468952_2',
  57. 'info_dict': {
  58. 'id': '35468952_2',
  59. 'ext': 'mp4',
  60. 'title': '【动画剧集】Rocket & Groot Season 1(2022)/火箭浣熊与格鲁特第1季 P02 S01E02 十拿九穩',
  61. 'duration': 90.459,
  62. 'uploader': '比令',
  63. 'uploader_id': '37259967',
  64. 'upload_date': '20220629',
  65. 'timestamp': 1656479962,
  66. 'tags': list,
  67. 'like_count': int,
  68. 'view_count': int,
  69. 'comment_count': int,
  70. 'thumbnail': r're:^https?://.*\.(jpg|jpeg)',
  71. 'description': 'md5:67583aaf3a0f933bd606bc8a2d3ebb17',
  72. }
  73. }]
  74. def _real_extract(self, url):
  75. video_id = self._match_id(url)
  76. webpage = self._download_webpage(url, video_id)
  77. json_all = self._search_json(r'window.videoInfo\s*=', webpage, 'videoInfo', video_id)
  78. title = json_all.get('title')
  79. video_list = json_all.get('videoList') or []
  80. video_internal_id = traverse_obj(json_all, ('currentVideoInfo', 'id'))
  81. if video_internal_id and len(video_list) > 1:
  82. part_idx, part_video_info = next(
  83. (idx + 1, v) for (idx, v) in enumerate(video_list)
  84. if v['id'] == video_internal_id)
  85. title = f'{title} P{part_idx:02d} {part_video_info["title"]}'
  86. return {
  87. **self._extract_metadata(video_id, json_all['currentVideoInfo']),
  88. 'title': title,
  89. 'thumbnail': json_all.get('coverUrl'),
  90. 'description': json_all.get('description'),
  91. 'uploader': traverse_obj(json_all, ('user', 'name')),
  92. 'uploader_id': traverse_obj(json_all, ('user', 'href')),
  93. 'tags': traverse_obj(json_all, ('tagList', ..., 'name')),
  94. 'view_count': int_or_none(json_all.get('viewCount')),
  95. 'like_count': int_or_none(json_all.get('likeCountShow')),
  96. 'comment_count': int_or_none(json_all.get('commentCountShow')),
  97. }
  98. class AcFunBangumiIE(AcFunVideoBaseIE):
  99. _VALID_URL = r'https?://www\.acfun\.cn/bangumi/(?P<id>aa[_\d]+)'
  100. _TESTS = [{
  101. 'url': 'https://www.acfun.cn/bangumi/aa6002917_36188_1745457?ac=2',
  102. 'info_dict': {
  103. 'id': 'aa6002917_36188_1745457__2',
  104. 'ext': 'mp4',
  105. 'title': '【7月】租借女友 水原千鹤角色曲『DATE』特别PV',
  106. 'upload_date': '20200916',
  107. 'timestamp': 1600243813,
  108. 'duration': 92.091,
  109. },
  110. }, {
  111. 'url': 'https://www.acfun.cn/bangumi/aa5023171_36188_1750645',
  112. 'info_dict': {
  113. 'id': 'aa5023171_36188_1750645',
  114. 'ext': 'mp4',
  115. 'title': '红孩儿之趴趴蛙寻石记 第5话 ',
  116. 'duration': 760.0,
  117. 'season': '红孩儿之趴趴蛙寻石记',
  118. 'season_id': 5023171,
  119. 'season_number': 1, # series has only 1 season
  120. 'episode': 'Episode 5',
  121. 'episode_number': 5,
  122. 'upload_date': '20181223',
  123. 'timestamp': 1545552185,
  124. 'thumbnail': r're:^https?://.*\.(jpg|jpeg|png)',
  125. 'comment_count': int,
  126. },
  127. }, {
  128. 'url': 'https://www.acfun.cn/bangumi/aa6065485_36188_1885061',
  129. 'info_dict': {
  130. 'id': 'aa6065485_36188_1885061',
  131. 'ext': 'mp4',
  132. 'title': '叽歪老表(第二季) 第5话 坚不可摧',
  133. 'season': '叽歪老表(第二季)',
  134. 'season_number': 2,
  135. 'season_id': 6065485,
  136. 'episode': '坚不可摧',
  137. 'episode_number': 5,
  138. 'upload_date': '20220324',
  139. 'timestamp': 1648082786,
  140. 'duration': 105.002,
  141. 'thumbnail': r're:^https?://.*\.(jpg|jpeg|png)',
  142. 'comment_count': int,
  143. },
  144. }]
  145. def _real_extract(self, url):
  146. video_id = self._match_id(url)
  147. ac_idx = parse_qs(url).get('ac', [None])[-1]
  148. video_id = f'{video_id}{format_field(ac_idx, None, "__%s")}'
  149. webpage = self._download_webpage(url, video_id)
  150. json_bangumi_data = self._search_json(r'window.bangumiData\s*=', webpage, 'bangumiData', video_id)
  151. if ac_idx:
  152. video_info = json_bangumi_data['hlVideoInfo']
  153. return {
  154. **self._extract_metadata(video_id, video_info),
  155. 'title': video_info.get('title'),
  156. }
  157. video_info = json_bangumi_data['currentVideoInfo']
  158. season_id = json_bangumi_data.get('bangumiId')
  159. season_number = season_id and next((
  160. idx for idx, v in enumerate(json_bangumi_data.get('relatedBangumis') or [], 1)
  161. if v.get('id') == season_id), 1)
  162. json_bangumi_list = self._search_json(
  163. r'window\.bangumiList\s*=', webpage, 'bangumiList', video_id, fatal=False)
  164. video_internal_id = int_or_none(traverse_obj(json_bangumi_data, ('currentVideoInfo', 'id')))
  165. episode_number = video_internal_id and next((
  166. idx for idx, v in enumerate(json_bangumi_list.get('items') or [], 1)
  167. if v.get('videoId') == video_internal_id), None)
  168. return {
  169. **self._extract_metadata(video_id, video_info),
  170. 'title': json_bangumi_data.get('showTitle'),
  171. 'thumbnail': json_bangumi_data.get('image'),
  172. 'season': json_bangumi_data.get('bangumiTitle'),
  173. 'season_id': season_id,
  174. 'season_number': season_number,
  175. 'episode': json_bangumi_data.get('title'),
  176. 'episode_number': episode_number,
  177. 'comment_count': int_or_none(json_bangumi_data.get('commentCount')),
  178. }