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.

99 lines
3.3 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. int_or_none,
  7. unified_strdate,
  8. ExtractorError,
  9. )
  10. class BiliBiliIE(InfoExtractor):
  11. _VALID_URL = r'http://www\.bilibili\.(?:tv|com)/video/av(?P<id>[0-9]+)/'
  12. _TEST = {
  13. 'url': 'http://www.bilibili.tv/video/av1074402/',
  14. 'md5': '2c301e4dab317596e837c3e7633e7d86',
  15. 'info_dict': {
  16. 'id': '1074402',
  17. 'ext': 'flv',
  18. 'title': '【金坷垃】金泡沫',
  19. 'duration': 308,
  20. 'upload_date': '20140420',
  21. 'thumbnail': 're:^https?://.+\.jpg',
  22. },
  23. }
  24. def _real_extract(self, url):
  25. video_id = self._match_id(url)
  26. webpage = self._download_webpage(url, video_id)
  27. if self._search_regex(r'(此视频不存在或被删除)', webpage, 'error message', default=None):
  28. raise ExtractorError('The video does not exist or was deleted', expected=True)
  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. cid = self._search_regex(r'cid=(\d+)', webpage, 'cid')
  50. lq_doc = self._download_xml(
  51. 'http://interface.bilibili.com/v_cdn_play?appkey=1&cid=%s' % cid,
  52. video_id,
  53. note='Downloading LQ video info'
  54. )
  55. lq_durl = lq_doc.find('./durl')
  56. formats = [{
  57. 'format_id': 'lq',
  58. 'quality': 1,
  59. 'url': lq_durl.find('./url').text,
  60. 'filesize': int_or_none(
  61. lq_durl.find('./size'), get_attr='text'),
  62. }]
  63. hq_doc = self._download_xml(
  64. 'http://interface.bilibili.com/playurl?appkey=1&cid=%s' % cid,
  65. video_id,
  66. note='Downloading HQ video info',
  67. fatal=False,
  68. )
  69. if hq_doc is not False:
  70. hq_durl = hq_doc.find('./durl')
  71. formats.append({
  72. 'format_id': 'hq',
  73. 'quality': 2,
  74. 'ext': 'flv',
  75. 'url': hq_durl.find('./url').text,
  76. 'filesize': int_or_none(
  77. hq_durl.find('./size'), get_attr='text'),
  78. })
  79. self._sort_formats(formats)
  80. return {
  81. 'id': video_id,
  82. 'title': title,
  83. 'formats': formats,
  84. 'duration': duration,
  85. 'upload_date': upload_date,
  86. 'thumbnail': thumbnail,
  87. }