bilibili.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import json
  4. import xml.etree.ElementTree as ET
  5. from .common import InfoExtractor
  6. from ..utils import (
  7. int_or_none,
  8. ExtractorError,
  9. )
  10. class BiliBiliIE(InfoExtractor):
  11. _VALID_URL = r'http://www\.bilibili\.(?:tv|com)/video/av(?P<id>[0-9]+)/'
  12. _TESTS = [{
  13. 'url': 'http://www.bilibili.tv/video/av1074402/',
  14. 'md5': '2c301e4dab317596e837c3e7633e7d86',
  15. 'info_dict': {
  16. 'id': '1554319',
  17. 'ext': 'flv',
  18. 'title': '【金坷垃】金泡沫',
  19. 'duration': 308313,
  20. 'upload_date': '20140420',
  21. 'thumbnail': 're:^https?://.+\.jpg',
  22. 'description': 'md5:ce18c2a2d2193f0df2917d270f2e5923',
  23. 'timestamp': 1397983878,
  24. 'uploader': '菊子桑',
  25. },
  26. }, {
  27. 'url': 'http://www.bilibili.com/video/av1041170/',
  28. 'info_dict': {
  29. 'id': '1041170',
  30. 'title': '【BD1080P】刀语【诸神&异域】',
  31. },
  32. 'playlist_count': 12,
  33. }]
  34. def _extract_video_info(self, cid, view_data, page_num=1, num_pages=1):
  35. title = view_data['title']
  36. page = self._download_webpage(
  37. 'http://interface.bilibili.com/v_cdn_play?appkey=8e9fc618fbd41e28&cid=%s' % cid,
  38. cid,
  39. 'Downloading page %d/%d' % (page_num, num_pages)
  40. )
  41. try:
  42. err_info = json.loads(page)
  43. raise ExtractorError(
  44. 'BiliBili said: ' + err_info['error_text'], expected=True)
  45. except ValueError:
  46. pass
  47. doc = ET.fromstring(page)
  48. durls = doc.findall('./durl')
  49. entries = []
  50. for durl in durls:
  51. entries.append({
  52. 'id': '%s_part%s' % (cid, durl.find('./order').text),
  53. 'title': title,
  54. 'url': durl.find('./url').text,
  55. 'filesize': int_or_none(durl.find('./filesize').text),
  56. 'ext': 'flv',
  57. 'duration': int_or_none(durl.find('./length').text) // 1000,
  58. })
  59. info = {
  60. 'id': cid,
  61. 'title': title,
  62. 'description': view_data.get('description'),
  63. 'thumbnail': view_data.get('pic'),
  64. 'uploader': view_data.get('author'),
  65. 'timestamp': int_or_none(view_data.get('created')),
  66. 'view_count': view_data.get('play'),
  67. 'duration': int_or_none(doc.find('./timelength').text),
  68. }
  69. if len(entries) == 1:
  70. entries[0].update(info)
  71. return entries[0]
  72. else:
  73. info.update({
  74. '_type': 'multi_video',
  75. 'entries': entries,
  76. })
  77. return info
  78. def _real_extract(self, url):
  79. video_id = self._match_id(url)
  80. view_data = self._download_json('http://api.bilibili.com/view?type=json&appkey=8e9fc618fbd41e28&id=%s' % video_id, video_id)
  81. num_pages = int_or_none(view_data['pages'])
  82. if num_pages > 1:
  83. play_list_title = view_data['title']
  84. page_list = self._download_json('http://www.bilibili.com/widget/getPageList?aid=%s' % video_id, video_id, 'Downloading page list metadata')
  85. entries = []
  86. for page in page_list:
  87. view_data['title'] = page['pagename']
  88. entries.append(self._extract_video_info(str(page['cid']), view_data, page['page'], num_pages))
  89. return self.playlist_result(entries, video_id, play_list_title, view_data.get('description'))
  90. else:
  91. return self._extract_video_info(str(view_data['cid']), view_data)