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.

123 lines
4.0 KiB

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