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.

110 lines
3.7 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..compat import compat_str
  6. from ..utils import (
  7. int_or_none,
  8. unescapeHTML,
  9. ExtractorError,
  10. xpath_text,
  11. )
  12. class BiliBiliIE(InfoExtractor):
  13. _VALID_URL = r'http://www\.bilibili\.(?:tv|com)/video/av(?P<id>\d+)(?:/index_(?P<page_num>\d+).html)?'
  14. _TESTS = [{
  15. 'url': 'http://www.bilibili.tv/video/av1074402/',
  16. 'md5': '2c301e4dab317596e837c3e7633e7d86',
  17. 'info_dict': {
  18. 'id': '1554319',
  19. 'ext': 'flv',
  20. 'title': '【金坷垃】金泡沫',
  21. 'duration': 308313,
  22. 'upload_date': '20140420',
  23. 'thumbnail': 're:^https?://.+\.jpg',
  24. 'description': 'md5:ce18c2a2d2193f0df2917d270f2e5923',
  25. 'timestamp': 1397983878,
  26. 'uploader': '菊子桑',
  27. },
  28. }, {
  29. 'url': 'http://www.bilibili.com/video/av1041170/',
  30. 'info_dict': {
  31. 'id': '1041170',
  32. 'title': '【BD1080P】刀语【诸神&异域】',
  33. 'description': '这是个神奇的故事~每个人不留弹幕不给走哦~切利哦!~',
  34. 'uploader': '枫叶逝去',
  35. 'timestamp': 1396501299,
  36. },
  37. 'playlist_count': 9,
  38. }]
  39. def _real_extract(self, url):
  40. mobj = re.match(self._VALID_URL, url)
  41. video_id = mobj.group('id')
  42. page_num = mobj.group('page_num') or '1'
  43. view_data = self._download_json(
  44. 'http://api.bilibili.com/view?type=json&appkey=8e9fc618fbd41e28&id=%s&page=%s' % (video_id, page_num),
  45. video_id)
  46. if 'error' in view_data:
  47. raise ExtractorError('%s said: %s' % (self.IE_NAME, view_data['error']), expected=True)
  48. cid = view_data['cid']
  49. title = unescapeHTML(view_data['title'])
  50. doc = self._download_xml(
  51. 'http://interface.bilibili.com/v_cdn_play?appkey=8e9fc618fbd41e28&cid=%s' % cid,
  52. cid,
  53. 'Downloading page %s/%s' % (page_num, view_data['pages'])
  54. )
  55. if xpath_text(doc, './result') == 'error':
  56. raise ExtractorError('%s said: %s' % (self.IE_NAME, xpath_text(doc, './message')), expected=True)
  57. entries = []
  58. for durl in doc.findall('./durl'):
  59. size = xpath_text(durl, ['./filesize', './size'])
  60. formats = [{
  61. 'url': durl.find('./url').text,
  62. 'filesize': int_or_none(size),
  63. 'ext': 'flv',
  64. }]
  65. backup_urls = durl.find('./backup_url')
  66. if backup_urls is not None:
  67. for backup_url in backup_urls.findall('./url'):
  68. formats.append({'url': backup_url.text})
  69. formats.reverse()
  70. entries.append({
  71. 'id': '%s_part%s' % (cid, xpath_text(durl, './order')),
  72. 'title': title,
  73. 'duration': int_or_none(xpath_text(durl, './length'), 1000),
  74. 'formats': formats,
  75. })
  76. info = {
  77. 'id': compat_str(cid),
  78. 'title': title,
  79. 'description': view_data.get('description'),
  80. 'thumbnail': view_data.get('pic'),
  81. 'uploader': view_data.get('author'),
  82. 'timestamp': int_or_none(view_data.get('created')),
  83. 'view_count': int_or_none(view_data.get('play')),
  84. 'duration': int_or_none(xpath_text(doc, './timelength')),
  85. }
  86. if len(entries) == 1:
  87. entries[0].update(info)
  88. return entries[0]
  89. else:
  90. info.update({
  91. '_type': 'multi_video',
  92. 'id': video_id,
  93. 'entries': entries,
  94. })
  95. return info