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.

229 lines
9.0 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import calendar
  4. import datetime
  5. import re
  6. from .common import InfoExtractor
  7. from ..compat import (
  8. compat_etree_fromstring,
  9. compat_str,
  10. compat_parse_qs,
  11. compat_xml_parse_error,
  12. )
  13. from ..utils import (
  14. ExtractorError,
  15. int_or_none,
  16. float_or_none,
  17. xpath_text,
  18. )
  19. class BiliBiliIE(InfoExtractor):
  20. _VALID_URL = r'https?://www\.bilibili\.(?:tv|com)/video/av(?P<id>\d+)'
  21. _TESTS = [{
  22. 'url': 'http://www.bilibili.tv/video/av1074402/',
  23. 'md5': '5f7d29e1a2872f3df0cf76b1f87d3788',
  24. 'info_dict': {
  25. 'id': '1554319',
  26. 'ext': 'flv',
  27. 'title': '【金坷垃】金泡沫',
  28. 'description': 'md5:ce18c2a2d2193f0df2917d270f2e5923',
  29. 'duration': 308.067,
  30. 'timestamp': 1398012660,
  31. 'upload_date': '20140420',
  32. 'thumbnail': 're:^https?://.+\.jpg',
  33. 'uploader': '菊子桑',
  34. 'uploader_id': '156160',
  35. },
  36. }, {
  37. 'url': 'http://www.bilibili.com/video/av1041170/',
  38. 'info_dict': {
  39. 'id': '1041170',
  40. 'title': '【BD1080P】刀语【诸神&异域】',
  41. 'description': '这是个神奇的故事~每个人不留弹幕不给走哦~切利哦!~',
  42. },
  43. 'playlist_count': 9,
  44. }, {
  45. 'url': 'http://www.bilibili.com/video/av4808130/',
  46. 'info_dict': {
  47. 'id': '4808130',
  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. },
  51. 'playlist': [{
  52. 'md5': '55cdadedf3254caaa0d5d27cf20a8f9c',
  53. 'info_dict': {
  54. 'id': '4808130_part1',
  55. 'ext': 'flv',
  56. 'title': '【长篇】哆啦A梦443【钉铛】',
  57. 'description': '(2016.05.27)来组合客人的脸吧&amp;amp;寻母六千里锭 抱歉,又轮到周日上班现在才到家 封面www.pixiv.net/member_illust.php?mode=medium&amp;amp;illust_id=56912929',
  58. 'timestamp': 1464564180,
  59. 'upload_date': '20160529',
  60. 'uploader': '喜欢拉面',
  61. 'uploader_id': '151066',
  62. },
  63. }, {
  64. 'md5': '926f9f67d0c482091872fbd8eca7ea3d',
  65. 'info_dict': {
  66. 'id': '4808130_part2',
  67. 'ext': 'flv',
  68. 'title': '【长篇】哆啦A梦443【钉铛】',
  69. 'description': '(2016.05.27)来组合客人的脸吧&amp;amp;寻母六千里锭 抱歉,又轮到周日上班现在才到家 封面www.pixiv.net/member_illust.php?mode=medium&amp;amp;illust_id=56912929',
  70. 'timestamp': 1464564180,
  71. 'upload_date': '20160529',
  72. 'uploader': '喜欢拉面',
  73. 'uploader_id': '151066',
  74. },
  75. }, {
  76. 'md5': '4b7b225b968402d7c32348c646f1fd83',
  77. 'info_dict': {
  78. 'id': '4808130_part3',
  79. 'ext': 'flv',
  80. 'title': '【长篇】哆啦A梦443【钉铛】',
  81. 'description': '(2016.05.27)来组合客人的脸吧&amp;amp;寻母六千里锭 抱歉,又轮到周日上班现在才到家 封面www.pixiv.net/member_illust.php?mode=medium&amp;amp;illust_id=56912929',
  82. 'timestamp': 1464564180,
  83. 'upload_date': '20160529',
  84. 'uploader': '喜欢拉面',
  85. 'uploader_id': '151066',
  86. },
  87. }, {
  88. 'md5': '7b795e214166501e9141139eea236e91',
  89. 'info_dict': {
  90. 'id': '4808130_part4',
  91. 'ext': 'flv',
  92. 'title': '【长篇】哆啦A梦443【钉铛】',
  93. 'description': '(2016.05.27)来组合客人的脸吧&amp;amp;寻母六千里锭 抱歉,又轮到周日上班现在才到家 封面www.pixiv.net/member_illust.php?mode=medium&amp;amp;illust_id=56912929',
  94. 'timestamp': 1464564180,
  95. 'upload_date': '20160529',
  96. 'uploader': '喜欢拉面',
  97. 'uploader_id': '151066',
  98. },
  99. }],
  100. }, {
  101. # Missing upload time
  102. 'url': 'http://www.bilibili.com/video/av1867637/',
  103. 'info_dict': {
  104. 'id': '2880301',
  105. 'ext': 'flv',
  106. 'title': '【HDTV】【喜剧】岳父岳母真难当 (2014)【法国票房冠军】',
  107. 'description': '一个信奉天主教的法国旧式传统资产阶级家庭中有四个女儿。三个女儿却分别找了阿拉伯、犹太、中国丈夫,老夫老妻唯独期盼剩下未嫁的小女儿能找一个信奉天主教的法国白人,结果没想到小女儿找了一位非裔黑人……【这次应该不会跳帧了】',
  108. 'uploader': '黑夜为猫',
  109. 'uploader_id': '610729',
  110. },
  111. 'params': {
  112. # Just to test metadata extraction
  113. 'skip_download': True,
  114. },
  115. 'expected_warnings': ['upload time'],
  116. }]
  117. # BiliBili blocks keys from time to time. The current key is extracted from
  118. # the Android client
  119. # TODO: find the sign algorithm used in the flash player
  120. _APP_KEY = '86385cdc024c0f6c'
  121. def _real_extract(self, url):
  122. mobj = re.match(self._VALID_URL, url)
  123. video_id = mobj.group('id')
  124. webpage = self._download_webpage(url, video_id)
  125. params = compat_parse_qs(self._search_regex(
  126. [r'EmbedPlayer\([^)]+,\s*"([^"]+)"\)',
  127. r'<iframe[^>]+src="https://secure\.bilibili\.com/secure,([^"]+)"'],
  128. webpage, 'player parameters'))
  129. cid = params['cid'][0]
  130. info_xml_str = self._download_webpage(
  131. 'http://interface.bilibili.com/v_cdn_play',
  132. cid, query={'appkey': self._APP_KEY, 'cid': cid},
  133. note='Downloading video info page')
  134. err_msg = None
  135. durls = None
  136. info_xml = None
  137. try:
  138. info_xml = compat_etree_fromstring(info_xml_str.encode('utf-8'))
  139. except compat_xml_parse_error:
  140. info_json = self._parse_json(info_xml_str, video_id, fatal=False)
  141. err_msg = (info_json or {}).get('error_text')
  142. else:
  143. err_msg = xpath_text(info_xml, './message')
  144. if info_xml is not None:
  145. durls = info_xml.findall('./durl')
  146. if not durls:
  147. if err_msg:
  148. raise ExtractorError('%s said: %s' % (self.IE_NAME, err_msg), expected=True)
  149. else:
  150. raise ExtractorError('No videos found!')
  151. entries = []
  152. for durl in durls:
  153. size = xpath_text(durl, ['./filesize', './size'])
  154. formats = [{
  155. 'url': durl.find('./url').text,
  156. 'filesize': int_or_none(size),
  157. }]
  158. for backup_url in durl.findall('./backup_url/url'):
  159. formats.append({
  160. 'url': backup_url.text,
  161. # backup URLs have lower priorities
  162. 'preference': -2 if 'hd.mp4' in backup_url.text else -3,
  163. })
  164. self._sort_formats(formats)
  165. entries.append({
  166. 'id': '%s_part%s' % (cid, xpath_text(durl, './order')),
  167. 'duration': int_or_none(xpath_text(durl, './length'), 1000),
  168. 'formats': formats,
  169. })
  170. title = self._html_search_regex('<h1[^>]+title="([^"]+)">', webpage, 'title')
  171. description = self._html_search_meta('description', webpage)
  172. datetime_str = self._html_search_regex(
  173. r'<time[^>]+datetime="([^"]+)"', webpage, 'upload time', fatal=False)
  174. timestamp = None
  175. if datetime_str:
  176. timestamp = calendar.timegm(datetime.datetime.strptime(datetime_str, '%Y-%m-%dT%H:%M').timetuple())
  177. # TODO 'view_count' requires deobfuscating Javascript
  178. info = {
  179. 'id': compat_str(cid),
  180. 'title': title,
  181. 'description': description,
  182. 'timestamp': timestamp,
  183. 'thumbnail': self._html_search_meta('thumbnailUrl', webpage),
  184. 'duration': float_or_none(xpath_text(info_xml, './timelength'), scale=1000),
  185. }
  186. uploader_mobj = re.search(
  187. r'<a[^>]+href="https?://space\.bilibili\.com/(?P<id>\d+)"[^>]+title="(?P<name>[^"]+)"',
  188. webpage)
  189. if uploader_mobj:
  190. info.update({
  191. 'uploader': uploader_mobj.group('name'),
  192. 'uploader_id': uploader_mobj.group('id'),
  193. })
  194. for entry in entries:
  195. entry.update(info)
  196. if len(entries) == 1:
  197. return entries[0]
  198. else:
  199. for idx, entry in enumerate(entries):
  200. entry['id'] = '%s_part%d' % (video_id, (idx + 1))
  201. return {
  202. '_type': 'multi_video',
  203. 'id': video_id,
  204. 'title': title,
  205. 'description': description,
  206. 'entries': entries,
  207. }