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.

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