nrk.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import itertools
  4. import random
  5. import re
  6. from .common import InfoExtractor
  7. from ..compat import compat_str
  8. from ..utils import (
  9. determine_ext,
  10. ExtractorError,
  11. int_or_none,
  12. parse_duration,
  13. try_get,
  14. urljoin,
  15. url_or_none,
  16. )
  17. class NRKBaseIE(InfoExtractor):
  18. _GEO_COUNTRIES = ['NO']
  19. _CDN_REPL_REGEX = r'''(?x)://
  20. (?:
  21. nrkod\d{1,2}-httpcache0-47115-cacheod0\.dna\.ip-only\.net/47115-cacheod0|
  22. nrk-od-no\.telenorcdn\.net|
  23. minicdn-od\.nrk\.no/od/nrkhd-osl-rr\.netwerk\.no/no
  24. )/'''
  25. def _extract_nrk_formats(self, asset_url, video_id):
  26. if re.match(r'https?://[^/]+\.akamaihd\.net/i/', asset_url):
  27. return self._extract_akamai_formats(asset_url, video_id)
  28. asset_url = re.sub(r'(?:bw_(?:low|high)=\d+|no_audio_only)&?', '', asset_url)
  29. formats = self._extract_m3u8_formats(
  30. asset_url, video_id, 'mp4', 'm3u8_native', fatal=False)
  31. if not formats and re.search(self._CDN_REPL_REGEX, asset_url):
  32. formats = self._extract_m3u8_formats(
  33. re.sub(self._CDN_REPL_REGEX, '://nrk-od-%02d.akamaized.net/no/' % random.randint(0, 99), asset_url),
  34. video_id, 'mp4', 'm3u8_native', fatal=False)
  35. return formats
  36. def _raise_error(self, data):
  37. MESSAGES = {
  38. 'ProgramRightsAreNotReady': 'Du kan dessverre ikke se eller høre programmet',
  39. 'ProgramRightsHasExpired': 'Programmet har gått ut',
  40. 'NoProgramRights': 'Ikke tilgjengelig',
  41. 'ProgramIsGeoBlocked': 'NRK har ikke rettigheter til å vise dette programmet utenfor Norge',
  42. }
  43. message_type = data.get('messageType', '')
  44. # Can be ProgramIsGeoBlocked or ChannelIsGeoBlocked*
  45. if 'IsGeoBlocked' in message_type or try_get(data, lambda x: x['usageRights']['isGeoBlocked']) is True:
  46. self.raise_geo_restricted(
  47. msg=MESSAGES.get('ProgramIsGeoBlocked'),
  48. countries=self._GEO_COUNTRIES)
  49. message = data.get('endUserMessage') or MESSAGES.get(message_type, message_type)
  50. raise ExtractorError('%s said: %s' % (self.IE_NAME, message), expected=True)
  51. def _call_api(self, path, video_id, item=None, note=None, fatal=True, query=None):
  52. return self._download_json(
  53. urljoin('http://psapi.nrk.no/', path),
  54. video_id, note or 'Downloading %s JSON' % item,
  55. fatal=fatal, query=query,
  56. headers={'Accept-Encoding': 'gzip, deflate, br'})
  57. class NRKIE(NRKBaseIE):
  58. _VALID_URL = r'''(?x)
  59. (?:
  60. nrk:|
  61. https?://
  62. (?:
  63. (?:www\.)?nrk\.no/video/(?:PS\*|[^_]+_)|
  64. v8[-.]psapi\.nrk\.no/mediaelement/
  65. )
  66. )
  67. (?P<id>[^?\#&]+)
  68. '''
  69. _TESTS = [{
  70. # video
  71. 'url': 'http://www.nrk.no/video/PS*150533',
  72. 'md5': 'f46be075326e23ad0e524edfcb06aeb6',
  73. 'info_dict': {
  74. 'id': '150533',
  75. 'ext': 'mp4',
  76. 'title': 'Dompap og andre fugler i Piip-Show',
  77. 'description': 'md5:d9261ba34c43b61c812cb6b0269a5c8f',
  78. 'duration': 262,
  79. }
  80. }, {
  81. # audio
  82. 'url': 'http://www.nrk.no/video/PS*154915',
  83. # MD5 is unstable
  84. 'info_dict': {
  85. 'id': '154915',
  86. 'ext': 'mp4',
  87. 'title': 'Slik høres internett ut når du er blind',
  88. 'description': 'md5:a621f5cc1bd75c8d5104cb048c6b8568',
  89. 'duration': 20,
  90. }
  91. }, {
  92. 'url': 'nrk:ecc1b952-96dc-4a98-81b9-5296dc7a98d9',
  93. 'only_matching': True,
  94. }, {
  95. 'url': 'nrk:clip/7707d5a3-ebe7-434a-87d5-a3ebe7a34a70',
  96. 'only_matching': True,
  97. }, {
  98. 'url': 'https://v8-psapi.nrk.no/mediaelement/ecc1b952-96dc-4a98-81b9-5296dc7a98d9',
  99. 'only_matching': True,
  100. }, {
  101. 'url': 'https://www.nrk.no/video/dompap-og-andre-fugler-i-piip-show_150533',
  102. 'only_matching': True,
  103. }, {
  104. 'url': 'https://www.nrk.no/video/humor/kommentatorboksen-reiser-til-sjos_d1fda11f-a4ad-437a-a374-0398bc84e999',
  105. 'only_matching': True,
  106. }, {
  107. # podcast
  108. 'url': 'nrk:l_96f4f1b0-de54-4e6a-b4f1-b0de54fe6af8',
  109. 'only_matching': True,
  110. }, {
  111. 'url': 'nrk:podcast/l_96f4f1b0-de54-4e6a-b4f1-b0de54fe6af8',
  112. 'only_matching': True,
  113. }, {
  114. # clip
  115. 'url': 'nrk:150533',
  116. 'only_matching': True,
  117. }, {
  118. 'url': 'nrk:clip/150533',
  119. 'only_matching': True,
  120. }, {
  121. # program
  122. 'url': 'nrk:MDDP12000117',
  123. 'only_matching': True,
  124. }, {
  125. 'url': 'nrk:program/ENRK10100318',
  126. 'only_matching': True,
  127. }, {
  128. # direkte
  129. 'url': 'nrk:nrk1',
  130. 'only_matching': True,
  131. }, {
  132. 'url': 'nrk:channel/nrk1',
  133. 'only_matching': True,
  134. }]
  135. def _real_extract(self, url):
  136. video_id = self._match_id(url).split('/')[-1]
  137. path_templ = 'playback/%s/' + video_id
  138. def call_playback_api(item, query=None):
  139. return self._call_api(path_templ % item, video_id, item, query=query)
  140. # known values for preferredCdn: akamai, iponly, minicdn and telenor
  141. manifest = call_playback_api('manifest', {'preferredCdn': 'akamai'})
  142. video_id = try_get(manifest, lambda x: x['id'], compat_str) or video_id
  143. if manifest.get('playability') == 'nonPlayable':
  144. self._raise_error(manifest['nonPlayable'])
  145. playable = manifest['playable']
  146. formats = []
  147. for asset in playable['assets']:
  148. if not isinstance(asset, dict):
  149. continue
  150. if asset.get('encrypted'):
  151. continue
  152. format_url = url_or_none(asset.get('url'))
  153. if not format_url:
  154. continue
  155. asset_format = (asset.get('format') or '').lower()
  156. if asset_format == 'hls' or determine_ext(format_url) == 'm3u8':
  157. formats.extend(self._extract_nrk_formats(format_url, video_id))
  158. elif asset_format == 'mp3':
  159. formats.append({
  160. 'url': format_url,
  161. 'format_id': asset_format,
  162. 'vcodec': 'none',
  163. })
  164. self._sort_formats(formats)
  165. data = call_playback_api('metadata')
  166. preplay = data['preplay']
  167. titles = preplay['titles']
  168. title = titles['title']
  169. alt_title = titles.get('subtitle')
  170. description = preplay.get('description')
  171. duration = parse_duration(playable.get('duration')) or parse_duration(data.get('duration'))
  172. thumbnails = []
  173. for image in try_get(
  174. preplay, lambda x: x['poster']['images'], list) or []:
  175. if not isinstance(image, dict):
  176. continue
  177. image_url = url_or_none(image.get('url'))
  178. if not image_url:
  179. continue
  180. thumbnails.append({
  181. 'url': image_url,
  182. 'width': int_or_none(image.get('pixelWidth')),
  183. 'height': int_or_none(image.get('pixelHeight')),
  184. })
  185. age_limit = int_or_none(try_get(
  186. data, lambda x: x['legalAge']['body']['rating']['code']))
  187. return {
  188. 'id': video_id,
  189. 'title': title,
  190. 'alt_title': alt_title,
  191. 'description': description,
  192. 'duration': duration,
  193. 'thumbnails': thumbnails,
  194. 'age_limit': age_limit,
  195. 'formats': formats,
  196. }
  197. class NRKTVIE(InfoExtractor):
  198. IE_DESC = 'NRK TV and NRK Radio'
  199. _EPISODE_RE = r'(?P<id>[a-zA-Z]{4}\d{8})'
  200. _VALID_URL = r'https?://(?:tv|radio)\.nrk(?:super)?\.no/(?:[^/]+/)*%s' % _EPISODE_RE
  201. _TESTS = [{
  202. 'url': 'https://tv.nrk.no/program/MDDP12000117',
  203. 'md5': 'c4a5960f1b00b40d47db65c1064e0ab1',
  204. 'info_dict': {
  205. 'id': 'MDDP12000117AA',
  206. 'ext': 'mp4',
  207. 'title': 'Alarm Trolltunga',
  208. 'description': 'md5:46923a6e6510eefcce23d5ef2a58f2ce',
  209. 'duration': 2223.44,
  210. 'age_limit': 6,
  211. },
  212. }, {
  213. 'url': 'https://tv.nrk.no/serie/20-spoersmaal-tv/MUHH48000314/23-05-2014',
  214. 'md5': '8d40dab61cea8ab0114e090b029a0565',
  215. 'info_dict': {
  216. 'id': 'MUHH48000314AA',
  217. 'ext': 'mp4',
  218. 'title': '20 spørsmål 23.05.2014',
  219. 'description': 'md5:bdea103bc35494c143c6a9acdd84887a',
  220. 'duration': 1741,
  221. 'series': '20 spørsmål',
  222. 'episode': '23.05.2014',
  223. },
  224. }, {
  225. 'url': 'https://tv.nrk.no/program/mdfp15000514',
  226. 'info_dict': {
  227. 'id': 'MDFP15000514CA',
  228. 'ext': 'mp4',
  229. 'title': 'Grunnlovsjubiléet - Stor ståhei for ingenting 24.05.2014',
  230. 'description': 'md5:89290c5ccde1b3a24bb8050ab67fe1db',
  231. 'duration': 4605.08,
  232. 'series': 'Kunnskapskanalen',
  233. 'episode': '24.05.2014',
  234. },
  235. 'params': {
  236. 'skip_download': True,
  237. },
  238. }, {
  239. # single playlist video
  240. 'url': 'https://tv.nrk.no/serie/tour-de-ski/MSPO40010515/06-01-2015#del=2',
  241. 'info_dict': {
  242. 'id': 'MSPO40010515AH',
  243. 'ext': 'mp4',
  244. 'title': 'Sprint fri teknikk, kvinner og menn 06.01.2015',
  245. 'description': 'md5:c03aba1e917561eface5214020551b7a',
  246. },
  247. 'params': {
  248. 'skip_download': True,
  249. },
  250. 'expected_warnings': ['Failed to download m3u8 information'],
  251. 'skip': 'particular part is not supported currently',
  252. }, {
  253. 'url': 'https://tv.nrk.no/serie/tour-de-ski/MSPO40010515/06-01-2015',
  254. 'info_dict': {
  255. 'id': 'MSPO40010515AH',
  256. 'ext': 'mp4',
  257. 'title': 'Sprint fri teknikk, kvinner og menn 06.01.2015',
  258. 'description': 'md5:c03aba1e917561eface5214020551b7a',
  259. },
  260. 'expected_warnings': ['Failed to download m3u8 information'],
  261. }, {
  262. 'url': 'https://tv.nrk.no/serie/anno/KMTE50001317/sesong-3/episode-13',
  263. 'info_dict': {
  264. 'id': 'KMTE50001317AA',
  265. 'ext': 'mp4',
  266. 'title': 'Anno 13:30',
  267. 'description': 'md5:11d9613661a8dbe6f9bef54e3a4cbbfa',
  268. 'duration': 2340,
  269. 'series': 'Anno',
  270. 'episode': '13:30',
  271. 'season_number': 3,
  272. 'episode_number': 13,
  273. },
  274. 'params': {
  275. 'skip_download': True,
  276. },
  277. }, {
  278. 'url': 'https://tv.nrk.no/serie/nytt-paa-nytt/MUHH46000317/27-01-2017',
  279. 'info_dict': {
  280. 'id': 'MUHH46000317AA',
  281. 'ext': 'mp4',
  282. 'title': 'Nytt på Nytt 27.01.2017',
  283. 'description': 'md5:5358d6388fba0ea6f0b6d11c48b9eb4b',
  284. 'duration': 1796,
  285. 'series': 'Nytt på nytt',
  286. 'episode': '27.01.2017',
  287. },
  288. 'params': {
  289. 'skip_download': True,
  290. },
  291. 'skip': 'ProgramRightsHasExpired',
  292. }, {
  293. 'url': 'https://radio.nrk.no/serie/dagsnytt/NPUB21019315/12-07-2015#',
  294. 'only_matching': True,
  295. }, {
  296. 'url': 'https://tv.nrk.no/serie/lindmo/2018/MUHU11006318/avspiller',
  297. 'only_matching': True,
  298. }, {
  299. 'url': 'https://radio.nrk.no/serie/dagsnytt/sesong/201507/NPUB21019315',
  300. 'only_matching': True,
  301. }]
  302. def _real_extract(self, url):
  303. video_id = self._match_id(url)
  304. return self.url_result(
  305. 'nrk:%s' % video_id, ie=NRKIE.ie_key(), video_id=video_id)
  306. class NRKTVEpisodeIE(InfoExtractor):
  307. _VALID_URL = r'https?://tv\.nrk\.no/serie/(?P<id>[^/]+/sesong/\d+/episode/\d+)'
  308. _TESTS = [{
  309. 'url': 'https://tv.nrk.no/serie/hellums-kro/sesong/1/episode/2',
  310. 'info_dict': {
  311. 'id': 'MUHH36005220BA',
  312. 'ext': 'mp4',
  313. 'title': 'Kro, krig og kjærlighet 2:6',
  314. 'description': 'md5:b32a7dc0b1ed27c8064f58b97bda4350',
  315. 'duration': 1563,
  316. 'series': 'Hellums kro',
  317. 'season_number': 1,
  318. 'episode_number': 2,
  319. 'episode': '2:6',
  320. 'age_limit': 6,
  321. },
  322. 'params': {
  323. 'skip_download': True,
  324. },
  325. }, {
  326. 'url': 'https://tv.nrk.no/serie/backstage/sesong/1/episode/8',
  327. 'info_dict': {
  328. 'id': 'MSUI14000816AA',
  329. 'ext': 'mp4',
  330. 'title': 'Backstage 8:30',
  331. 'description': 'md5:de6ca5d5a2d56849e4021f2bf2850df4',
  332. 'duration': 1320,
  333. 'series': 'Backstage',
  334. 'season_number': 1,
  335. 'episode_number': 8,
  336. 'episode': '8:30',
  337. },
  338. 'params': {
  339. 'skip_download': True,
  340. },
  341. 'skip': 'ProgramRightsHasExpired',
  342. }]
  343. def _real_extract(self, url):
  344. display_id = self._match_id(url)
  345. webpage = self._download_webpage(url, display_id)
  346. info = self._search_json_ld(webpage, display_id, default={})
  347. nrk_id = info.get('@id') or self._html_search_meta(
  348. 'nrk:program-id', webpage, default=None) or self._search_regex(
  349. r'data-program-id=["\'](%s)' % NRKTVIE._EPISODE_RE, webpage,
  350. 'nrk id')
  351. assert re.match(NRKTVIE._EPISODE_RE, nrk_id)
  352. info.update({
  353. '_type': 'url_transparent',
  354. 'id': nrk_id,
  355. 'url': 'nrk:%s' % nrk_id,
  356. 'ie_key': NRKIE.ie_key(),
  357. })
  358. return info
  359. class NRKTVSerieBaseIE(NRKBaseIE):
  360. def _extract_entries(self, entry_list):
  361. if not isinstance(entry_list, list):
  362. return []
  363. entries = []
  364. for episode in entry_list:
  365. nrk_id = episode.get('prfId') or episode.get('episodeId')
  366. if not nrk_id or not isinstance(nrk_id, compat_str):
  367. continue
  368. entries.append(self.url_result(
  369. 'nrk:%s' % nrk_id, ie=NRKIE.ie_key(), video_id=nrk_id))
  370. return entries
  371. _ASSETS_KEYS = ('episodes', 'instalments',)
  372. def _extract_assets_key(self, embedded):
  373. for asset_key in self._ASSETS_KEYS:
  374. if embedded.get(asset_key):
  375. return asset_key
  376. @staticmethod
  377. def _catalog_name(serie_kind):
  378. return 'podcast' if serie_kind in ('podcast', 'podkast') else 'series'
  379. def _entries(self, data, display_id):
  380. for page_num in itertools.count(1):
  381. embedded = data.get('_embedded') or data
  382. if not isinstance(embedded, dict):
  383. break
  384. assets_key = self._extract_assets_key(embedded)
  385. if not assets_key:
  386. break
  387. # Extract entries
  388. entries = try_get(
  389. embedded,
  390. (lambda x: x[assets_key]['_embedded'][assets_key],
  391. lambda x: x[assets_key]),
  392. list)
  393. for e in self._extract_entries(entries):
  394. yield e
  395. # Find next URL
  396. next_url_path = try_get(
  397. data,
  398. (lambda x: x['_links']['next']['href'],
  399. lambda x: x['_embedded'][assets_key]['_links']['next']['href']),
  400. compat_str)
  401. if not next_url_path:
  402. break
  403. data = self._call_api(
  404. next_url_path, display_id,
  405. note='Downloading %s JSON page %d' % (assets_key, page_num),
  406. fatal=False)
  407. if not data:
  408. break
  409. class NRKTVSeasonIE(NRKTVSerieBaseIE):
  410. _VALID_URL = r'''(?x)
  411. https?://
  412. (?P<domain>tv|radio)\.nrk\.no/
  413. (?P<serie_kind>serie|pod[ck]ast)/
  414. (?P<serie>[^/]+)/
  415. (?:
  416. (?:sesong/)?(?P<id>\d+)|
  417. sesong/(?P<id_2>[^/?#&]+)
  418. )
  419. '''
  420. _TESTS = [{
  421. 'url': 'https://tv.nrk.no/serie/backstage/sesong/1',
  422. 'info_dict': {
  423. 'id': 'backstage/1',
  424. 'title': 'Sesong 1',
  425. },
  426. 'playlist_mincount': 30,
  427. }, {
  428. # no /sesong/ in path
  429. 'url': 'https://tv.nrk.no/serie/lindmo/2016',
  430. 'info_dict': {
  431. 'id': 'lindmo/2016',
  432. 'title': '2016',
  433. },
  434. 'playlist_mincount': 29,
  435. }, {
  436. # weird nested _embedded in catalog JSON response
  437. 'url': 'https://radio.nrk.no/serie/dickie-dick-dickens/sesong/1',
  438. 'info_dict': {
  439. 'id': 'dickie-dick-dickens/1',
  440. 'title': 'Sesong 1',
  441. },
  442. 'playlist_mincount': 11,
  443. }, {
  444. # 841 entries, multi page
  445. 'url': 'https://radio.nrk.no/serie/dagsnytt/sesong/201509',
  446. 'info_dict': {
  447. 'id': 'dagsnytt/201509',
  448. 'title': 'September 2015',
  449. },
  450. 'playlist_mincount': 841,
  451. }, {
  452. # 180 entries, single page
  453. 'url': 'https://tv.nrk.no/serie/spangas/sesong/1',
  454. 'only_matching': True,
  455. }, {
  456. 'url': 'https://radio.nrk.no/podkast/hele_historien/sesong/diagnose-kverulant',
  457. 'info_dict': {
  458. 'id': 'hele_historien/diagnose-kverulant',
  459. 'title': 'Diagnose kverulant',
  460. },
  461. 'playlist_mincount': 3,
  462. }, {
  463. 'url': 'https://radio.nrk.no/podkast/loerdagsraadet/sesong/202101',
  464. 'only_matching': True,
  465. }]
  466. @classmethod
  467. def suitable(cls, url):
  468. return (False if NRKTVIE.suitable(url) or NRKTVEpisodeIE.suitable(url) or NRKRadioPodkastIE.suitable(url)
  469. else super(NRKTVSeasonIE, cls).suitable(url))
  470. def _real_extract(self, url):
  471. mobj = re.match(self._VALID_URL, url)
  472. domain = mobj.group('domain')
  473. serie_kind = mobj.group('serie_kind')
  474. serie = mobj.group('serie')
  475. season_id = mobj.group('id') or mobj.group('id_2')
  476. display_id = '%s/%s' % (serie, season_id)
  477. data = self._call_api(
  478. '%s/catalog/%s/%s/seasons/%s'
  479. % (domain, self._catalog_name(serie_kind), serie, season_id),
  480. display_id, 'season', query={'pageSize': 50})
  481. title = try_get(data, lambda x: x['titles']['title'], compat_str) or display_id
  482. return self.playlist_result(
  483. self._entries(data, display_id),
  484. display_id, title)
  485. class NRKTVSeriesIE(NRKTVSerieBaseIE):
  486. _VALID_URL = r'https?://(?P<domain>(?:tv|radio)\.nrk|(?:tv\.)?nrksuper)\.no/(?P<serie_kind>serie|pod[ck]ast)/(?P<id>[^/]+)'
  487. _TESTS = [{
  488. # new layout, instalments
  489. 'url': 'https://tv.nrk.no/serie/groenn-glede',
  490. 'info_dict': {
  491. 'id': 'groenn-glede',
  492. 'title': 'Grønn glede',
  493. 'description': 'md5:7576e92ae7f65da6993cf90ee29e4608',
  494. },
  495. 'playlist_mincount': 90,
  496. }, {
  497. # new layout, instalments, more entries
  498. 'url': 'https://tv.nrk.no/serie/lindmo',
  499. 'only_matching': True,
  500. }, {
  501. 'url': 'https://tv.nrk.no/serie/blank',
  502. 'info_dict': {
  503. 'id': 'blank',
  504. 'title': 'Blank',
  505. 'description': 'md5:7664b4e7e77dc6810cd3bca367c25b6e',
  506. },
  507. 'playlist_mincount': 30,
  508. }, {
  509. # new layout, seasons
  510. 'url': 'https://tv.nrk.no/serie/backstage',
  511. 'info_dict': {
  512. 'id': 'backstage',
  513. 'title': 'Backstage',
  514. 'description': 'md5:63692ceb96813d9a207e9910483d948b',
  515. },
  516. 'playlist_mincount': 60,
  517. }, {
  518. # old layout
  519. 'url': 'https://tv.nrksuper.no/serie/labyrint',
  520. 'info_dict': {
  521. 'id': 'labyrint',
  522. 'title': 'Labyrint',
  523. 'description': 'I Daidalos sin undersjøiske Labyrint venter spennende oppgaver, skumle robotskapninger og slim.',
  524. },
  525. 'playlist_mincount': 3,
  526. }, {
  527. 'url': 'https://tv.nrk.no/serie/broedrene-dal-og-spektralsteinene',
  528. 'only_matching': True,
  529. }, {
  530. 'url': 'https://tv.nrk.no/serie/saving-the-human-race',
  531. 'only_matching': True,
  532. }, {
  533. 'url': 'https://tv.nrk.no/serie/postmann-pat',
  534. 'only_matching': True,
  535. }, {
  536. 'url': 'https://radio.nrk.no/serie/dickie-dick-dickens',
  537. 'info_dict': {
  538. 'id': 'dickie-dick-dickens',
  539. 'title': 'Dickie Dick Dickens',
  540. 'description': 'md5:19e67411ffe57f7dce08a943d7a0b91f',
  541. },
  542. 'playlist_mincount': 8,
  543. }, {
  544. 'url': 'https://nrksuper.no/serie/labyrint',
  545. 'only_matching': True,
  546. }, {
  547. 'url': 'https://radio.nrk.no/podkast/ulrikkes_univers',
  548. 'info_dict': {
  549. 'id': 'ulrikkes_univers',
  550. },
  551. 'playlist_mincount': 10,
  552. }, {
  553. 'url': 'https://radio.nrk.no/podkast/ulrikkes_univers/nrkno-poddkast-26588-134079-05042018030000',
  554. 'only_matching': True,
  555. }]
  556. @classmethod
  557. def suitable(cls, url):
  558. return (
  559. False if any(ie.suitable(url)
  560. for ie in (NRKTVIE, NRKTVEpisodeIE, NRKRadioPodkastIE, NRKTVSeasonIE))
  561. else super(NRKTVSeriesIE, cls).suitable(url))
  562. def _real_extract(self, url):
  563. site, serie_kind, series_id = re.match(self._VALID_URL, url).groups()
  564. is_radio = site == 'radio.nrk'
  565. domain = 'radio' if is_radio else 'tv'
  566. size_prefix = 'p' if is_radio else 'embeddedInstalmentsP'
  567. series = self._call_api(
  568. '%s/catalog/%s/%s'
  569. % (domain, self._catalog_name(serie_kind), series_id),
  570. series_id, 'serie', query={size_prefix + 'ageSize': 50})
  571. titles = try_get(series, [
  572. lambda x: x['titles'],
  573. lambda x: x[x['type']]['titles'],
  574. lambda x: x[x['seriesType']]['titles'],
  575. ]) or {}
  576. entries = []
  577. entries.extend(self._entries(series, series_id))
  578. embedded = series.get('_embedded') or {}
  579. linked_seasons = try_get(series, lambda x: x['_links']['seasons']) or []
  580. embedded_seasons = embedded.get('seasons') or []
  581. if len(linked_seasons) > len(embedded_seasons):
  582. for season in linked_seasons:
  583. season_url = urljoin(url, season.get('href'))
  584. if not season_url:
  585. season_name = season.get('name')
  586. if season_name and isinstance(season_name, compat_str):
  587. season_url = 'https://%s.nrk.no/serie/%s/sesong/%s' % (domain, series_id, season_name)
  588. if season_url:
  589. entries.append(self.url_result(
  590. season_url, ie=NRKTVSeasonIE.ie_key(),
  591. video_title=season.get('title')))
  592. else:
  593. for season in embedded_seasons:
  594. entries.extend(self._entries(season, series_id))
  595. entries.extend(self._entries(
  596. embedded.get('extraMaterial') or {}, series_id))
  597. return self.playlist_result(
  598. entries, series_id, titles.get('title'), titles.get('subtitle'))
  599. class NRKTVDirekteIE(NRKTVIE):
  600. IE_DESC = 'NRK TV Direkte and NRK Radio Direkte'
  601. _VALID_URL = r'https?://(?:tv|radio)\.nrk\.no/direkte/(?P<id>[^/?#&]+)'
  602. _TESTS = [{
  603. 'url': 'https://tv.nrk.no/direkte/nrk1',
  604. 'only_matching': True,
  605. }, {
  606. 'url': 'https://radio.nrk.no/direkte/p1_oslo_akershus',
  607. 'only_matching': True,
  608. }]
  609. class NRKRadioPodkastIE(InfoExtractor):
  610. _VALID_URL = r'https?://radio\.nrk\.no/pod[ck]ast/(?:[^/]+/)+(?P<id>l_[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12})'
  611. _TESTS = [{
  612. 'url': 'https://radio.nrk.no/podkast/ulrikkes_univers/l_96f4f1b0-de54-4e6a-b4f1-b0de54fe6af8',
  613. 'md5': '8d40dab61cea8ab0114e090b029a0565',
  614. 'info_dict': {
  615. 'id': 'MUHH48000314AA',
  616. 'ext': 'mp4',
  617. 'title': '20 spørsmål 23.05.2014',
  618. 'description': 'md5:bdea103bc35494c143c6a9acdd84887a',
  619. 'duration': 1741,
  620. 'series': '20 spørsmål',
  621. 'episode': '23.05.2014',
  622. },
  623. }, {
  624. 'url': 'https://radio.nrk.no/podcast/ulrikkes_univers/l_96f4f1b0-de54-4e6a-b4f1-b0de54fe6af8',
  625. 'only_matching': True,
  626. }, {
  627. 'url': 'https://radio.nrk.no/podkast/ulrikkes_univers/sesong/1/l_96f4f1b0-de54-4e6a-b4f1-b0de54fe6af8',
  628. 'only_matching': True,
  629. }, {
  630. 'url': 'https://radio.nrk.no/podkast/hele_historien/sesong/bortfoert-i-bergen/l_774d1a2c-7aa7-4965-8d1a-2c7aa7d9652c',
  631. 'only_matching': True,
  632. }]
  633. def _real_extract(self, url):
  634. video_id = self._match_id(url)
  635. return self.url_result(
  636. 'nrk:%s' % video_id, ie=NRKIE.ie_key(), video_id=video_id)
  637. class NRKPlaylistBaseIE(InfoExtractor):
  638. def _extract_description(self, webpage):
  639. pass
  640. def _real_extract(self, url):
  641. playlist_id = self._match_id(url)
  642. webpage = self._download_webpage(url, playlist_id)
  643. entries = [
  644. self.url_result('nrk:%s' % video_id, NRKIE.ie_key())
  645. for video_id in re.findall(self._ITEM_RE, webpage)
  646. ]
  647. playlist_title = self. _extract_title(webpage)
  648. playlist_description = self._extract_description(webpage)
  649. return self.playlist_result(
  650. entries, playlist_id, playlist_title, playlist_description)
  651. class NRKPlaylistIE(NRKPlaylistBaseIE):
  652. _VALID_URL = r'https?://(?:www\.)?nrk\.no/(?!video|skole)(?:[^/]+/)+(?P<id>[^/]+)'
  653. _ITEM_RE = r'class="[^"]*\brich\b[^"]*"[^>]+data-video-id="([^"]+)"'
  654. _TESTS = [{
  655. 'url': 'http://www.nrk.no/troms/gjenopplev-den-historiske-solformorkelsen-1.12270763',
  656. 'info_dict': {
  657. 'id': 'gjenopplev-den-historiske-solformorkelsen-1.12270763',
  658. 'title': 'Gjenopplev den historiske solformørkelsen',
  659. 'description': 'md5:c2df8ea3bac5654a26fc2834a542feed',
  660. },
  661. 'playlist_count': 2,
  662. }, {
  663. 'url': 'http://www.nrk.no/kultur/bok/rivertonprisen-til-karin-fossum-1.12266449',
  664. 'info_dict': {
  665. 'id': 'rivertonprisen-til-karin-fossum-1.12266449',
  666. 'title': 'Rivertonprisen til Karin Fossum',
  667. 'description': 'Første kvinne på 15 år til å vinne krimlitteraturprisen.',
  668. },
  669. 'playlist_count': 2,
  670. }]
  671. def _extract_title(self, webpage):
  672. return self._og_search_title(webpage, fatal=False)
  673. def _extract_description(self, webpage):
  674. return self._og_search_description(webpage)
  675. class NRKTVEpisodesIE(NRKPlaylistBaseIE):
  676. _VALID_URL = r'https?://tv\.nrk\.no/program/[Ee]pisodes/[^/]+/(?P<id>\d+)'
  677. _ITEM_RE = r'data-episode=["\']%s' % NRKTVIE._EPISODE_RE
  678. _TESTS = [{
  679. 'url': 'https://tv.nrk.no/program/episodes/nytt-paa-nytt/69031',
  680. 'info_dict': {
  681. 'id': '69031',
  682. 'title': 'Nytt på nytt, sesong: 201210',
  683. },
  684. 'playlist_count': 4,
  685. }]
  686. def _extract_title(self, webpage):
  687. return self._html_search_regex(
  688. r'<h1>([^<]+)</h1>', webpage, 'title', fatal=False)
  689. class NRKSkoleIE(InfoExtractor):
  690. IE_DESC = 'NRK Skole'
  691. _VALID_URL = r'https?://(?:www\.)?nrk\.no/skole/?\?.*\bmediaId=(?P<id>\d+)'
  692. _TESTS = [{
  693. 'url': 'https://www.nrk.no/skole/?page=search&q=&mediaId=14099',
  694. 'md5': '18c12c3d071953c3bf8d54ef6b2587b7',
  695. 'info_dict': {
  696. 'id': '6021',
  697. 'ext': 'mp4',
  698. 'title': 'Genetikk og eneggede tvillinger',
  699. 'description': 'md5:3aca25dcf38ec30f0363428d2b265f8d',
  700. 'duration': 399,
  701. },
  702. }, {
  703. 'url': 'https://www.nrk.no/skole/?page=objectives&subject=naturfag&objective=K15114&mediaId=19355',
  704. 'only_matching': True,
  705. }]
  706. def _real_extract(self, url):
  707. video_id = self._match_id(url)
  708. nrk_id = self._download_json(
  709. 'https://nrkno-skole-prod.kube.nrk.no/skole/api/media/%s' % video_id,
  710. video_id)['psId']
  711. return self.url_result('nrk:%s' % nrk_id)