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.

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