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.

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