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