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.

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