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.

299 lines
11 KiB

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