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.

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