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.

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