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.

125 lines
4.3 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. _TEST = {
  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. _APP_KEY = '6f90a59ac58a4123'
  32. _BILIBILI_KEY = '0bfd84cc3940035173f35e6777508326'
  33. def _real_extract(self, url):
  34. video_id = self._match_id(url)
  35. webpage = self._download_webpage(url, video_id)
  36. if 'anime/v' not in url:
  37. cid = compat_parse_qs(self._search_regex(
  38. [r'EmbedPlayer\([^)]+,\s*"([^"]+)"\)',
  39. r'<iframe[^>]+src="https://secure\.bilibili\.com/secure,([^"]+)"'],
  40. webpage, 'player parameters'))['cid'][0]
  41. else:
  42. js = self._download_json(
  43. 'http://bangumi.bilibili.com/web_api/get_source', video_id,
  44. data=urlencode_postdata({'episode_id': video_id}),
  45. headers={'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'})
  46. cid = js['result']['cid']
  47. payload = 'appkey=%s&cid=%s&otype=json&quality=2&type=mp4' % (self._APP_KEY, cid)
  48. sign = hashlib.md5((payload + self._BILIBILI_KEY).encode('utf-8')).hexdigest()
  49. video_info = self._download_json(
  50. 'http://interface.bilibili.com/playurl?%s&sign=%s' % (payload, sign),
  51. video_id, note='Downloading video info page')
  52. entries = []
  53. for idx, durl in enumerate(video_info['durl']):
  54. formats = [{
  55. 'url': durl['url'],
  56. 'filesize': int_or_none(durl['size']),
  57. }]
  58. for backup_url in durl.get('backup_url', []):
  59. formats.append({
  60. 'url': backup_url,
  61. # backup URLs have lower priorities
  62. 'preference': -2 if 'hd.mp4' in backup_url else -3,
  63. })
  64. self._sort_formats(formats)
  65. entries.append({
  66. 'id': '%s_part%s' % (video_id, idx),
  67. 'duration': float_or_none(durl.get('length'), 1000),
  68. 'formats': formats,
  69. })
  70. title = self._html_search_regex('<h1[^>]+title="([^"]+)">', webpage, 'title')
  71. description = self._html_search_meta('description', webpage)
  72. timestamp = unified_timestamp(self._html_search_regex(
  73. r'<time[^>]+datetime="([^"]+)"', webpage, 'upload time', fatal=False))
  74. thumbnail = self._html_search_meta(['og:image', 'thumbnailUrl'], webpage)
  75. # TODO 'view_count' requires deobfuscating Javascript
  76. info = {
  77. 'id': video_id,
  78. 'title': title,
  79. 'description': description,
  80. 'timestamp': timestamp,
  81. 'thumbnail': thumbnail,
  82. 'duration': float_or_none(video_info.get('timelength'), scale=1000),
  83. }
  84. uploader_mobj = re.search(
  85. r'<a[^>]+href="https?://space\.bilibili\.com/(?P<id>\d+)"[^>]+title="(?P<name>[^"]+)"',
  86. webpage)
  87. if uploader_mobj:
  88. info.update({
  89. 'uploader': uploader_mobj.group('name'),
  90. 'uploader_id': uploader_mobj.group('id'),
  91. })
  92. for entry in entries:
  93. entry.update(info)
  94. if len(entries) == 1:
  95. return entries[0]
  96. else:
  97. for idx, entry in enumerate(entries):
  98. entry['id'] = '%s_part%d' % (video_id, (idx + 1))
  99. return {
  100. '_type': 'multi_video',
  101. 'id': video_id,
  102. 'title': title,
  103. 'description': description,
  104. 'entries': entries,
  105. }