drbonanza.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. from __future__ import unicode_literals
  2. from .common import InfoExtractor
  3. from .common import ExtractorError
  4. from ..utils import parse_iso8601
  5. import json
  6. import re
  7. class DRBonanzaIE(InfoExtractor):
  8. _VALID_URL = r'https?://(?:www\.)?dr\.dk/bonanza/(?:[^/]+/)+(?:[^/])+?(?:assetId=(?P<id>\d+))?(?:[#&]|$)'
  9. _TESTS = [{
  10. 'url': 'http://www.dr.dk/bonanza/serie/portraetter/Talkshowet.htm?assetId=65517',
  11. 'md5': 'fe330252ddea607635cf2eb2c99a0af3',
  12. 'info_dict': {
  13. 'id': '65517',
  14. 'ext': 'mp4',
  15. 'title': 'Talkshowet - Leonard Cohen',
  16. 'description': 'md5:8f34194fb30cd8c8a30ad8b27b70c0ca',
  17. 'timestamp': 1295537932,
  18. 'upload_date': '20110120',
  19. 'duration': 3664000,
  20. },
  21. },{
  22. 'url': 'http://www.dr.dk/bonanza/radio/serie/sport/fodbold.htm?assetId=59410',
  23. 'md5': '6dfe039417e76795fb783c52da3de11d',
  24. 'info_dict': {
  25. 'id': '59410',
  26. 'ext': 'mp3',
  27. 'title': 'EM fodbold 1992 Danmark - Tyskland finale Transmission',
  28. 'description': 'md5:501e5a195749480552e214fbbed16c4e',
  29. 'timestamp': 1223274900,
  30. 'upload_date': '20081006',
  31. 'duration': 7369000,
  32. },
  33. }]
  34. def _real_extract(self, url):
  35. url_id = self._match_id(url)
  36. webpage = self._download_webpage(url, url_id if url_id else "")
  37. if url_id:
  38. info = json.loads(self._html_search_regex(r'({.*?' + url_id + '.*})', webpage, 'json'))
  39. else:
  40. # Just fetch the first video on that page
  41. info = json.loads(self._html_search_regex(r'bonanzaFunctions.newPlaylist\(({.*})\)', webpage, 'json'))
  42. asset_id = str(info['AssetId'])
  43. title = info['Title'].rstrip(' \'\"-,.:;!?')
  44. duration = info['Duration']
  45. timestamp = parse_iso8601(re.sub(r'\.\d+$', '', info['Created'])) # First published online. "FirstPublished" contains the date for original airing.
  46. def parse_filename_info(url):
  47. match = re.search(r'/\d+_(?P<width>\d+)x(?P<height>\d+)x(?P<bitrate>\d+)K\.(?P<ext>\w+)$', url)
  48. if match:
  49. return {'width': int(match.group(1)), 'height': int(match.group(2)), 'bitrate': int(match.group(3)), 'ext': match.group(4)}
  50. match = re.search(r'/\d+_(?P<bitrate>\d+)K\.(?P<ext>\w+)$', url)
  51. if match:
  52. return {'bitrate': int(match.group(1)), 'ext': match.group(2)}
  53. return {'width': None, 'height': None, 'bitrate': None, 'ext': None}
  54. video_types = ['VideoHigh', 'VideoMid', 'VideoLow']
  55. preferencemap = {
  56. 'VideoHigh': -1,
  57. 'VideoMid': -2,
  58. 'VideoLow': -3,
  59. 'Audio': -4,
  60. }
  61. formats = []
  62. for file in info['Files']:
  63. if info['Type'] == "Video":
  64. if file['Type'] in video_types:
  65. fileinfo = parse_filename_info(file['Location'])
  66. formats.append({
  67. 'url': file['Location'],
  68. 'format_id': file['Type'].replace('Video', ''),
  69. 'preference': preferencemap.get(file['Type'], -10),
  70. 'width': fileinfo['width'],
  71. 'height': fileinfo['height'],
  72. 'vbr': fileinfo['bitrate'],
  73. 'ext': fileinfo['ext'],
  74. })
  75. elif file['Type'] == "Thumb":
  76. thumbnail = file['Location']
  77. elif info['Type'] == "Audio":
  78. if file['Type'] == "Audio":
  79. fileinfo = parse_filename_info(file['Location'])
  80. formats.append({
  81. 'url': file['Location'],
  82. 'format_id': file['Type'],
  83. 'abr': fileinfo['bitrate'],
  84. 'ext': fileinfo['ext'],
  85. 'vcodec': 'none',
  86. })
  87. elif file['Type'] == "Thumb":
  88. thumbnail = file['Location']
  89. description = "{}\n{}\n{}\n".format(info['Description'], info['Actors'], info['Colophon'])
  90. for f in formats:
  91. f['url'] = f['url'].replace('rtmp://vod-bonanza.gss.dr.dk/bonanza/', 'http://vodfiles.dr.dk/')
  92. f['url'] = f['url'].replace('mp4:bonanza', 'bonanza')
  93. self._sort_formats(formats)
  94. display_id = re.sub(r'[^\w\d-]', '', re.sub(r' ', '-', title.lower())) + '-' + asset_id
  95. display_id = re.sub(r'-+', '-', display_id)
  96. return {
  97. 'id': asset_id,
  98. 'display_id': display_id,
  99. 'title': title,
  100. 'formats': formats,
  101. 'description': description,
  102. 'thumbnail': thumbnail,
  103. 'timestamp': timestamp,
  104. 'duration': duration,
  105. }