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.

301 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_str
  6. from ..utils import (
  7. ExtractorError,
  8. float_or_none,
  9. parse_duration,
  10. unified_strdate,
  11. )
  12. class NRKIE(InfoExtractor):
  13. _VALID_URL = r'(?:nrk:|http://(?:www\.)?nrk\.no/video/PS\*)(?P<id>\d+)'
  14. _TESTS = [
  15. {
  16. 'url': 'http://www.nrk.no/video/PS*150533',
  17. 'md5': 'bccd850baebefe23b56d708a113229c2',
  18. 'info_dict': {
  19. 'id': '150533',
  20. 'ext': 'flv',
  21. 'title': 'Dompap og andre fugler i Piip-Show',
  22. 'description': 'md5:d9261ba34c43b61c812cb6b0269a5c8f',
  23. 'duration': 263,
  24. }
  25. },
  26. {
  27. 'url': 'http://www.nrk.no/video/PS*154915',
  28. 'md5': '0b1493ba1aae7d9579a5ad5531bc395a',
  29. 'info_dict': {
  30. 'id': '154915',
  31. 'ext': 'flv',
  32. 'title': 'Slik høres internett ut når du er blind',
  33. 'description': 'md5:a621f5cc1bd75c8d5104cb048c6b8568',
  34. 'duration': 20,
  35. }
  36. },
  37. ]
  38. def _real_extract(self, url):
  39. video_id = self._match_id(url)
  40. data = self._download_json(
  41. 'http://v8.psapi.nrk.no/mediaelement/%s' % video_id,
  42. video_id, 'Downloading media JSON')
  43. if data['usageRights']['isGeoBlocked']:
  44. raise ExtractorError(
  45. 'NRK har ikke rettig-heter til å vise dette programmet utenfor Norge',
  46. expected=True)
  47. video_url = data['mediaUrl'] + '?hdcore=3.5.0&plugin=aasp-3.5.0.151.81'
  48. duration = parse_duration(data.get('duration'))
  49. images = data.get('images')
  50. if images:
  51. thumbnails = images['webImages']
  52. thumbnails.sort(key=lambda image: image['pixelWidth'])
  53. thumbnail = thumbnails[-1]['imageUrl']
  54. else:
  55. thumbnail = None
  56. return {
  57. 'id': video_id,
  58. 'url': video_url,
  59. 'ext': 'flv',
  60. 'title': data['title'],
  61. 'description': data['description'],
  62. 'duration': duration,
  63. 'thumbnail': thumbnail,
  64. }
  65. class NRKPlaylistIE(InfoExtractor):
  66. _VALID_URL = r'http://(?:www\.)?nrk\.no/(?!video)(?:[^/]+/)+(?P<id>[^/]+)'
  67. _TESTS = [{
  68. 'url': 'http://www.nrk.no/troms/gjenopplev-den-historiske-solformorkelsen-1.12270763',
  69. 'info_dict': {
  70. 'id': 'gjenopplev-den-historiske-solformorkelsen-1.12270763',
  71. 'title': 'Gjenopplev den historiske solformørkelsen',
  72. 'description': 'md5:c2df8ea3bac5654a26fc2834a542feed',
  73. },
  74. 'playlist_count': 2,
  75. }, {
  76. 'url': 'http://www.nrk.no/kultur/bok/rivertonprisen-til-karin-fossum-1.12266449',
  77. 'info_dict': {
  78. 'id': 'rivertonprisen-til-karin-fossum-1.12266449',
  79. 'title': 'Rivertonprisen til Karin Fossum',
  80. 'description': 'Første kvinne på 15 år til å vinne krimlitteraturprisen.',
  81. },
  82. 'playlist_count': 5,
  83. }]
  84. def _real_extract(self, url):
  85. playlist_id = self._match_id(url)
  86. webpage = self._download_webpage(url, playlist_id)
  87. entries = [
  88. self.url_result('nrk:%s' % video_id, 'NRK')
  89. for video_id in re.findall(
  90. r'class="[^"]*\brich\b[^"]*"[^>]+data-video-id="([^"]+)"',
  91. webpage)
  92. ]
  93. playlist_title = self._og_search_title(webpage)
  94. playlist_description = self._og_search_description(webpage)
  95. return self.playlist_result(
  96. entries, playlist_id, playlist_title, playlist_description)
  97. class NRKTVIE(InfoExtractor):
  98. _VALID_URL = r'(?P<baseurl>http://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+))?'
  99. _TESTS = [
  100. {
  101. 'url': 'http://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': 'http://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': 'http://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': 'http://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. def _debug_print(self, txt):
  172. if self._downloader.params.get('verbose', False):
  173. self.to_screen('[debug] %s' % txt)
  174. def _get_subtitles(self, subtitlesurl, video_id, baseurl):
  175. url = "%s%s" % (baseurl, subtitlesurl)
  176. self._debug_print('%s: Subtitle url: %s' % (video_id, url))
  177. captions = self._download_xml(
  178. url, video_id, 'Downloading subtitles',
  179. transform_source=lambda s: s.replace(r'<br />', '\r\n'))
  180. lang = captions.get('lang', 'no')
  181. ps = captions.findall('./{0}body/{0}div/{0}p'.format('{http://www.w3.org/ns/ttml}'))
  182. srt = ''
  183. for pos, p in enumerate(ps):
  184. begin = parse_duration(p.get('begin'))
  185. duration = parse_duration(p.get('dur'))
  186. starttime = self._subtitles_timecode(begin)
  187. endtime = self._subtitles_timecode(begin + duration)
  188. srt += '%s\r\n%s --> %s\r\n%s\r\n\r\n' % (compat_str(pos), starttime, endtime, p.text)
  189. return {lang: [
  190. {'ext': 'ttml', 'url': url},
  191. {'ext': 'srt', 'data': srt},
  192. ]}
  193. def _extract_f4m(self, manifest_url, video_id):
  194. return self._extract_f4m_formats(manifest_url + '?hdcore=3.1.1&plugin=aasp-3.1.1.69.124', video_id)
  195. def _real_extract(self, url):
  196. mobj = re.match(self._VALID_URL, url)
  197. video_id = mobj.group('id')
  198. part_id = mobj.group('part_id')
  199. baseurl = mobj.group('baseurl')
  200. webpage = self._download_webpage(url, video_id)
  201. title = self._html_search_meta(
  202. 'title', webpage, 'title')
  203. description = self._html_search_meta(
  204. 'description', webpage, 'description')
  205. thumbnail = self._html_search_regex(
  206. r'data-posterimage="([^"]+)"',
  207. webpage, 'thumbnail', fatal=False)
  208. upload_date = unified_strdate(self._html_search_meta(
  209. 'rightsfrom', webpage, 'upload date', fatal=False))
  210. duration = float_or_none(self._html_search_regex(
  211. r'data-duration="([^"]+)"',
  212. webpage, 'duration', fatal=False))
  213. # playlist
  214. parts = re.findall(
  215. r'<a href="#del=(\d+)"[^>]+data-argument="([^"]+)">([^<]+)</a>', webpage)
  216. if parts:
  217. entries = []
  218. for current_part_id, stream_url, part_title in parts:
  219. if part_id and current_part_id != part_id:
  220. continue
  221. video_part_id = '%s-part%s' % (video_id, current_part_id)
  222. formats = self._extract_f4m(stream_url, video_part_id)
  223. entries.append({
  224. 'id': video_part_id,
  225. 'title': part_title,
  226. 'description': description,
  227. 'thumbnail': thumbnail,
  228. 'upload_date': upload_date,
  229. 'formats': formats,
  230. })
  231. if part_id:
  232. if entries:
  233. return entries[0]
  234. else:
  235. playlist = self.playlist_result(entries, video_id, title, description)
  236. playlist.update({
  237. 'thumbnail': thumbnail,
  238. 'upload_date': upload_date,
  239. 'duration': duration,
  240. })
  241. return playlist
  242. formats = []
  243. f4m_url = re.search(r'data-media="([^"]+)"', webpage)
  244. if f4m_url:
  245. formats.extend(self._extract_f4m(f4m_url.group(1), video_id))
  246. m3u8_url = re.search(r'data-hls-media="([^"]+)"', webpage)
  247. if m3u8_url:
  248. formats.extend(self._extract_m3u8_formats(m3u8_url.group(1), video_id, 'mp4'))
  249. self._sort_formats(formats)
  250. subtitles_url = self._html_search_regex(
  251. r'data-subtitlesurl[ ]*=[ ]*"([^"]+)"',
  252. webpage, 'subtitle URL', default=None)
  253. subtitles = None
  254. if subtitles_url:
  255. subtitles = self.extract_subtitles(subtitles_url, video_id, baseurl)
  256. return {
  257. 'id': video_id,
  258. 'title': title,
  259. 'description': description,
  260. 'thumbnail': thumbnail,
  261. 'upload_date': upload_date,
  262. 'duration': duration,
  263. 'formats': formats,
  264. 'subtitles': subtitles,
  265. }