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.

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