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.

296 lines
11 KiB

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