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.

262 lines
10 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 (
  8. compat_HTTPError,
  9. compat_str,
  10. compat_urlparse,
  11. )
  12. from ..utils import (
  13. ExtractorError,
  14. int_or_none,
  15. remove_end,
  16. try_get,
  17. unified_strdate,
  18. update_url_query,
  19. USER_AGENTS,
  20. )
  21. class DPlayIE(InfoExtractor):
  22. _VALID_URL = r'https?://(?P<domain>www\.dplay\.(?:dk|se|no))/[^/]+/(?P<id>[^/?#]+)'
  23. _TESTS = [{
  24. # non geo restricted, via secure api, unsigned download hls URL
  25. 'url': 'http://www.dplay.se/nugammalt-77-handelser-som-format-sverige/season-1-svensken-lar-sig-njuta-av-livet/',
  26. 'info_dict': {
  27. 'id': '3172',
  28. 'display_id': 'season-1-svensken-lar-sig-njuta-av-livet',
  29. 'ext': 'mp4',
  30. 'title': 'Svensken lär sig njuta av livet',
  31. 'description': 'md5:d3819c9bccffd0fe458ca42451dd50d8',
  32. 'duration': 2650,
  33. 'timestamp': 1365454320,
  34. 'upload_date': '20130408',
  35. 'creator': 'Kanal 5 (Home)',
  36. 'series': 'Nugammalt - 77 händelser som format Sverige',
  37. 'season_number': 1,
  38. 'episode_number': 1,
  39. 'age_limit': 0,
  40. },
  41. }, {
  42. # geo restricted, via secure api, unsigned download hls URL
  43. 'url': 'http://www.dplay.dk/mig-og-min-mor/season-6-episode-12/',
  44. 'info_dict': {
  45. 'id': '70816',
  46. 'display_id': 'season-6-episode-12',
  47. 'ext': 'mp4',
  48. 'title': 'Episode 12',
  49. 'description': 'md5:9c86e51a93f8a4401fc9641ef9894c90',
  50. 'duration': 2563,
  51. 'timestamp': 1429696800,
  52. 'upload_date': '20150422',
  53. 'creator': 'Kanal 4 (Home)',
  54. 'series': 'Mig og min mor',
  55. 'season_number': 6,
  56. 'episode_number': 12,
  57. 'age_limit': 0,
  58. },
  59. }, {
  60. # geo restricted, via direct unsigned hls URL
  61. 'url': 'http://www.dplay.no/pga-tour/season-1-hoydepunkter-18-21-februar/',
  62. 'only_matching': True,
  63. }]
  64. def _real_extract(self, url):
  65. mobj = re.match(self._VALID_URL, url)
  66. display_id = mobj.group('id')
  67. domain = mobj.group('domain')
  68. webpage = self._download_webpage(url, display_id)
  69. video_id = self._search_regex(
  70. r'data-video-id=["\'](\d+)', webpage, 'video id')
  71. info = self._download_json(
  72. 'http://%s/api/v2/ajax/videos?video_id=%s' % (domain, video_id),
  73. video_id)['data'][0]
  74. title = info['title']
  75. PROTOCOLS = ('hls', 'hds')
  76. formats = []
  77. def extract_formats(protocol, manifest_url):
  78. if protocol == 'hls':
  79. m3u8_formats = self._extract_m3u8_formats(
  80. manifest_url, video_id, ext='mp4',
  81. entry_protocol='m3u8_native', m3u8_id=protocol, fatal=False)
  82. # Sometimes final URLs inside m3u8 are unsigned, let's fix this
  83. # ourselves. Also fragments' URLs are only served signed for
  84. # Safari user agent.
  85. query = compat_urlparse.parse_qs(compat_urlparse.urlparse(manifest_url).query)
  86. for m3u8_format in m3u8_formats:
  87. m3u8_format.update({
  88. 'url': update_url_query(m3u8_format['url'], query),
  89. 'http_headers': {
  90. 'User-Agent': USER_AGENTS['Safari'],
  91. },
  92. })
  93. formats.extend(m3u8_formats)
  94. elif protocol == 'hds':
  95. formats.extend(self._extract_f4m_formats(
  96. manifest_url + '&hdcore=3.8.0&plugin=flowplayer-3.8.0.0',
  97. video_id, f4m_id=protocol, fatal=False))
  98. domain_tld = domain.split('.')[-1]
  99. if domain_tld in ('se', 'dk', 'no'):
  100. for protocol in PROTOCOLS:
  101. # Providing dsc-geo allows to bypass geo restriction in some cases
  102. self._set_cookie(
  103. 'secure.dplay.%s' % domain_tld, 'dsc-geo',
  104. json.dumps({
  105. 'countryCode': domain_tld.upper(),
  106. 'expiry': (time.time() + 20 * 60) * 1000,
  107. }))
  108. stream = self._download_json(
  109. 'https://secure.dplay.%s/secure/api/v2/user/authorization/stream/%s?stream_type=%s'
  110. % (domain_tld, video_id, protocol), video_id,
  111. 'Downloading %s stream JSON' % protocol, fatal=False)
  112. if stream and stream.get(protocol):
  113. extract_formats(protocol, stream[protocol])
  114. # The last resort is to try direct unsigned hls/hds URLs from info dictionary.
  115. # Sometimes this does work even when secure API with dsc-geo has failed (e.g.
  116. # http://www.dplay.no/pga-tour/season-1-hoydepunkter-18-21-februar/).
  117. if not formats:
  118. for protocol in PROTOCOLS:
  119. if info.get(protocol):
  120. extract_formats(protocol, info[protocol])
  121. self._sort_formats(formats)
  122. subtitles = {}
  123. for lang in ('se', 'sv', 'da', 'nl', 'no'):
  124. for format_id in ('web_vtt', 'vtt', 'srt'):
  125. subtitle_url = info.get('subtitles_%s_%s' % (lang, format_id))
  126. if subtitle_url:
  127. subtitles.setdefault(lang, []).append({'url': subtitle_url})
  128. return {
  129. 'id': video_id,
  130. 'display_id': display_id,
  131. 'title': title,
  132. 'description': info.get('video_metadata_longDescription'),
  133. 'duration': int_or_none(info.get('video_metadata_length'), scale=1000),
  134. 'timestamp': int_or_none(info.get('video_publish_date')),
  135. 'creator': info.get('video_metadata_homeChannel'),
  136. 'series': info.get('video_metadata_show'),
  137. 'season_number': int_or_none(info.get('season')),
  138. 'episode_number': int_or_none(info.get('episode')),
  139. 'age_limit': int_or_none(info.get('minimum_age')),
  140. 'formats': formats,
  141. 'subtitles': subtitles,
  142. }
  143. class DPlayItIE(InfoExtractor):
  144. _VALID_URL = r'https?://it\.dplay\.com/[^/]+/[^/]+/(?P<id>[^/?#]+)'
  145. _GEO_COUNTRIES = ['IT']
  146. _TEST = {
  147. 'url': 'http://it.dplay.com/nove/biografie-imbarazzanti/luigi-di-maio-la-psicosi-di-stanislawskij/',
  148. 'md5': '2b808ffb00fc47b884a172ca5d13053c',
  149. 'info_dict': {
  150. 'id': '6918',
  151. 'display_id': 'luigi-di-maio-la-psicosi-di-stanislawskij',
  152. 'ext': 'mp4',
  153. 'title': 'Biografie imbarazzanti: Luigi Di Maio: la psicosi di Stanislawskij',
  154. 'description': 'md5:3c7a4303aef85868f867a26f5cc14813',
  155. 'thumbnail': r're:^https?://.*\.jpe?g',
  156. 'upload_date': '20160524',
  157. 'series': 'Biografie imbarazzanti',
  158. 'season_number': 1,
  159. 'episode': 'Luigi Di Maio: la psicosi di Stanislawskij',
  160. 'episode_number': 1,
  161. },
  162. }
  163. def _real_extract(self, url):
  164. display_id = self._match_id(url)
  165. webpage = self._download_webpage(url, display_id)
  166. title = remove_end(self._og_search_title(webpage), ' | Dplay')
  167. video_id = None
  168. info = self._search_regex(
  169. r'playback_json\s*:\s*JSON\.parse\s*\(\s*("(?:\\.|[^"\\])+?")',
  170. webpage, 'playback JSON', default=None)
  171. if info:
  172. for _ in range(2):
  173. info = self._parse_json(info, display_id, fatal=False)
  174. if not info:
  175. break
  176. else:
  177. video_id = try_get(info, lambda x: x['data']['id'])
  178. if not info:
  179. info_url = self._search_regex(
  180. r'url\s*[:=]\s*["\']((?:https?:)?//[^/]+/playback/videoPlaybackInfo/\d+)',
  181. webpage, 'info url')
  182. video_id = info_url.rpartition('/')[-1]
  183. try:
  184. info = self._download_json(
  185. info_url, display_id, headers={
  186. 'Authorization': 'Bearer %s' % self._get_cookies(url).get(
  187. 'dplayit_token').value,
  188. 'Referer': url,
  189. })
  190. except ExtractorError as e:
  191. if isinstance(e.cause, compat_HTTPError) and e.cause.code in (400, 403):
  192. info = self._parse_json(e.cause.read().decode('utf-8'), display_id)
  193. error = info['errors'][0]
  194. if error.get('code') == 'access.denied.geoblocked':
  195. self.raise_geo_restricted(
  196. msg=error.get('detail'), countries=self._GEO_COUNTRIES)
  197. raise ExtractorError(info['errors'][0]['detail'], expected=True)
  198. raise
  199. hls_url = info['data']['attributes']['streaming']['hls']['url']
  200. formats = self._extract_m3u8_formats(
  201. hls_url, display_id, ext='mp4', entry_protocol='m3u8_native',
  202. m3u8_id='hls')
  203. series = self._html_search_regex(
  204. r'(?s)<h1[^>]+class=["\'].*?\bshow_title\b.*?["\'][^>]*>(.+?)</h1>',
  205. webpage, 'series', fatal=False)
  206. episode = self._search_regex(
  207. r'<p[^>]+class=["\'].*?\bdesc_ep\b.*?["\'][^>]*>\s*<br/>\s*<b>([^<]+)',
  208. webpage, 'episode', fatal=False)
  209. mobj = re.search(
  210. r'(?s)<span[^>]+class=["\']dates["\'][^>]*>.+?\bS\.(?P<season_number>\d+)\s+E\.(?P<episode_number>\d+)\s*-\s*(?P<upload_date>\d{2}/\d{2}/\d{4})',
  211. webpage)
  212. if mobj:
  213. season_number = int(mobj.group('season_number'))
  214. episode_number = int(mobj.group('episode_number'))
  215. upload_date = unified_strdate(mobj.group('upload_date'))
  216. else:
  217. season_number = episode_number = upload_date = None
  218. return {
  219. 'id': compat_str(video_id or display_id),
  220. 'display_id': display_id,
  221. 'title': title,
  222. 'description': self._og_search_description(webpage),
  223. 'thumbnail': self._og_search_thumbnail(webpage),
  224. 'series': series,
  225. 'season_number': season_number,
  226. 'episode': episode,
  227. 'episode_number': episode_number,
  228. 'upload_date': upload_date,
  229. 'formats': formats,
  230. }