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.

336 lines
12 KiB

  1. # encoding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..compat import (
  6. compat_urlparse,
  7. compat_urllib_parse_unquote,
  8. )
  9. from ..utils import (
  10. determine_ext,
  11. ExtractorError,
  12. float_or_none,
  13. parse_duration,
  14. unified_strdate,
  15. )
  16. class NRKIE(InfoExtractor):
  17. _VALID_URL = r'(?:nrk:|https?://(?:www\.)?nrk\.no/video/PS\*)(?P<id>\d+)'
  18. _TESTS = [
  19. {
  20. 'url': 'http://www.nrk.no/video/PS*150533',
  21. 'md5': 'bccd850baebefe23b56d708a113229c2',
  22. 'info_dict': {
  23. 'id': '150533',
  24. 'ext': 'flv',
  25. 'title': 'Dompap og andre fugler i Piip-Show',
  26. 'description': 'md5:d9261ba34c43b61c812cb6b0269a5c8f',
  27. 'duration': 263,
  28. }
  29. },
  30. {
  31. 'url': 'http://www.nrk.no/video/PS*154915',
  32. 'md5': '0b1493ba1aae7d9579a5ad5531bc395a',
  33. 'info_dict': {
  34. 'id': '154915',
  35. 'ext': 'flv',
  36. 'title': 'Slik høres internett ut når du er blind',
  37. 'description': 'md5:a621f5cc1bd75c8d5104cb048c6b8568',
  38. 'duration': 20,
  39. }
  40. },
  41. ]
  42. def _real_extract(self, url):
  43. video_id = self._match_id(url)
  44. data = self._download_json(
  45. 'http://v8.psapi.nrk.no/mediaelement/%s' % video_id,
  46. video_id, 'Downloading media JSON')
  47. media_url = data.get('mediaUrl')
  48. if not media_url:
  49. if data['usageRights']['isGeoBlocked']:
  50. raise ExtractorError(
  51. 'NRK har ikke rettigheter til å vise dette programmet utenfor Norge',
  52. expected=True)
  53. if determine_ext(media_url) == 'f4m':
  54. formats = self._extract_f4m_formats(
  55. media_url + '?hdcore=3.5.0&plugin=aasp-3.5.0.151.81', video_id, f4m_id='hds')
  56. else:
  57. formats = [{
  58. 'url': media_url,
  59. 'ext': 'flv',
  60. }]
  61. duration = parse_duration(data.get('duration'))
  62. images = data.get('images')
  63. if images:
  64. thumbnails = images['webImages']
  65. thumbnails.sort(key=lambda image: image['pixelWidth'])
  66. thumbnail = thumbnails[-1]['imageUrl']
  67. else:
  68. thumbnail = None
  69. return {
  70. 'id': video_id,
  71. 'title': data['title'],
  72. 'description': data['description'],
  73. 'duration': duration,
  74. 'thumbnail': thumbnail,
  75. 'formats': formats,
  76. }
  77. class NRKPlaylistIE(InfoExtractor):
  78. _VALID_URL = r'https?://(?:www\.)?nrk\.no/(?!video|skole)(?:[^/]+/)+(?P<id>[^/]+)'
  79. _TESTS = [{
  80. 'url': 'http://www.nrk.no/troms/gjenopplev-den-historiske-solformorkelsen-1.12270763',
  81. 'info_dict': {
  82. 'id': 'gjenopplev-den-historiske-solformorkelsen-1.12270763',
  83. 'title': 'Gjenopplev den historiske solformørkelsen',
  84. 'description': 'md5:c2df8ea3bac5654a26fc2834a542feed',
  85. },
  86. 'playlist_count': 2,
  87. }, {
  88. 'url': 'http://www.nrk.no/kultur/bok/rivertonprisen-til-karin-fossum-1.12266449',
  89. 'info_dict': {
  90. 'id': 'rivertonprisen-til-karin-fossum-1.12266449',
  91. 'title': 'Rivertonprisen til Karin Fossum',
  92. 'description': 'Første kvinne på 15 år til å vinne krimlitteraturprisen.',
  93. },
  94. 'playlist_count': 5,
  95. }]
  96. def _real_extract(self, url):
  97. playlist_id = self._match_id(url)
  98. webpage = self._download_webpage(url, playlist_id)
  99. entries = [
  100. self.url_result('nrk:%s' % video_id, 'NRK')
  101. for video_id in re.findall(
  102. r'class="[^"]*\brich\b[^"]*"[^>]+data-video-id="([^"]+)"',
  103. webpage)
  104. ]
  105. playlist_title = self._og_search_title(webpage)
  106. playlist_description = self._og_search_description(webpage)
  107. return self.playlist_result(
  108. entries, playlist_id, playlist_title, playlist_description)
  109. class NRKSkoleIE(InfoExtractor):
  110. IE_DESC = 'NRK Skole'
  111. _VALID_URL = r'https?://(?:www\.)?nrk\.no/skole/klippdetalj?.*\btopic=(?P<id>[^/?#&]+)'
  112. _TESTS = [{
  113. 'url': 'http://nrk.no/skole/klippdetalj?topic=nrk:klipp/616532',
  114. 'md5': '04cd85877cc1913bce73c5d28a47e00f',
  115. 'info_dict': {
  116. 'id': '6021',
  117. 'ext': 'flv',
  118. 'title': 'Genetikk og eneggede tvillinger',
  119. 'description': 'md5:3aca25dcf38ec30f0363428d2b265f8d',
  120. 'duration': 399,
  121. },
  122. }, {
  123. 'url': 'http://www.nrk.no/skole/klippdetalj?topic=nrk%3Aklipp%2F616532#embed',
  124. 'only_matching': True,
  125. }, {
  126. 'url': 'http://www.nrk.no/skole/klippdetalj?topic=urn:x-mediadb:21379',
  127. 'only_matching': True,
  128. }]
  129. def _real_extract(self, url):
  130. video_id = compat_urllib_parse_unquote(self._match_id(url))
  131. webpage = self._download_webpage(url, video_id)
  132. nrk_id = self._search_regex(r'data-nrk-id=["\'](\d+)', webpage, 'nrk id')
  133. return self.url_result('nrk:%s' % nrk_id)
  134. class NRKTVIE(InfoExtractor):
  135. IE_DESC = 'NRK TV and NRK Radio'
  136. _VALID_URL = r'(?P<baseurl>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+))?'
  137. _TESTS = [
  138. {
  139. 'url': 'https://tv.nrk.no/serie/20-spoersmaal-tv/MUHH48000314/23-05-2014',
  140. 'info_dict': {
  141. 'id': 'MUHH48000314',
  142. 'ext': 'mp4',
  143. 'title': '20 spørsmål',
  144. 'description': 'md5:bdea103bc35494c143c6a9acdd84887a',
  145. 'upload_date': '20140523',
  146. 'duration': 1741.52,
  147. },
  148. 'params': {
  149. # m3u8 download
  150. 'skip_download': True,
  151. },
  152. },
  153. {
  154. 'url': 'https://tv.nrk.no/program/mdfp15000514',
  155. 'info_dict': {
  156. 'id': 'mdfp15000514',
  157. 'ext': 'mp4',
  158. 'title': 'Grunnlovsjubiléet - Stor ståhei for ingenting',
  159. 'description': 'md5:654c12511f035aed1e42bdf5db3b206a',
  160. 'upload_date': '20140524',
  161. 'duration': 4605.08,
  162. },
  163. 'params': {
  164. # m3u8 download
  165. 'skip_download': True,
  166. },
  167. },
  168. {
  169. # single playlist video
  170. 'url': 'https://tv.nrk.no/serie/tour-de-ski/MSPO40010515/06-01-2015#del=2',
  171. 'md5': 'adbd1dbd813edaf532b0a253780719c2',
  172. 'info_dict': {
  173. 'id': 'MSPO40010515-part2',
  174. 'ext': 'flv',
  175. 'title': 'Tour de Ski: Sprint fri teknikk, kvinner og menn 06.01.2015 (del 2:2)',
  176. 'description': 'md5:238b67b97a4ac7d7b4bf0edf8cc57d26',
  177. 'upload_date': '20150106',
  178. },
  179. 'skip': 'Only works from Norway',
  180. },
  181. {
  182. 'url': 'https://tv.nrk.no/serie/tour-de-ski/MSPO40010515/06-01-2015',
  183. 'playlist': [
  184. {
  185. 'md5': '9480285eff92d64f06e02a5367970a7a',
  186. 'info_dict': {
  187. 'id': 'MSPO40010515-part1',
  188. 'ext': 'flv',
  189. 'title': 'Tour de Ski: Sprint fri teknikk, kvinner og menn 06.01.2015 (del 1:2)',
  190. 'description': 'md5:238b67b97a4ac7d7b4bf0edf8cc57d26',
  191. 'upload_date': '20150106',
  192. },
  193. },
  194. {
  195. 'md5': 'adbd1dbd813edaf532b0a253780719c2',
  196. 'info_dict': {
  197. 'id': 'MSPO40010515-part2',
  198. 'ext': 'flv',
  199. 'title': 'Tour de Ski: Sprint fri teknikk, kvinner og menn 06.01.2015 (del 2:2)',
  200. 'description': 'md5:238b67b97a4ac7d7b4bf0edf8cc57d26',
  201. 'upload_date': '20150106',
  202. },
  203. },
  204. ],
  205. 'info_dict': {
  206. 'id': 'MSPO40010515',
  207. 'title': 'Tour de Ski: Sprint fri teknikk, kvinner og menn',
  208. 'description': 'md5:238b67b97a4ac7d7b4bf0edf8cc57d26',
  209. 'upload_date': '20150106',
  210. 'duration': 6947.5199999999995,
  211. },
  212. 'skip': 'Only works from Norway',
  213. },
  214. {
  215. 'url': 'https://radio.nrk.no/serie/dagsnytt/NPUB21019315/12-07-2015#',
  216. 'only_matching': True,
  217. }
  218. ]
  219. def _extract_f4m(self, manifest_url, video_id):
  220. return self._extract_f4m_formats(
  221. manifest_url + '?hdcore=3.1.1&plugin=aasp-3.1.1.69.124', video_id, f4m_id='hds')
  222. def _real_extract(self, url):
  223. mobj = re.match(self._VALID_URL, url)
  224. video_id = mobj.group('id')
  225. part_id = mobj.group('part_id')
  226. base_url = mobj.group('baseurl')
  227. webpage = self._download_webpage(url, video_id)
  228. title = self._html_search_meta(
  229. 'title', webpage, 'title')
  230. description = self._html_search_meta(
  231. 'description', webpage, 'description')
  232. thumbnail = self._html_search_regex(
  233. r'data-posterimage="([^"]+)"',
  234. webpage, 'thumbnail', fatal=False)
  235. upload_date = unified_strdate(self._html_search_meta(
  236. 'rightsfrom', webpage, 'upload date', fatal=False))
  237. duration = float_or_none(self._html_search_regex(
  238. r'data-duration="([^"]+)"',
  239. webpage, 'duration', fatal=False))
  240. # playlist
  241. parts = re.findall(
  242. r'<a href="#del=(\d+)"[^>]+data-argument="([^"]+)">([^<]+)</a>', webpage)
  243. if parts:
  244. entries = []
  245. for current_part_id, stream_url, part_title in parts:
  246. if part_id and current_part_id != part_id:
  247. continue
  248. video_part_id = '%s-part%s' % (video_id, current_part_id)
  249. formats = self._extract_f4m(stream_url, video_part_id)
  250. entries.append({
  251. 'id': video_part_id,
  252. 'title': part_title,
  253. 'description': description,
  254. 'thumbnail': thumbnail,
  255. 'upload_date': upload_date,
  256. 'formats': formats,
  257. })
  258. if part_id:
  259. if entries:
  260. return entries[0]
  261. else:
  262. playlist = self.playlist_result(entries, video_id, title, description)
  263. playlist.update({
  264. 'thumbnail': thumbnail,
  265. 'upload_date': upload_date,
  266. 'duration': duration,
  267. })
  268. return playlist
  269. formats = []
  270. f4m_url = re.search(r'data-media="([^"]+)"', webpage)
  271. if f4m_url:
  272. formats.extend(self._extract_f4m(f4m_url.group(1), video_id))
  273. m3u8_url = re.search(r'data-hls-media="([^"]+)"', webpage)
  274. if m3u8_url:
  275. formats.extend(self._extract_m3u8_formats(m3u8_url.group(1), video_id, 'mp4', m3u8_id='hls'))
  276. self._sort_formats(formats)
  277. subtitles_url = self._html_search_regex(
  278. r'data-subtitlesurl\s*=\s*(["\'])(?P<url>.+?)\1',
  279. webpage, 'subtitle URL', default=None, group='url')
  280. subtitles = {}
  281. if subtitles_url:
  282. subtitles['no'] = [{
  283. 'ext': 'ttml',
  284. 'url': compat_urlparse.urljoin(base_url, subtitles_url),
  285. }]
  286. return {
  287. 'id': video_id,
  288. 'title': title,
  289. 'description': description,
  290. 'thumbnail': thumbnail,
  291. 'upload_date': upload_date,
  292. 'duration': duration,
  293. 'formats': formats,
  294. 'subtitles': subtitles,
  295. }