bilibili.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. import json
  5. import xml.etree.ElementTree as ET
  6. from .common import InfoExtractor
  7. from ..utils import (
  8. int_or_none,
  9. unescapeHTML,
  10. ExtractorError,
  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. page = self._download_webpage(
  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. try:
  56. err_info = json.loads(page)
  57. raise ExtractorError(
  58. 'BiliBili said: ' + err_info['error_text'], expected=True)
  59. except ValueError:
  60. pass
  61. doc = ET.fromstring(page)
  62. entries = []
  63. for durl in doc.findall('./durl'):
  64. size = durl.find('./filesize|./size')
  65. formats = [{
  66. 'url': durl.find('./url').text,
  67. 'filesize': int_or_none(size.text) if size else None,
  68. 'ext': 'flv',
  69. }]
  70. backup_urls = durl.find('./backup_url')
  71. if backup_urls is not None:
  72. for backup_url in backup_urls.findall('./url'):
  73. formats.append({'url': backup_url.text})
  74. formats.reverse()
  75. entries.append({
  76. 'id': '%s_part%s' % (cid, durl.find('./order').text),
  77. 'title': title,
  78. 'duration': int_or_none(durl.find('./length').text) // 1000,
  79. 'formats': formats,
  80. })
  81. info = {
  82. 'id': str(cid),
  83. 'title': title,
  84. 'description': view_data.get('description'),
  85. 'thumbnail': view_data.get('pic'),
  86. 'uploader': view_data.get('author'),
  87. 'timestamp': int_or_none(view_data.get('created')),
  88. 'view_count': view_data.get('play'),
  89. 'duration': int_or_none(doc.find('./timelength').text),
  90. }
  91. if len(entries) == 1:
  92. entries[0].update(info)
  93. return entries[0]
  94. else:
  95. info.update({
  96. '_type': 'multi_video',
  97. 'id': video_id,
  98. 'entries': entries,
  99. })
  100. return info