bilibili.py 3.8 KB

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