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.

131 lines
4.8 KiB

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