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.

106 lines
3.6 KiB

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