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.

105 lines
3.5 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..compat import compat_parse_qs
  6. from ..utils import (
  7. ExtractorError,
  8. int_or_none,
  9. unified_strdate,
  10. )
  11. class BiliBiliIE(InfoExtractor):
  12. _VALID_URL = r'http://www\.bilibili\.(?:tv|com)/video/av(?P<id>[0-9]+)/'
  13. _TEST = {
  14. 'url': 'http://www.bilibili.tv/video/av1074402/',
  15. 'md5': '2c301e4dab317596e837c3e7633e7d86',
  16. 'info_dict': {
  17. 'id': '1074402',
  18. 'ext': 'flv',
  19. 'title': '【金坷垃】金泡沫',
  20. 'duration': 308,
  21. 'upload_date': '20140420',
  22. 'thumbnail': 're:^https?://.+\.jpg',
  23. },
  24. }
  25. def _real_extract(self, url):
  26. video_id = self._match_id(url)
  27. webpage = self._download_webpage(url, video_id)
  28. video_code = self._search_regex(
  29. r'(?s)<div itemprop="video".*?>(.*?)</div>', webpage, 'video code')
  30. title = self._html_search_meta(
  31. 'media:title', video_code, 'title', fatal=True)
  32. duration_str = self._html_search_meta(
  33. 'duration', video_code, 'duration')
  34. if duration_str is None:
  35. duration = None
  36. else:
  37. duration_mobj = re.match(
  38. r'^T(?:(?P<hours>[0-9]+)H)?(?P<minutes>[0-9]+)M(?P<seconds>[0-9]+)S$',
  39. duration_str)
  40. duration = (
  41. int_or_none(duration_mobj.group('hours'), default=0) * 3600 +
  42. int(duration_mobj.group('minutes')) * 60 +
  43. int(duration_mobj.group('seconds')))
  44. upload_date = unified_strdate(self._html_search_meta(
  45. 'uploadDate', video_code, fatal=False))
  46. thumbnail = self._html_search_meta(
  47. 'thumbnailUrl', video_code, 'thumbnail', fatal=False)
  48. player_params = compat_parse_qs(self._html_search_regex(
  49. r'<iframe .*?class="player" src="https://secure\.bilibili\.(?:tv|com)/secure,([^"]+)"',
  50. webpage, 'player params'))
  51. if 'cid' in player_params:
  52. cid = player_params['cid'][0]
  53. lq_doc = self._download_xml(
  54. 'http://interface.bilibili.cn/v_cdn_play?cid=%s' % cid,
  55. video_id,
  56. note='Downloading LQ video info'
  57. )
  58. lq_durl = lq_doc.find('.//durl')
  59. formats = [{
  60. 'format_id': 'lq',
  61. 'quality': 1,
  62. 'url': lq_durl.find('./url').text,
  63. 'filesize': int_or_none(
  64. lq_durl.find('./size'), get_attr='text'),
  65. }]
  66. hq_doc = self._download_xml(
  67. 'http://interface.bilibili.cn/playurl?cid=%s' % cid,
  68. video_id,
  69. note='Downloading HQ video info',
  70. fatal=False,
  71. )
  72. if hq_doc is not False:
  73. hq_durl = hq_doc.find('.//durl')
  74. formats.append({
  75. 'format_id': 'hq',
  76. 'quality': 2,
  77. 'ext': 'flv',
  78. 'url': hq_durl.find('./url').text,
  79. 'filesize': int_or_none(
  80. hq_durl.find('./size'), get_attr='text'),
  81. })
  82. else:
  83. raise ExtractorError('Unsupported player parameters: %r' % (player_params,))
  84. self._sort_formats(formats)
  85. return {
  86. 'id': video_id,
  87. 'title': title,
  88. 'formats': formats,
  89. 'duration': duration,
  90. 'upload_date': upload_date,
  91. 'thumbnail': thumbnail,
  92. }