nrk.py 28 KB

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