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.

290 lines
10 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. _VALID_URL = r'(?P<baseurl>https?://tv\.nrk(?:super)?\.no/)(?:serie/[^/]+|program)/(?P<id>[a-zA-Z]{4}\d{8})(?:/\d{2}-\d{2}-\d{4})?(?:#del=(?P<part_id>\d+))?'
  98. _TESTS = [
  99. {
  100. 'url': 'https://tv.nrk.no/serie/20-spoersmaal-tv/MUHH48000314/23-05-2014',
  101. 'md5': 'adf2c5454fa2bf032f47a9f8fb351342',
  102. 'info_dict': {
  103. 'id': 'MUHH48000314',
  104. 'ext': 'flv',
  105. 'title': '20 spørsmål',
  106. 'description': 'md5:bdea103bc35494c143c6a9acdd84887a',
  107. 'upload_date': '20140523',
  108. 'duration': 1741.52,
  109. },
  110. },
  111. {
  112. 'url': 'https://tv.nrk.no/program/mdfp15000514',
  113. 'md5': '383650ece2b25ecec996ad7b5bb2a384',
  114. 'info_dict': {
  115. 'id': 'mdfp15000514',
  116. 'ext': 'flv',
  117. 'title': 'Kunnskapskanalen: Grunnlovsjubiléet - Stor ståhei for ingenting',
  118. 'description': 'md5:654c12511f035aed1e42bdf5db3b206a',
  119. 'upload_date': '20140524',
  120. 'duration': 4605.0,
  121. },
  122. },
  123. {
  124. # single playlist video
  125. 'url': 'https://tv.nrk.no/serie/tour-de-ski/MSPO40010515/06-01-2015#del=2',
  126. 'md5': 'adbd1dbd813edaf532b0a253780719c2',
  127. 'info_dict': {
  128. 'id': 'MSPO40010515-part2',
  129. 'ext': 'flv',
  130. 'title': 'Tour de Ski: Sprint fri teknikk, kvinner og menn 06.01.2015 (del 2:2)',
  131. 'description': 'md5:238b67b97a4ac7d7b4bf0edf8cc57d26',
  132. 'upload_date': '20150106',
  133. },
  134. 'skip': 'Only works from Norway',
  135. },
  136. {
  137. 'url': 'https://tv.nrk.no/serie/tour-de-ski/MSPO40010515/06-01-2015',
  138. 'playlist': [
  139. {
  140. 'md5': '9480285eff92d64f06e02a5367970a7a',
  141. 'info_dict': {
  142. 'id': 'MSPO40010515-part1',
  143. 'ext': 'flv',
  144. 'title': 'Tour de Ski: Sprint fri teknikk, kvinner og menn 06.01.2015 (del 1:2)',
  145. 'description': 'md5:238b67b97a4ac7d7b4bf0edf8cc57d26',
  146. 'upload_date': '20150106',
  147. },
  148. },
  149. {
  150. 'md5': 'adbd1dbd813edaf532b0a253780719c2',
  151. 'info_dict': {
  152. 'id': 'MSPO40010515-part2',
  153. 'ext': 'flv',
  154. 'title': 'Tour de Ski: Sprint fri teknikk, kvinner og menn 06.01.2015 (del 2:2)',
  155. 'description': 'md5:238b67b97a4ac7d7b4bf0edf8cc57d26',
  156. 'upload_date': '20150106',
  157. },
  158. },
  159. ],
  160. 'info_dict': {
  161. 'id': 'MSPO40010515',
  162. 'title': 'Tour de Ski: Sprint fri teknikk, kvinner og menn',
  163. 'description': 'md5:238b67b97a4ac7d7b4bf0edf8cc57d26',
  164. 'upload_date': '20150106',
  165. 'duration': 6947.5199999999995,
  166. },
  167. 'skip': 'Only works from Norway',
  168. }
  169. ]
  170. def _debug_print(self, txt):
  171. if self._downloader.params.get('verbose', False):
  172. self.to_screen('[debug] %s' % txt)
  173. def _get_subtitles(self, subtitlesurl, video_id, baseurl):
  174. url = "%s%s" % (baseurl, subtitlesurl)
  175. self._debug_print('%s: Subtitle url: %s' % (video_id, url))
  176. captions = self._download_xml(
  177. url, video_id, 'Downloading subtitles')
  178. lang = captions.get('lang', 'no')
  179. return {lang: [
  180. {'ext': 'ttml', 'url': url},
  181. ]}
  182. def _extract_f4m(self, manifest_url, video_id):
  183. return self._extract_f4m_formats(manifest_url + '?hdcore=3.1.1&plugin=aasp-3.1.1.69.124', video_id)
  184. def _real_extract(self, url):
  185. mobj = re.match(self._VALID_URL, url)
  186. video_id = mobj.group('id')
  187. part_id = mobj.group('part_id')
  188. baseurl = mobj.group('baseurl')
  189. webpage = self._download_webpage(url, video_id)
  190. title = self._html_search_meta(
  191. 'title', webpage, 'title')
  192. description = self._html_search_meta(
  193. 'description', webpage, 'description')
  194. thumbnail = self._html_search_regex(
  195. r'data-posterimage="([^"]+)"',
  196. webpage, 'thumbnail', fatal=False)
  197. upload_date = unified_strdate(self._html_search_meta(
  198. 'rightsfrom', webpage, 'upload date', fatal=False))
  199. duration = float_or_none(self._html_search_regex(
  200. r'data-duration="([^"]+)"',
  201. webpage, 'duration', fatal=False))
  202. # playlist
  203. parts = re.findall(
  204. r'<a href="#del=(\d+)"[^>]+data-argument="([^"]+)">([^<]+)</a>', webpage)
  205. if parts:
  206. entries = []
  207. for current_part_id, stream_url, part_title in parts:
  208. if part_id and current_part_id != part_id:
  209. continue
  210. video_part_id = '%s-part%s' % (video_id, current_part_id)
  211. formats = self._extract_f4m(stream_url, video_part_id)
  212. entries.append({
  213. 'id': video_part_id,
  214. 'title': part_title,
  215. 'description': description,
  216. 'thumbnail': thumbnail,
  217. 'upload_date': upload_date,
  218. 'formats': formats,
  219. })
  220. if part_id:
  221. if entries:
  222. return entries[0]
  223. else:
  224. playlist = self.playlist_result(entries, video_id, title, description)
  225. playlist.update({
  226. 'thumbnail': thumbnail,
  227. 'upload_date': upload_date,
  228. 'duration': duration,
  229. })
  230. return playlist
  231. formats = []
  232. f4m_url = re.search(r'data-media="([^"]+)"', webpage)
  233. if f4m_url:
  234. formats.extend(self._extract_f4m(f4m_url.group(1), video_id))
  235. m3u8_url = re.search(r'data-hls-media="([^"]+)"', webpage)
  236. if m3u8_url:
  237. formats.extend(self._extract_m3u8_formats(m3u8_url.group(1), video_id, 'mp4'))
  238. self._sort_formats(formats)
  239. subtitles_url = self._html_search_regex(
  240. r'data-subtitlesurl[ ]*=[ ]*"([^"]+)"',
  241. webpage, 'subtitle URL', default=None)
  242. subtitles = None
  243. if subtitles_url:
  244. subtitles = self.extract_subtitles(subtitles_url, video_id, baseurl)
  245. return {
  246. 'id': video_id,
  247. 'title': title,
  248. 'description': description,
  249. 'thumbnail': thumbnail,
  250. 'upload_date': upload_date,
  251. 'duration': duration,
  252. 'formats': formats,
  253. 'subtitles': subtitles,
  254. }