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.

135 lines
4.4 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. import itertools
  5. import json
  6. import xml.etree.ElementTree as ET
  7. from .common import InfoExtractor
  8. from ..utils import (
  9. int_or_none,
  10. unified_strdate,
  11. ExtractorError,
  12. )
  13. class BiliBiliIE(InfoExtractor):
  14. _VALID_URL = r'http://www\.bilibili\.(?:tv|com)/video/av(?P<id>[0-9]+)/'
  15. _TESTS = [{
  16. 'url': 'http://www.bilibili.tv/video/av1074402/',
  17. 'md5': '2c301e4dab317596e837c3e7633e7d86',
  18. 'info_dict': {
  19. 'id': '1074402_part1',
  20. 'ext': 'flv',
  21. 'title': '【金坷垃】金泡沫',
  22. 'duration': 308,
  23. 'upload_date': '20140420',
  24. 'thumbnail': 're:^https?://.+\.jpg',
  25. },
  26. }, {
  27. 'url': 'http://www.bilibili.com/video/av1041170/',
  28. 'info_dict': {
  29. 'id': '1041170',
  30. 'title': '【BD1080P】刀语【诸神&异域】',
  31. },
  32. 'playlist_count': 9,
  33. }]
  34. def _real_extract(self, url):
  35. video_id = self._match_id(url)
  36. webpage = self._download_webpage(url, video_id)
  37. if self._search_regex(r'(此视频不存在或被删除)', webpage, 'error message', default=None):
  38. raise ExtractorError('The video does not exist or was deleted', expected=True)
  39. video_code = self._search_regex(
  40. r'(?s)<div itemprop="video".*?>(.*?)</div>', webpage, 'video code')
  41. title = self._html_search_meta(
  42. 'media:title', video_code, 'title', fatal=True)
  43. duration_str = self._html_search_meta(
  44. 'duration', video_code, 'duration')
  45. if duration_str is None:
  46. duration = None
  47. else:
  48. duration_mobj = re.match(
  49. r'^T(?:(?P<hours>[0-9]+)H)?(?P<minutes>[0-9]+)M(?P<seconds>[0-9]+)S$',
  50. duration_str)
  51. duration = (
  52. int_or_none(duration_mobj.group('hours'), default=0) * 3600 +
  53. int(duration_mobj.group('minutes')) * 60 +
  54. int(duration_mobj.group('seconds')))
  55. upload_date = unified_strdate(self._html_search_meta(
  56. 'uploadDate', video_code, fatal=False))
  57. thumbnail = self._html_search_meta(
  58. 'thumbnailUrl', video_code, 'thumbnail', fatal=False)
  59. cid = self._search_regex(r'cid=(\d+)', webpage, 'cid')
  60. entries = []
  61. lq_page = self._download_webpage(
  62. 'http://interface.bilibili.com/v_cdn_play?appkey=1&cid=%s' % cid,
  63. video_id,
  64. note='Downloading LQ video info'
  65. )
  66. try:
  67. err_info = json.loads(lq_page)
  68. raise ExtractorError(
  69. 'BiliBili said: ' + err_info['error_text'], expected=True)
  70. except ValueError:
  71. pass
  72. lq_doc = ET.fromstring(lq_page)
  73. lq_durls = lq_doc.findall('./durl')
  74. hq_doc = self._download_xml(
  75. 'http://interface.bilibili.com/playurl?appkey=1&cid=%s' % cid,
  76. video_id,
  77. note='Downloading HQ video info',
  78. fatal=False,
  79. )
  80. if hq_doc is not False:
  81. hq_durls = hq_doc.findall('./durl')
  82. assert len(lq_durls) == len(hq_durls)
  83. else:
  84. hq_durls = itertools.repeat(None)
  85. i = 1
  86. for lq_durl, hq_durl in zip(lq_durls, hq_durls):
  87. formats = [{
  88. 'format_id': 'lq',
  89. 'quality': 1,
  90. 'url': lq_durl.find('./url').text,
  91. 'filesize': int_or_none(
  92. lq_durl.find('./size'), get_attr='text'),
  93. }]
  94. if hq_durl is not None:
  95. formats.append({
  96. 'format_id': 'hq',
  97. 'quality': 2,
  98. 'ext': 'flv',
  99. 'url': hq_durl.find('./url').text,
  100. 'filesize': int_or_none(
  101. hq_durl.find('./size'), get_attr='text'),
  102. })
  103. self._sort_formats(formats)
  104. entries.append({
  105. 'id': '%s_part%d' % (video_id, i),
  106. 'title': title,
  107. 'formats': formats,
  108. 'duration': duration,
  109. 'upload_date': upload_date,
  110. 'thumbnail': thumbnail,
  111. })
  112. i += 1
  113. return {
  114. '_type': 'multi_video',
  115. 'entries': entries,
  116. 'id': video_id,
  117. 'title': title
  118. }