nrk.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  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. def _extract_from_playback(self, video_id):
  58. path_templ = 'playback/%s/' + video_id
  59. def call_playback_api(item, query=None):
  60. return self._call_api(path_templ % item, video_id, item, query=query)
  61. # known values for preferredCdn: akamai, iponly, minicdn and telenor
  62. manifest = call_playback_api('manifest', {'preferredCdn': 'akamai'})
  63. if manifest.get('playability') == 'nonPlayable':
  64. self._raise_error(manifest['nonPlayable'])
  65. playable = manifest['playable']
  66. formats = []
  67. for asset in playable['assets']:
  68. if not isinstance(asset, dict):
  69. continue
  70. if asset.get('encrypted'):
  71. continue
  72. format_url = url_or_none(asset.get('url'))
  73. if not format_url:
  74. continue
  75. if asset.get('format') == 'HLS' or determine_ext(format_url) == 'm3u8':
  76. formats.extend(self._extract_nrk_formats(format_url, video_id))
  77. self._sort_formats(formats)
  78. data = call_playback_api('metadata')
  79. preplay = data['preplay']
  80. titles = preplay['titles']
  81. title = titles['title']
  82. alt_title = titles.get('subtitle')
  83. description = preplay.get('description')
  84. duration = parse_duration(playable.get('duration')) or parse_duration(data.get('duration'))
  85. thumbnails = []
  86. for image in try_get(
  87. preplay, lambda x: x['poster']['images'], list) or []:
  88. if not isinstance(image, dict):
  89. continue
  90. image_url = url_or_none(image.get('url'))
  91. if not image_url:
  92. continue
  93. thumbnails.append({
  94. 'url': image_url,
  95. 'width': int_or_none(image.get('pixelWidth')),
  96. 'height': int_or_none(image.get('pixelHeight')),
  97. })
  98. return {
  99. 'id': video_id,
  100. 'title': title,
  101. 'alt_title': alt_title,
  102. 'description': description,
  103. 'duration': duration,
  104. 'thumbnails': thumbnails,
  105. 'formats': formats,
  106. }
  107. class NRKIE(NRKBaseIE):
  108. _VALID_URL = r'''(?x)
  109. (?:
  110. nrk:|
  111. https?://
  112. (?:
  113. (?:www\.)?nrk\.no/video/(?:PS\*|[^_]+_)|
  114. v8[-.]psapi\.nrk\.no/mediaelement/
  115. )
  116. )
  117. (?P<id>[^?\#&]+)
  118. '''
  119. _TESTS = [{
  120. # video
  121. 'url': 'http://www.nrk.no/video/PS*150533',
  122. 'md5': 'f46be075326e23ad0e524edfcb06aeb6',
  123. 'info_dict': {
  124. 'id': '150533',
  125. 'ext': 'mp4',
  126. 'title': 'Dompap og andre fugler i Piip-Show',
  127. 'description': 'md5:d9261ba34c43b61c812cb6b0269a5c8f',
  128. 'duration': 262,
  129. }
  130. }, {
  131. # audio
  132. 'url': 'http://www.nrk.no/video/PS*154915',
  133. # MD5 is unstable
  134. 'info_dict': {
  135. 'id': '154915',
  136. 'ext': 'mp4',
  137. 'title': 'Slik høres internett ut når du er blind',
  138. 'description': 'md5:a621f5cc1bd75c8d5104cb048c6b8568',
  139. 'duration': 20,
  140. }
  141. }, {
  142. 'url': 'nrk:ecc1b952-96dc-4a98-81b9-5296dc7a98d9',
  143. 'only_matching': True,
  144. }, {
  145. 'url': 'nrk:clip/7707d5a3-ebe7-434a-87d5-a3ebe7a34a70',
  146. 'only_matching': True,
  147. }, {
  148. 'url': 'https://v8-psapi.nrk.no/mediaelement/ecc1b952-96dc-4a98-81b9-5296dc7a98d9',
  149. 'only_matching': True,
  150. }, {
  151. 'url': 'https://www.nrk.no/video/dompap-og-andre-fugler-i-piip-show_150533',
  152. 'only_matching': True,
  153. }, {
  154. 'url': 'https://www.nrk.no/video/humor/kommentatorboksen-reiser-til-sjos_d1fda11f-a4ad-437a-a374-0398bc84e999',
  155. 'only_matching': True,
  156. }]
  157. def _real_extract(self, url):
  158. video_id = self._match_id(url)
  159. return self._extract_from_playback(video_id)
  160. class NRKTVIE(NRKBaseIE):
  161. IE_DESC = 'NRK TV and NRK Radio'
  162. _EPISODE_RE = r'(?P<id>[a-zA-Z]{4}\d{8})'
  163. _VALID_URL = r'https?://(?:tv|radio)\.nrk(?:super)?\.no/(?:[^/]+/)*%s' % _EPISODE_RE
  164. _TESTS = [{
  165. 'url': 'https://tv.nrk.no/program/MDDP12000117',
  166. 'md5': 'c4a5960f1b00b40d47db65c1064e0ab1',
  167. 'info_dict': {
  168. 'id': 'MDDP12000117AA',
  169. 'ext': 'mp4',
  170. 'title': 'Alarm Trolltunga',
  171. 'description': 'md5:46923a6e6510eefcce23d5ef2a58f2ce',
  172. 'duration': 2223.44,
  173. 'age_limit': 6,
  174. },
  175. }, {
  176. 'url': 'https://tv.nrk.no/serie/20-spoersmaal-tv/MUHH48000314/23-05-2014',
  177. 'md5': '8d40dab61cea8ab0114e090b029a0565',
  178. 'info_dict': {
  179. 'id': 'MUHH48000314AA',
  180. 'ext': 'mp4',
  181. 'title': '20 spørsmål 23.05.2014',
  182. 'description': 'md5:bdea103bc35494c143c6a9acdd84887a',
  183. 'duration': 1741,
  184. 'series': '20 spørsmål',
  185. 'episode': '23.05.2014',
  186. },
  187. }, {
  188. 'url': 'https://tv.nrk.no/program/mdfp15000514',
  189. 'info_dict': {
  190. 'id': 'MDFP15000514CA',
  191. 'ext': 'mp4',
  192. 'title': 'Grunnlovsjubiléet - Stor ståhei for ingenting 24.05.2014',
  193. 'description': 'md5:89290c5ccde1b3a24bb8050ab67fe1db',
  194. 'duration': 4605.08,
  195. 'series': 'Kunnskapskanalen',
  196. 'episode': '24.05.2014',
  197. },
  198. 'params': {
  199. 'skip_download': True,
  200. },
  201. }, {
  202. # single playlist video
  203. 'url': 'https://tv.nrk.no/serie/tour-de-ski/MSPO40010515/06-01-2015#del=2',
  204. 'info_dict': {
  205. 'id': 'MSPO40010515AH',
  206. 'ext': 'mp4',
  207. 'title': 'Sprint fri teknikk, kvinner og menn 06.01.2015',
  208. 'description': 'md5:c03aba1e917561eface5214020551b7a',
  209. },
  210. 'params': {
  211. 'skip_download': True,
  212. },
  213. 'expected_warnings': ['Failed to download m3u8 information'],
  214. 'skip': 'particular part is not supported currently',
  215. }, {
  216. 'url': 'https://tv.nrk.no/serie/tour-de-ski/MSPO40010515/06-01-2015',
  217. 'info_dict': {
  218. 'id': 'MSPO40010515AH',
  219. 'ext': 'mp4',
  220. 'title': 'Sprint fri teknikk, kvinner og menn 06.01.2015',
  221. 'description': 'md5:c03aba1e917561eface5214020551b7a',
  222. },
  223. 'expected_warnings': ['Failed to download m3u8 information'],
  224. }, {
  225. 'url': 'https://tv.nrk.no/serie/anno/KMTE50001317/sesong-3/episode-13',
  226. 'info_dict': {
  227. 'id': 'KMTE50001317AA',
  228. 'ext': 'mp4',
  229. 'title': 'Anno 13:30',
  230. 'description': 'md5:11d9613661a8dbe6f9bef54e3a4cbbfa',
  231. 'duration': 2340,
  232. 'series': 'Anno',
  233. 'episode': '13:30',
  234. 'season_number': 3,
  235. 'episode_number': 13,
  236. },
  237. 'params': {
  238. 'skip_download': True,
  239. },
  240. }, {
  241. 'url': 'https://tv.nrk.no/serie/nytt-paa-nytt/MUHH46000317/27-01-2017',
  242. 'info_dict': {
  243. 'id': 'MUHH46000317AA',
  244. 'ext': 'mp4',
  245. 'title': 'Nytt på Nytt 27.01.2017',
  246. 'description': 'md5:5358d6388fba0ea6f0b6d11c48b9eb4b',
  247. 'duration': 1796,
  248. 'series': 'Nytt på nytt',
  249. 'episode': '27.01.2017',
  250. },
  251. 'params': {
  252. 'skip_download': True,
  253. },
  254. 'skip': 'ProgramRightsHasExpired',
  255. }, {
  256. 'url': 'https://radio.nrk.no/serie/dagsnytt/NPUB21019315/12-07-2015#',
  257. 'only_matching': True,
  258. }, {
  259. 'url': 'https://tv.nrk.no/serie/lindmo/2018/MUHU11006318/avspiller',
  260. 'only_matching': True,
  261. }, {
  262. 'url': 'https://radio.nrk.no/serie/dagsnytt/sesong/201507/NPUB21019315',
  263. 'only_matching': True,
  264. }]
  265. def _real_extract(self, url):
  266. video_id = self._match_id(url)
  267. return self._extract_from_playback(video_id)
  268. class NRKTVEpisodeIE(InfoExtractor):
  269. _VALID_URL = r'https?://tv\.nrk\.no/serie/(?P<id>[^/]+/sesong/\d+/episode/\d+)'
  270. _TESTS = [{
  271. 'url': 'https://tv.nrk.no/serie/hellums-kro/sesong/1/episode/2',
  272. 'info_dict': {
  273. 'id': 'MUHH36005220BA',
  274. 'ext': 'mp4',
  275. 'title': 'Kro, krig og kjærlighet 2:6',
  276. 'description': 'md5:b32a7dc0b1ed27c8064f58b97bda4350',
  277. 'duration': 1563,
  278. 'series': 'Hellums kro',
  279. 'season_number': 1,
  280. 'episode_number': 2,
  281. 'episode': '2:6',
  282. 'age_limit': 6,
  283. },
  284. 'params': {
  285. 'skip_download': True,
  286. },
  287. }, {
  288. 'url': 'https://tv.nrk.no/serie/backstage/sesong/1/episode/8',
  289. 'info_dict': {
  290. 'id': 'MSUI14000816AA',
  291. 'ext': 'mp4',
  292. 'title': 'Backstage 8:30',
  293. 'description': 'md5:de6ca5d5a2d56849e4021f2bf2850df4',
  294. 'duration': 1320,
  295. 'series': 'Backstage',
  296. 'season_number': 1,
  297. 'episode_number': 8,
  298. 'episode': '8:30',
  299. },
  300. 'params': {
  301. 'skip_download': True,
  302. },
  303. 'skip': 'ProgramRightsHasExpired',
  304. }]
  305. def _real_extract(self, url):
  306. display_id = self._match_id(url)
  307. webpage = self._download_webpage(url, display_id)
  308. info = self._search_json_ld(webpage, display_id, default={})
  309. nrk_id = info.get('@id') or self._html_search_meta(
  310. 'nrk:program-id', webpage, default=None) or self._search_regex(
  311. r'data-program-id=["\'](%s)' % NRKTVIE._EPISODE_RE, webpage,
  312. 'nrk id')
  313. assert re.match(NRKTVIE._EPISODE_RE, nrk_id)
  314. info.update({
  315. '_type': 'url_transparent',
  316. 'id': nrk_id,
  317. 'url': 'nrk:%s' % nrk_id,
  318. 'ie_key': NRKIE.ie_key(),
  319. })
  320. return info
  321. class NRKTVSerieBaseIE(NRKBaseIE):
  322. def _extract_entries(self, entry_list):
  323. if not isinstance(entry_list, list):
  324. return []
  325. entries = []
  326. for episode in entry_list:
  327. nrk_id = episode.get('prfId') or episode.get('episodeId')
  328. if not nrk_id or not isinstance(nrk_id, compat_str):
  329. continue
  330. if not re.match(NRKTVIE._EPISODE_RE, nrk_id):
  331. continue
  332. entries.append(self.url_result(
  333. 'nrk:%s' % nrk_id, ie=NRKIE.ie_key(), video_id=nrk_id))
  334. return entries
  335. _ASSETS_KEYS = ('episodes', 'instalments',)
  336. def _extract_assets_key(self, embedded):
  337. for asset_key in self._ASSETS_KEYS:
  338. if embedded.get(asset_key):
  339. return asset_key
  340. def _entries(self, data, display_id):
  341. for page_num in itertools.count(1):
  342. embedded = data.get('_embedded') or data
  343. if not isinstance(embedded, dict):
  344. break
  345. assets_key = self._extract_assets_key(embedded)
  346. if not assets_key:
  347. break
  348. # Extract entries
  349. entries = try_get(
  350. embedded,
  351. (lambda x: x[assets_key]['_embedded'][assets_key],
  352. lambda x: x[assets_key]),
  353. list)
  354. for e in self._extract_entries(entries):
  355. yield e
  356. # Find next URL
  357. next_url_path = try_get(
  358. data,
  359. (lambda x: x['_links']['next']['href'],
  360. lambda x: x['_embedded'][assets_key]['_links']['next']['href']),
  361. compat_str)
  362. if not next_url_path:
  363. break
  364. data = self._call_api(
  365. next_url_path, display_id,
  366. note='Downloading %s JSON page %d' % (assets_key, page_num),
  367. fatal=False)
  368. if not data:
  369. break
  370. class NRKTVSeasonIE(NRKTVSerieBaseIE):
  371. _VALID_URL = r'https?://(?P<domain>tv|radio)\.nrk\.no/serie/(?P<serie>[^/]+)/(?:sesong/)?(?P<id>\d+)'
  372. _TESTS = [{
  373. 'url': 'https://tv.nrk.no/serie/backstage/sesong/1',
  374. 'info_dict': {
  375. 'id': 'backstage/1',
  376. 'title': 'Sesong 1',
  377. },
  378. 'playlist_mincount': 30,
  379. }, {
  380. # no /sesong/ in path
  381. 'url': 'https://tv.nrk.no/serie/lindmo/2016',
  382. 'info_dict': {
  383. 'id': 'lindmo/2016',
  384. 'title': '2016',
  385. },
  386. 'playlist_mincount': 29,
  387. }, {
  388. # weird nested _embedded in catalog JSON response
  389. 'url': 'https://radio.nrk.no/serie/dickie-dick-dickens/sesong/1',
  390. 'info_dict': {
  391. 'id': 'dickie-dick-dickens/1',
  392. 'title': 'Sesong 1',
  393. },
  394. 'playlist_mincount': 11,
  395. }, {
  396. # 841 entries, multi page
  397. 'url': 'https://radio.nrk.no/serie/dagsnytt/sesong/201509',
  398. 'info_dict': {
  399. 'id': 'dagsnytt/201509',
  400. 'title': 'September 2015',
  401. },
  402. 'playlist_mincount': 841,
  403. }, {
  404. # 180 entries, single page
  405. 'url': 'https://tv.nrk.no/serie/spangas/sesong/1',
  406. 'only_matching': True,
  407. }]
  408. @classmethod
  409. def suitable(cls, url):
  410. return (False if NRKTVIE.suitable(url) or NRKTVEpisodeIE.suitable(url)
  411. else super(NRKTVSeasonIE, cls).suitable(url))
  412. def _real_extract(self, url):
  413. domain, serie, season_id = re.match(self._VALID_URL, url).groups()
  414. display_id = '%s/%s' % (serie, season_id)
  415. data = self._call_api(
  416. '%s/catalog/series/%s/seasons/%s' % (domain, serie, season_id),
  417. display_id, 'season', query={'pageSize': 50})
  418. title = try_get(data, lambda x: x['titles']['title'], compat_str) or display_id
  419. return self.playlist_result(
  420. self._entries(data, display_id),
  421. display_id, title)
  422. class NRKTVSeriesIE(NRKTVSerieBaseIE):
  423. _VALID_URL = r'https?://(?P<domain>(?:tv|radio)\.nrk|(?:tv\.)?nrksuper)\.no/serie/(?P<id>[^/]+)'
  424. _TESTS = [{
  425. # new layout, instalments
  426. 'url': 'https://tv.nrk.no/serie/groenn-glede',
  427. 'info_dict': {
  428. 'id': 'groenn-glede',
  429. 'title': 'Grønn glede',
  430. 'description': 'md5:7576e92ae7f65da6993cf90ee29e4608',
  431. },
  432. 'playlist_mincount': 90,
  433. }, {
  434. # new layout, instalments, more entries
  435. 'url': 'https://tv.nrk.no/serie/lindmo',
  436. 'only_matching': True,
  437. }, {
  438. 'url': 'https://tv.nrk.no/serie/blank',
  439. 'info_dict': {
  440. 'id': 'blank',
  441. 'title': 'Blank',
  442. 'description': 'md5:7664b4e7e77dc6810cd3bca367c25b6e',
  443. },
  444. 'playlist_mincount': 30,
  445. }, {
  446. # new layout, seasons
  447. 'url': 'https://tv.nrk.no/serie/backstage',
  448. 'info_dict': {
  449. 'id': 'backstage',
  450. 'title': 'Backstage',
  451. 'description': 'md5:63692ceb96813d9a207e9910483d948b',
  452. },
  453. 'playlist_mincount': 60,
  454. }, {
  455. # old layout
  456. 'url': 'https://tv.nrksuper.no/serie/labyrint',
  457. 'info_dict': {
  458. 'id': 'labyrint',
  459. 'title': 'Labyrint',
  460. 'description': 'I Daidalos sin undersjøiske Labyrint venter spennende oppgaver, skumle robotskapninger og slim.',
  461. },
  462. 'playlist_mincount': 3,
  463. }, {
  464. 'url': 'https://tv.nrk.no/serie/broedrene-dal-og-spektralsteinene',
  465. 'only_matching': True,
  466. }, {
  467. 'url': 'https://tv.nrk.no/serie/saving-the-human-race',
  468. 'only_matching': True,
  469. }, {
  470. 'url': 'https://tv.nrk.no/serie/postmann-pat',
  471. 'only_matching': True,
  472. }, {
  473. 'url': 'https://radio.nrk.no/serie/dickie-dick-dickens',
  474. 'info_dict': {
  475. 'id': 'dickie-dick-dickens',
  476. 'title': 'Dickie Dick Dickens',
  477. 'description': 'md5:19e67411ffe57f7dce08a943d7a0b91f',
  478. },
  479. 'playlist_mincount': 8,
  480. }, {
  481. 'url': 'https://nrksuper.no/serie/labyrint',
  482. 'only_matching': True,
  483. }]
  484. @classmethod
  485. def suitable(cls, url):
  486. return (
  487. False if any(ie.suitable(url)
  488. for ie in (NRKTVIE, NRKTVEpisodeIE, NRKTVSeasonIE))
  489. else super(NRKTVSeriesIE, cls).suitable(url))
  490. def _real_extract(self, url):
  491. site, series_id = re.match(self._VALID_URL, url).groups()
  492. is_radio = site == 'radio.nrk'
  493. domain = 'radio' if is_radio else 'tv'
  494. size_prefix = 'p' if is_radio else 'embeddedInstalmentsP'
  495. series = self._call_api(
  496. '%s/catalog/series/%s' % (domain, series_id),
  497. series_id, 'serie', query={size_prefix + 'ageSize': 50})
  498. titles = try_get(series, [
  499. lambda x: x['titles'],
  500. lambda x: x[x['type']]['titles'],
  501. lambda x: x[x['seriesType']]['titles'],
  502. ]) or {}
  503. entries = []
  504. entries.extend(self._entries(series, series_id))
  505. embedded = series.get('_embedded') or {}
  506. linked_seasons = try_get(series, lambda x: x['_links']['seasons']) or []
  507. embedded_seasons = embedded.get('seasons') or []
  508. if len(linked_seasons) > len(embedded_seasons):
  509. for season in linked_seasons:
  510. season_name = season.get('name')
  511. if season_name and isinstance(season_name, compat_str):
  512. entries.append(self.url_result(
  513. 'https://%s.nrk.no/serie/%s/sesong/%s'
  514. % (domain, series_id, season_name),
  515. ie=NRKTVSeasonIE.ie_key(),
  516. video_title=season.get('title')))
  517. else:
  518. for season in embedded_seasons:
  519. entries.extend(self._entries(season, series_id))
  520. entries.extend(self._entries(
  521. embedded.get('extraMaterial') or {}, series_id))
  522. return self.playlist_result(
  523. entries, series_id, titles.get('title'), titles.get('subtitle'))
  524. class NRKTVDirekteIE(NRKTVIE):
  525. IE_DESC = 'NRK TV Direkte and NRK Radio Direkte'
  526. _VALID_URL = r'https?://(?:tv|radio)\.nrk\.no/direkte/(?P<id>[^/?#&]+)'
  527. _TESTS = [{
  528. 'url': 'https://tv.nrk.no/direkte/nrk1',
  529. 'only_matching': True,
  530. }, {
  531. 'url': 'https://radio.nrk.no/direkte/p1_oslo_akershus',
  532. 'only_matching': True,
  533. }]
  534. class NRKPlaylistBaseIE(InfoExtractor):
  535. def _extract_description(self, webpage):
  536. pass
  537. def _real_extract(self, url):
  538. playlist_id = self._match_id(url)
  539. webpage = self._download_webpage(url, playlist_id)
  540. entries = [
  541. self.url_result('nrk:%s' % video_id, NRKIE.ie_key())
  542. for video_id in re.findall(self._ITEM_RE, webpage)
  543. ]
  544. playlist_title = self. _extract_title(webpage)
  545. playlist_description = self._extract_description(webpage)
  546. return self.playlist_result(
  547. entries, playlist_id, playlist_title, playlist_description)
  548. class NRKPlaylistIE(NRKPlaylistBaseIE):
  549. _VALID_URL = r'https?://(?:www\.)?nrk\.no/(?!video|skole)(?:[^/]+/)+(?P<id>[^/]+)'
  550. _ITEM_RE = r'class="[^"]*\brich\b[^"]*"[^>]+data-video-id="([^"]+)"'
  551. _TESTS = [{
  552. 'url': 'http://www.nrk.no/troms/gjenopplev-den-historiske-solformorkelsen-1.12270763',
  553. 'info_dict': {
  554. 'id': 'gjenopplev-den-historiske-solformorkelsen-1.12270763',
  555. 'title': 'Gjenopplev den historiske solformørkelsen',
  556. 'description': 'md5:c2df8ea3bac5654a26fc2834a542feed',
  557. },
  558. 'playlist_count': 2,
  559. }, {
  560. 'url': 'http://www.nrk.no/kultur/bok/rivertonprisen-til-karin-fossum-1.12266449',
  561. 'info_dict': {
  562. 'id': 'rivertonprisen-til-karin-fossum-1.12266449',
  563. 'title': 'Rivertonprisen til Karin Fossum',
  564. 'description': 'Første kvinne på 15 år til å vinne krimlitteraturprisen.',
  565. },
  566. 'playlist_count': 2,
  567. }]
  568. def _extract_title(self, webpage):
  569. return self._og_search_title(webpage, fatal=False)
  570. def _extract_description(self, webpage):
  571. return self._og_search_description(webpage)
  572. class NRKTVEpisodesIE(NRKPlaylistBaseIE):
  573. _VALID_URL = r'https?://tv\.nrk\.no/program/[Ee]pisodes/[^/]+/(?P<id>\d+)'
  574. _ITEM_RE = r'data-episode=["\']%s' % NRKTVIE._EPISODE_RE
  575. _TESTS = [{
  576. 'url': 'https://tv.nrk.no/program/episodes/nytt-paa-nytt/69031',
  577. 'info_dict': {
  578. 'id': '69031',
  579. 'title': 'Nytt på nytt, sesong: 201210',
  580. },
  581. 'playlist_count': 4,
  582. }]
  583. def _extract_title(self, webpage):
  584. return self._html_search_regex(
  585. r'<h1>([^<]+)</h1>', webpage, 'title', fatal=False)
  586. class NRKSkoleIE(InfoExtractor):
  587. IE_DESC = 'NRK Skole'
  588. _VALID_URL = r'https?://(?:www\.)?nrk\.no/skole/?\?.*\bmediaId=(?P<id>\d+)'
  589. _TESTS = [{
  590. 'url': 'https://www.nrk.no/skole/?page=search&q=&mediaId=14099',
  591. 'md5': '18c12c3d071953c3bf8d54ef6b2587b7',
  592. 'info_dict': {
  593. 'id': '6021',
  594. 'ext': 'mp4',
  595. 'title': 'Genetikk og eneggede tvillinger',
  596. 'description': 'md5:3aca25dcf38ec30f0363428d2b265f8d',
  597. 'duration': 399,
  598. },
  599. }, {
  600. 'url': 'https://www.nrk.no/skole/?page=objectives&subject=naturfag&objective=K15114&mediaId=19355',
  601. 'only_matching': True,
  602. }]
  603. def _real_extract(self, url):
  604. video_id = self._match_id(url)
  605. nrk_id = self._download_json(
  606. 'https://nrkno-skole-prod.kube.nrk.no/skole/api/media/%s' % video_id,
  607. video_id)['psId']
  608. return self.url_result('nrk:%s' % nrk_id)