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.

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