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.

287 lines
11 KiB

  1. # encoding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..compat import compat_urllib_parse_unquote
  6. from ..utils import (
  7. ExtractorError,
  8. int_or_none,
  9. parse_age_limit,
  10. parse_duration,
  11. )
  12. class NRKBaseIE(InfoExtractor):
  13. def _extract_formats(self, manifest_url, video_id, fatal=True):
  14. formats = []
  15. formats.extend(self._extract_f4m_formats(
  16. manifest_url + '?hdcore=3.5.0&plugin=aasp-3.5.0.151.81',
  17. video_id, f4m_id='hds', fatal=fatal))
  18. formats.extend(self._extract_m3u8_formats(manifest_url.replace(
  19. 'akamaihd.net/z/', 'akamaihd.net/i/').replace('/manifest.f4m', '/master.m3u8'),
  20. video_id, 'mp4', 'm3u8_native', m3u8_id='hls', fatal=fatal))
  21. return formats
  22. def _real_extract(self, url):
  23. video_id = self._match_id(url)
  24. data = self._download_json(
  25. 'http://%s/mediaelement/%s' % (self._API_HOST, video_id),
  26. video_id, 'Downloading mediaelement JSON')
  27. title = data.get('fullTitle') or data.get('mainTitle') or data['title']
  28. video_id = data.get('id') or video_id
  29. entries = []
  30. media_assets = data.get('mediaAssets')
  31. if media_assets and isinstance(media_assets, list):
  32. def video_id_and_title(idx):
  33. return ((video_id, title) if len(media_assets) == 1
  34. else ('%s-%d' % (video_id, idx), '%s (Part %d)' % (title, idx)))
  35. for num, asset in enumerate(media_assets, 1):
  36. asset_url = asset.get('url')
  37. if not asset_url:
  38. continue
  39. formats = self._extract_formats(asset_url, video_id, fatal=False)
  40. if not formats:
  41. continue
  42. self._sort_formats(formats)
  43. entry_id, entry_title = video_id_and_title(num)
  44. duration = parse_duration(asset.get('duration'))
  45. subtitles = {}
  46. for subtitle in ('webVtt', 'timedText'):
  47. subtitle_url = asset.get('%sSubtitlesUrl' % subtitle)
  48. if subtitle_url:
  49. subtitles.setdefault('no', []).append({'url': subtitle_url})
  50. entries.append({
  51. 'id': asset.get('carrierId') or entry_id,
  52. 'title': entry_title,
  53. 'duration': duration,
  54. 'subtitles': subtitles,
  55. 'formats': formats,
  56. })
  57. if not entries:
  58. media_url = data.get('mediaUrl')
  59. if media_url:
  60. formats = self._extract_formats(media_url, video_id)
  61. self._sort_formats(formats)
  62. duration = parse_duration(data.get('duration'))
  63. entries = [{
  64. 'id': video_id,
  65. 'title': title,
  66. 'duration': duration,
  67. 'formats': formats,
  68. }]
  69. if not entries:
  70. if data.get('usageRights', {}).get('isGeoBlocked'):
  71. raise ExtractorError(
  72. 'NRK har ikke rettigheter til å vise dette programmet utenfor Norge',
  73. expected=True)
  74. conviva = data.get('convivaStatistics') or {}
  75. series = conviva.get('seriesName') or data.get('seriesTitle')
  76. episode = conviva.get('episodeName') or data.get('episodeNumberOrDate')
  77. thumbnails = None
  78. images = data.get('images')
  79. if images and isinstance(images, dict):
  80. web_images = images.get('webImages')
  81. if isinstance(web_images, list):
  82. thumbnails = [{
  83. 'url': image['imageUrl'],
  84. 'width': int_or_none(image.get('width')),
  85. 'height': int_or_none(image.get('height')),
  86. } for image in web_images if image.get('imageUrl')]
  87. description = data.get('description')
  88. common_info = {
  89. 'description': description,
  90. 'series': series,
  91. 'episode': episode,
  92. 'age_limit': parse_age_limit(data.get('legalAge')),
  93. 'thumbnails': thumbnails,
  94. }
  95. vcodec = 'none' if data.get('mediaType') == 'Audio' else None
  96. # TODO: extract chapters when https://github.com/rg3/youtube-dl/pull/9409 is merged
  97. for entry in entries:
  98. entry.update(common_info)
  99. for f in entry['formats']:
  100. f['vcodec'] = vcodec
  101. return self.playlist_result(entries, video_id, title, description)
  102. class NRKIE(NRKBaseIE):
  103. _VALID_URL = r'(?:nrk:|https?://(?:www\.)?nrk\.no/video/PS\*)(?P<id>\d+)'
  104. _API_HOST = 'v8.psapi.nrk.no'
  105. _TESTS = [{
  106. # video
  107. 'url': 'http://www.nrk.no/video/PS*150533',
  108. 'md5': '2f7f6eeb2aacdd99885f355428715cfa',
  109. 'info_dict': {
  110. 'id': '150533',
  111. 'ext': 'mp4',
  112. 'title': 'Dompap og andre fugler i Piip-Show',
  113. 'description': 'md5:d9261ba34c43b61c812cb6b0269a5c8f',
  114. 'duration': 263,
  115. }
  116. }, {
  117. # audio
  118. 'url': 'http://www.nrk.no/video/PS*154915',
  119. # MD5 is unstable
  120. 'info_dict': {
  121. 'id': '154915',
  122. 'ext': 'flv',
  123. 'title': 'Slik høres internett ut når du er blind',
  124. 'description': 'md5:a621f5cc1bd75c8d5104cb048c6b8568',
  125. 'duration': 20,
  126. }
  127. }]
  128. class NRKTVIE(NRKBaseIE):
  129. IE_DESC = 'NRK TV and NRK Radio'
  130. _VALID_URL = r'https?://(?:tv|radio)\.nrk(?:super)?\.no/(?:serie/[^/]+|program)/(?P<id>[a-zA-Z]{4}\d{8})(?:/\d{2}-\d{2}-\d{4})?(?:#del=(?P<part_id>\d+))?'
  131. _API_HOST = 'psapi-we.nrk.no'
  132. _TESTS = [{
  133. 'url': 'https://tv.nrk.no/serie/20-spoersmaal-tv/MUHH48000314/23-05-2014',
  134. 'md5': '4e9ca6629f09e588ed240fb11619922a',
  135. 'info_dict': {
  136. 'id': 'MUHH48000314AA',
  137. 'ext': 'mp4',
  138. 'title': '20 spørsmål 23.05.2014',
  139. 'description': 'md5:bdea103bc35494c143c6a9acdd84887a',
  140. 'duration': 1741.52,
  141. },
  142. }, {
  143. 'url': 'https://tv.nrk.no/program/mdfp15000514',
  144. 'md5': '43d0be26663d380603a9cf0c24366531',
  145. 'info_dict': {
  146. 'id': 'MDFP15000514CA',
  147. 'ext': 'mp4',
  148. 'title': 'Grunnlovsjubiléet - Stor ståhei for ingenting 24.05.2014',
  149. 'description': 'md5:89290c5ccde1b3a24bb8050ab67fe1db',
  150. 'duration': 4605.08,
  151. },
  152. }, {
  153. # single playlist video
  154. 'url': 'https://tv.nrk.no/serie/tour-de-ski/MSPO40010515/06-01-2015#del=2',
  155. 'md5': 'adbd1dbd813edaf532b0a253780719c2',
  156. 'info_dict': {
  157. 'id': 'MSPO40010515-part2',
  158. 'ext': 'flv',
  159. 'title': 'Tour de Ski: Sprint fri teknikk, kvinner og menn 06.01.2015 (del 2:2)',
  160. 'description': 'md5:238b67b97a4ac7d7b4bf0edf8cc57d26',
  161. },
  162. 'skip': 'Only works from Norway',
  163. }, {
  164. 'url': 'https://tv.nrk.no/serie/tour-de-ski/MSPO40010515/06-01-2015',
  165. 'playlist': [{
  166. 'md5': '9480285eff92d64f06e02a5367970a7a',
  167. 'info_dict': {
  168. 'id': 'MSPO40010515-part1',
  169. 'ext': 'flv',
  170. 'title': 'Tour de Ski: Sprint fri teknikk, kvinner og menn 06.01.2015 (del 1:2)',
  171. 'description': 'md5:238b67b97a4ac7d7b4bf0edf8cc57d26',
  172. },
  173. }, {
  174. 'md5': 'adbd1dbd813edaf532b0a253780719c2',
  175. 'info_dict': {
  176. 'id': 'MSPO40010515-part2',
  177. 'ext': 'flv',
  178. 'title': 'Tour de Ski: Sprint fri teknikk, kvinner og menn 06.01.2015 (del 2:2)',
  179. 'description': 'md5:238b67b97a4ac7d7b4bf0edf8cc57d26',
  180. },
  181. }],
  182. 'info_dict': {
  183. 'id': 'MSPO40010515',
  184. 'title': 'Tour de Ski: Sprint fri teknikk, kvinner og menn',
  185. 'description': 'md5:238b67b97a4ac7d7b4bf0edf8cc57d26',
  186. 'duration': 6947.52,
  187. },
  188. 'skip': 'Only works from Norway',
  189. }, {
  190. 'url': 'https://radio.nrk.no/serie/dagsnytt/NPUB21019315/12-07-2015#',
  191. 'only_matching': True,
  192. }]
  193. class NRKPlaylistIE(InfoExtractor):
  194. _VALID_URL = r'https?://(?:www\.)?nrk\.no/(?!video|skole)(?:[^/]+/)+(?P<id>[^/]+)'
  195. _TESTS = [{
  196. 'url': 'http://www.nrk.no/troms/gjenopplev-den-historiske-solformorkelsen-1.12270763',
  197. 'info_dict': {
  198. 'id': 'gjenopplev-den-historiske-solformorkelsen-1.12270763',
  199. 'title': 'Gjenopplev den historiske solformørkelsen',
  200. 'description': 'md5:c2df8ea3bac5654a26fc2834a542feed',
  201. },
  202. 'playlist_count': 2,
  203. }, {
  204. 'url': 'http://www.nrk.no/kultur/bok/rivertonprisen-til-karin-fossum-1.12266449',
  205. 'info_dict': {
  206. 'id': 'rivertonprisen-til-karin-fossum-1.12266449',
  207. 'title': 'Rivertonprisen til Karin Fossum',
  208. 'description': 'Første kvinne på 15 år til å vinne krimlitteraturprisen.',
  209. },
  210. 'playlist_count': 5,
  211. }]
  212. def _real_extract(self, url):
  213. playlist_id = self._match_id(url)
  214. webpage = self._download_webpage(url, playlist_id)
  215. entries = [
  216. self.url_result('nrk:%s' % video_id, 'NRK')
  217. for video_id in re.findall(
  218. r'class="[^"]*\brich\b[^"]*"[^>]+data-video-id="([^"]+)"',
  219. webpage)
  220. ]
  221. playlist_title = self._og_search_title(webpage)
  222. playlist_description = self._og_search_description(webpage)
  223. return self.playlist_result(
  224. entries, playlist_id, playlist_title, playlist_description)
  225. class NRKSkoleIE(InfoExtractor):
  226. IE_DESC = 'NRK Skole'
  227. _VALID_URL = r'https?://(?:www\.)?nrk\.no/skole/klippdetalj?.*\btopic=(?P<id>[^/?#&]+)'
  228. _TESTS = [{
  229. 'url': 'http://nrk.no/skole/klippdetalj?topic=nrk:klipp/616532',
  230. 'md5': '04cd85877cc1913bce73c5d28a47e00f',
  231. 'info_dict': {
  232. 'id': '6021',
  233. 'ext': 'flv',
  234. 'title': 'Genetikk og eneggede tvillinger',
  235. 'description': 'md5:3aca25dcf38ec30f0363428d2b265f8d',
  236. 'duration': 399,
  237. },
  238. }, {
  239. 'url': 'http://www.nrk.no/skole/klippdetalj?topic=nrk%3Aklipp%2F616532#embed',
  240. 'only_matching': True,
  241. }, {
  242. 'url': 'http://www.nrk.no/skole/klippdetalj?topic=urn:x-mediadb:21379',
  243. 'only_matching': True,
  244. }]
  245. def _real_extract(self, url):
  246. video_id = compat_urllib_parse_unquote(self._match_id(url))
  247. webpage = self._download_webpage(url, video_id)
  248. nrk_id = self._search_regex(r'data-nrk-id=["\'](\d+)', webpage, 'nrk id')
  249. return self.url_result('nrk:%s' % nrk_id)