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.

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