nrk.py 31 KB

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