You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

293 lines
11 KiB

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