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.

163 lines
6.4 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import json
  4. import re
  5. import time
  6. from .common import InfoExtractor
  7. from ..compat import compat_urlparse
  8. from ..utils import (
  9. int_or_none,
  10. update_url_query,
  11. )
  12. class DPlayIE(InfoExtractor):
  13. _VALID_URL = r'https?://(?P<domain>it\.dplay\.com|www\.dplay\.(?:dk|se|no))/[^/]+/(?P<id>[^/?#]+)'
  14. _TESTS = [{
  15. # geo restricted, via direct unsigned hls URL
  16. 'url': 'http://it.dplay.com/take-me-out/stagione-1-episodio-25/',
  17. 'info_dict': {
  18. 'id': '1255600',
  19. 'display_id': 'stagione-1-episodio-25',
  20. 'ext': 'mp4',
  21. 'title': 'Episodio 25',
  22. 'description': 'md5:cae5f40ad988811b197d2d27a53227eb',
  23. 'duration': 2761,
  24. 'timestamp': 1454701800,
  25. 'upload_date': '20160205',
  26. 'creator': 'RTIT',
  27. 'series': 'Take me out',
  28. 'season_number': 1,
  29. 'episode_number': 25,
  30. 'age_limit': 0,
  31. },
  32. 'expected_warnings': ['Unable to download f4m manifest'],
  33. }, {
  34. # non geo restricted, via secure api, unsigned download hls URL
  35. 'url': 'http://www.dplay.se/nugammalt-77-handelser-som-format-sverige/season-1-svensken-lar-sig-njuta-av-livet/',
  36. 'info_dict': {
  37. 'id': '3172',
  38. 'display_id': 'season-1-svensken-lar-sig-njuta-av-livet',
  39. 'ext': 'mp4',
  40. 'title': 'Svensken lär sig njuta av livet',
  41. 'description': 'md5:d3819c9bccffd0fe458ca42451dd50d8',
  42. 'duration': 2650,
  43. 'timestamp': 1365454320,
  44. 'upload_date': '20130408',
  45. 'creator': 'Kanal 5 (Home)',
  46. 'series': 'Nugammalt - 77 händelser som format Sverige',
  47. 'season_number': 1,
  48. 'episode_number': 1,
  49. 'age_limit': 0,
  50. },
  51. }, {
  52. # geo restricted, via secure api, unsigned download hls URL
  53. 'url': 'http://www.dplay.dk/mig-og-min-mor/season-6-episode-12/',
  54. 'info_dict': {
  55. 'id': '70816',
  56. 'display_id': 'season-6-episode-12',
  57. 'ext': 'mp4',
  58. 'title': 'Episode 12',
  59. 'description': 'md5:9c86e51a93f8a4401fc9641ef9894c90',
  60. 'duration': 2563,
  61. 'timestamp': 1429696800,
  62. 'upload_date': '20150422',
  63. 'creator': 'Kanal 4 (Home)',
  64. 'series': 'Mig og min mor',
  65. 'season_number': 6,
  66. 'episode_number': 12,
  67. 'age_limit': 0,
  68. },
  69. }, {
  70. # geo restricted, via direct unsigned hls URL
  71. 'url': 'http://www.dplay.no/pga-tour/season-1-hoydepunkter-18-21-februar/',
  72. 'only_matching': True,
  73. }]
  74. def _real_extract(self, url):
  75. mobj = re.match(self._VALID_URL, url)
  76. display_id = mobj.group('id')
  77. domain = mobj.group('domain')
  78. webpage = self._download_webpage(url, display_id)
  79. video_id = self._search_regex(
  80. r'data-video-id=["\'](\d+)', webpage, 'video id')
  81. info = self._download_json(
  82. 'http://%s/api/v2/ajax/videos?video_id=%s' % (domain, video_id),
  83. video_id)['data'][0]
  84. title = info['title']
  85. PROTOCOLS = ('hls', 'hds')
  86. formats = []
  87. def extract_formats(protocol, manifest_url):
  88. if protocol == 'hls':
  89. m3u8_formats = self._extract_m3u8_formats(
  90. manifest_url, video_id, ext='mp4',
  91. entry_protocol='m3u8_native', m3u8_id=protocol, fatal=False)
  92. # Sometimes final URLs inside m3u8 are unsigned, let's fix this
  93. # ourselves
  94. query = compat_urlparse.parse_qs(compat_urlparse.urlparse(manifest_url).query)
  95. for m3u8_format in m3u8_formats:
  96. m3u8_format['url'] = update_url_query(m3u8_format['url'], query)
  97. formats.extend(m3u8_formats)
  98. elif protocol == 'hds':
  99. formats.extend(self._extract_f4m_formats(
  100. manifest_url + '&hdcore=3.8.0&plugin=flowplayer-3.8.0.0',
  101. video_id, f4m_id=protocol, fatal=False))
  102. domain_tld = domain.split('.')[-1]
  103. if domain_tld in ('se', 'dk', 'no'):
  104. for protocol in PROTOCOLS:
  105. # Providing dsc-geo allows to bypass geo restriction in some cases
  106. self._set_cookie(
  107. 'secure.dplay.%s' % domain_tld, 'dsc-geo',
  108. json.dumps({
  109. 'countryCode': domain_tld.upper(),
  110. 'expiry': (time.time() + 20 * 60) * 1000,
  111. }))
  112. stream = self._download_json(
  113. 'https://secure.dplay.%s/secure/api/v2/user/authorization/stream/%s?stream_type=%s'
  114. % (domain_tld, video_id, protocol), video_id,
  115. 'Downloading %s stream JSON' % protocol, fatal=False)
  116. if stream and stream.get(protocol):
  117. extract_formats(protocol, stream[protocol])
  118. # The last resort is to try direct unsigned hls/hds URLs from info dictionary.
  119. # Sometimes this does work even when secure API with dsc-geo has failed (e.g.
  120. # http://www.dplay.no/pga-tour/season-1-hoydepunkter-18-21-februar/).
  121. if not formats:
  122. for protocol in PROTOCOLS:
  123. if info.get(protocol):
  124. extract_formats(protocol, info[protocol])
  125. self._sort_formats(formats)
  126. subtitles = {}
  127. for lang in ('se', 'sv', 'da', 'nl', 'no'):
  128. for format_id in ('web_vtt', 'vtt', 'srt'):
  129. subtitle_url = info.get('subtitles_%s_%s' % (lang, format_id))
  130. if subtitle_url:
  131. subtitles.setdefault(lang, []).append({'url': subtitle_url})
  132. return {
  133. 'id': video_id,
  134. 'display_id': display_id,
  135. 'title': title,
  136. 'description': info.get('video_metadata_longDescription'),
  137. 'duration': int_or_none(info.get('video_metadata_length'), scale=1000),
  138. 'timestamp': int_or_none(info.get('video_publish_date')),
  139. 'creator': info.get('video_metadata_homeChannel'),
  140. 'series': info.get('video_metadata_show'),
  141. 'season_number': int_or_none(info.get('season')),
  142. 'episode_number': int_or_none(info.get('episode')),
  143. 'age_limit': int_or_none(info.get('minimum_age')),
  144. 'formats': formats,
  145. 'subtitles': subtitles,
  146. }