bilibili.py 3.7 KB

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