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.

182 lines
6.9 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import hashlib
  4. import re
  5. from .common import InfoExtractor
  6. from ..compat import compat_parse_qs
  7. from ..utils import (
  8. int_or_none,
  9. float_or_none,
  10. unified_timestamp,
  11. urlencode_postdata,
  12. )
  13. class BiliBiliIE(InfoExtractor):
  14. _VALID_URL = r'https?://(?:www\.|bangumi\.|)bilibili\.(?:tv|com)/(?:video/av|anime/v/)(?P<id>\d+)'
  15. _TESTS = [{
  16. 'url': 'http://www.bilibili.tv/video/av1074402/',
  17. 'md5': '9fa226fe2b8a9a4d5a69b4c6a183417e',
  18. 'info_dict': {
  19. 'id': '1074402',
  20. 'ext': 'mp4',
  21. 'title': '【金坷垃】金泡沫',
  22. 'description': 'md5:ce18c2a2d2193f0df2917d270f2e5923',
  23. 'duration': 308.315,
  24. 'timestamp': 1398012660,
  25. 'upload_date': '20140420',
  26. 'thumbnail': 're:^https?://.+\.jpg',
  27. 'uploader': '菊子桑',
  28. 'uploader_id': '156160',
  29. },
  30. }, {
  31. 'url': 'http://www.bilibili.com/video/av1041170/',
  32. 'info_dict': {
  33. 'id': '1041170',
  34. 'ext': 'mp4',
  35. 'title': '【BD1080P】刀语【诸神&异域】',
  36. 'description': '这是个神奇的故事~每个人不留弹幕不给走哦~切利哦!~',
  37. 'duration': 3382.259,
  38. 'timestamp': 1396530060,
  39. 'upload_date': '20140403',
  40. 'thumbnail': 're:^https?://.+\.jpg',
  41. 'uploader': '枫叶逝去',
  42. 'uploader_id': '520116',
  43. },
  44. }, {
  45. 'url': 'http://www.bilibili.com/video/av4808130/',
  46. 'info_dict': {
  47. 'id': '4808130',
  48. 'ext': 'mp4',
  49. 'title': '【长篇】哆啦A梦443【钉铛】',
  50. 'description': '(2016.05.27)来组合客人的脸吧&amp;amp;寻母六千里锭 抱歉,又轮到周日上班现在才到家 封面www.pixiv.net/member_illust.php?mode=medium&amp;amp;illust_id=56912929',
  51. 'duration': 1493.995,
  52. 'timestamp': 1464564180,
  53. 'upload_date': '20160529',
  54. 'thumbnail': 're:^https?://.+\.jpg',
  55. 'uploader': '喜欢拉面',
  56. 'uploader_id': '151066',
  57. },
  58. }, {
  59. # Missing upload time
  60. 'url': 'http://www.bilibili.com/video/av1867637/',
  61. 'info_dict': {
  62. 'id': '1867637',
  63. 'ext': 'mp4',
  64. 'title': '【HDTV】【喜剧】岳父岳母真难当 (2014)【法国票房冠军】',
  65. 'description': '一个信奉天主教的法国旧式传统资产阶级家庭中有四个女儿。三个女儿却分别找了阿拉伯、犹太、中国丈夫,老夫老妻唯独期盼剩下未嫁的小女儿能找一个信奉天主教的法国白人,结果没想到小女儿找了一位非裔黑人……【这次应该不会跳帧了】',
  66. 'duration': 5760.0,
  67. 'uploader': '黑夜为猫',
  68. 'uploader_id': '610729',
  69. 'thumbnail': 're:^https?://.+\.jpg',
  70. },
  71. 'params': {
  72. # Just to test metadata extraction
  73. 'skip_download': True,
  74. },
  75. 'expected_warnings': ['upload time'],
  76. }, {
  77. 'url': 'http://bangumi.bilibili.com/anime/v/40068',
  78. 'md5': '08d539a0884f3deb7b698fb13ba69696',
  79. 'info_dict': {
  80. 'id': '40068',
  81. 'ext': 'mp4',
  82. 'duration': 1402.357,
  83. 'title': '混沌武士 : 第7集 四面楚歌 A Risky Racket',
  84. 'description': 'md5:6a9622b911565794c11f25f81d6a97d2',
  85. 'thumbnail': 're:^http?://.+\.jpg',
  86. },
  87. }]
  88. _APP_KEY = '6f90a59ac58a4123'
  89. _BILIBILI_KEY = '0bfd84cc3940035173f35e6777508326'
  90. def _real_extract(self, url):
  91. video_id = self._match_id(url)
  92. webpage = self._download_webpage(url, video_id)
  93. if 'anime/v' not in url:
  94. cid = compat_parse_qs(self._search_regex(
  95. [r'EmbedPlayer\([^)]+,\s*"([^"]+)"\)',
  96. r'<iframe[^>]+src="https://secure\.bilibili\.com/secure,([^"]+)"'],
  97. webpage, 'player parameters'))['cid'][0]
  98. else:
  99. js = self._download_json(
  100. 'http://bangumi.bilibili.com/web_api/get_source', video_id,
  101. data=urlencode_postdata({'episode_id': video_id}),
  102. headers={'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'})
  103. cid = js['result']['cid']
  104. payload = 'appkey=%s&cid=%s&otype=json&quality=2&type=mp4' % (self._APP_KEY, cid)
  105. sign = hashlib.md5((payload + self._BILIBILI_KEY).encode('utf-8')).hexdigest()
  106. video_info = self._download_json(
  107. 'http://interface.bilibili.com/playurl?%s&sign=%s' % (payload, sign),
  108. video_id, note='Downloading video info page')
  109. entries = []
  110. for idx, durl in enumerate(video_info['durl']):
  111. formats = [{
  112. 'url': durl['url'],
  113. 'filesize': int_or_none(durl['size']),
  114. }]
  115. for backup_url in durl['backup_url']:
  116. formats.append({
  117. 'url': backup_url,
  118. # backup URLs have lower priorities
  119. 'preference': -2 if 'hd.mp4' in backup_url else -3,
  120. })
  121. self._sort_formats(formats)
  122. entries.append({
  123. 'id': '%s_part%s' % (video_id, idx),
  124. 'duration': float_or_none(durl.get('length'), 1000),
  125. 'formats': formats,
  126. })
  127. title = self._html_search_regex('<h1[^>]+title="([^"]+)">', webpage, 'title')
  128. description = self._html_search_meta('description', webpage)
  129. timestamp = unified_timestamp(self._html_search_regex(
  130. r'<time[^>]+datetime="([^"]+)"', webpage, 'upload time', fatal=False))
  131. thumbnail = self._html_search_meta(['og:image', 'thumbnailUrl'], webpage)
  132. # TODO 'view_count' requires deobfuscating Javascript
  133. info = {
  134. 'id': video_id,
  135. 'title': title,
  136. 'description': description,
  137. 'timestamp': timestamp,
  138. 'thumbnail': thumbnail,
  139. 'duration': float_or_none(video_info.get('timelength'), scale=1000),
  140. }
  141. uploader_mobj = re.search(
  142. r'<a[^>]+href="https?://space\.bilibili\.com/(?P<id>\d+)"[^>]+title="(?P<name>[^"]+)"',
  143. webpage)
  144. if uploader_mobj:
  145. info.update({
  146. 'uploader': uploader_mobj.group('name'),
  147. 'uploader_id': uploader_mobj.group('id'),
  148. })
  149. for entry in entries:
  150. entry.update(info)
  151. if len(entries) == 1:
  152. return entries[0]
  153. else:
  154. for idx, entry in enumerate(entries):
  155. entry['id'] = '%s_part%d' % (video_id, (idx + 1))
  156. return {
  157. '_type': 'multi_video',
  158. 'id': video_id,
  159. 'title': title,
  160. 'description': description,
  161. 'entries': entries,
  162. }