acfun.py 7.8 KB

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