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.

260 lines
9.8 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. from .subtitles import SubtitlesInfoExtractor
  12. class NRKIE(InfoExtractor):
  13. _VALID_URL = r'http://(?:www\.)?nrk\.no/(?:video|lyd)/[^/]+/(?P<id>[\dA-F]{16})'
  14. _TESTS = [
  15. {
  16. 'url': 'http://www.nrk.no/video/dompap_og_andre_fugler_i_piip_show/D0FA54B5C8B6CE59/emne/piipshow/',
  17. 'md5': 'a6eac35052f3b242bb6bb7f43aed5886',
  18. 'info_dict': {
  19. 'id': '150533',
  20. 'ext': 'flv',
  21. 'title': 'Dompap og andre fugler i Piip-Show',
  22. 'description': 'md5:d9261ba34c43b61c812cb6b0269a5c8f'
  23. }
  24. },
  25. {
  26. 'url': 'http://www.nrk.no/lyd/lyd_av_oppleser_for_blinde/AEFDDD5473BA0198/',
  27. 'md5': '3471f2a51718195164e88f46bf427668',
  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. }
  34. },
  35. ]
  36. def _real_extract(self, url):
  37. mobj = re.match(self._VALID_URL, url)
  38. video_id = mobj.group('id')
  39. page = self._download_webpage(url, video_id)
  40. video_id = self._html_search_regex(r'<div class="nrk-video" data-nrk-id="(\d+)">', page, 'video id')
  41. data = self._download_json(
  42. 'http://v7.psapi.nrk.no/mediaelement/%s' % video_id, video_id, 'Downloading media JSON')
  43. if data['usageRights']['isGeoBlocked']:
  44. raise ExtractorError('NRK har ikke rettig-heter til å vise dette programmet utenfor Norge', expected=True)
  45. video_url = data['mediaUrl'] + '?hdcore=3.1.1&plugin=aasp-3.1.1.69.124'
  46. images = data.get('images')
  47. if images:
  48. thumbnails = images['webImages']
  49. thumbnails.sort(key=lambda image: image['pixelWidth'])
  50. thumbnail = thumbnails[-1]['imageUrl']
  51. else:
  52. thumbnail = None
  53. return {
  54. 'id': video_id,
  55. 'url': video_url,
  56. 'ext': 'flv',
  57. 'title': data['title'],
  58. 'description': data['description'],
  59. 'thumbnail': thumbnail,
  60. }
  61. class NRKTVIE(SubtitlesInfoExtractor):
  62. _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+))?'
  63. _TESTS = [
  64. {
  65. 'url': 'http://tv.nrk.no/serie/20-spoersmaal-tv/MUHH48000314/23-05-2014',
  66. 'md5': 'adf2c5454fa2bf032f47a9f8fb351342',
  67. 'info_dict': {
  68. 'id': 'MUHH48000314',
  69. 'ext': 'flv',
  70. 'title': '20 spørsmål',
  71. 'description': 'md5:bdea103bc35494c143c6a9acdd84887a',
  72. 'upload_date': '20140523',
  73. 'duration': 1741.52,
  74. },
  75. },
  76. {
  77. 'url': 'http://tv.nrk.no/program/mdfp15000514',
  78. 'md5': '383650ece2b25ecec996ad7b5bb2a384',
  79. 'info_dict': {
  80. 'id': 'mdfp15000514',
  81. 'ext': 'flv',
  82. 'title': 'Kunnskapskanalen: Grunnlovsjubiléet - Stor ståhei for ingenting',
  83. 'description': 'md5:654c12511f035aed1e42bdf5db3b206a',
  84. 'upload_date': '20140524',
  85. 'duration': 4605.0,
  86. },
  87. },
  88. {
  89. # single playlist video
  90. 'url': 'http://tv.nrk.no/serie/tour-de-ski/MSPO40010515/06-01-2015#del=2',
  91. 'md5': 'adbd1dbd813edaf532b0a253780719c2',
  92. 'info_dict': {
  93. 'id': 'MSPO40010515-part2',
  94. 'ext': 'flv',
  95. 'title': 'Tour de Ski: Sprint fri teknikk, kvinner og menn 06.01.2015 (del 2:2)',
  96. 'description': 'md5:238b67b97a4ac7d7b4bf0edf8cc57d26',
  97. 'upload_date': '20150106',
  98. },
  99. 'skip': 'Only works from Norway',
  100. },
  101. {
  102. 'url': 'http://tv.nrk.no/serie/tour-de-ski/MSPO40010515/06-01-2015',
  103. 'playlist': [
  104. {
  105. 'md5': '9480285eff92d64f06e02a5367970a7a',
  106. 'info_dict': {
  107. 'id': 'MSPO40010515-part1',
  108. 'ext': 'flv',
  109. 'title': 'Tour de Ski: Sprint fri teknikk, kvinner og menn 06.01.2015 (del 1:2)',
  110. 'description': 'md5:238b67b97a4ac7d7b4bf0edf8cc57d26',
  111. 'upload_date': '20150106',
  112. },
  113. },
  114. {
  115. 'md5': 'adbd1dbd813edaf532b0a253780719c2',
  116. 'info_dict': {
  117. 'id': 'MSPO40010515-part2',
  118. 'ext': 'flv',
  119. 'title': 'Tour de Ski: Sprint fri teknikk, kvinner og menn 06.01.2015 (del 2:2)',
  120. 'description': 'md5:238b67b97a4ac7d7b4bf0edf8cc57d26',
  121. 'upload_date': '20150106',
  122. },
  123. },
  124. ],
  125. 'info_dict': {
  126. 'id': 'MSPO40010515',
  127. 'title': 'Tour de Ski: Sprint fri teknikk, kvinner og menn',
  128. 'description': 'md5:238b67b97a4ac7d7b4bf0edf8cc57d26',
  129. 'upload_date': '20150106',
  130. 'duration': 6947.5199999999995,
  131. },
  132. 'skip': 'Only works from Norway',
  133. }
  134. ]
  135. def _seconds2str(self, s):
  136. return '%02d:%02d:%02d.%03d' % (s / 3600, (s % 3600) / 60, s % 60, (s % 1) * 1000)
  137. def _debug_print(self, txt):
  138. if self._downloader.params.get('verbose', False):
  139. self.to_screen('[debug] %s' % txt)
  140. def _extract_captions(self, subtitlesurl, video_id, baseurl):
  141. url = "%s%s" % (baseurl, subtitlesurl)
  142. self._debug_print('%s: Subtitle url: %s' % (video_id, url))
  143. captions = self._download_xml(url, video_id, 'Downloading subtitles')
  144. lang = captions.get('lang', 'no')
  145. ps = captions.findall('./{0}body/{0}div/{0}p'.format('{http://www.w3.org/ns/ttml}'))
  146. srt = ''
  147. for pos, p in enumerate(ps):
  148. begin = parse_duration(p.get('begin'))
  149. duration = parse_duration(p.get('dur'))
  150. starttime = self._seconds2str(begin)
  151. endtime = self._seconds2str(begin + duration)
  152. text = '\n'.join(p.itertext())
  153. srt += '%s\r\n%s --> %s\r\n%s\r\n\r\n' % (str(pos), starttime, endtime, text)
  154. return {lang: srt}
  155. def _extract_f4m(self, manifest_url, video_id):
  156. return self._extract_f4m_formats(manifest_url + '?hdcore=3.1.1&plugin=aasp-3.1.1.69.124', video_id)
  157. def _real_extract(self, url):
  158. mobj = re.match(self._VALID_URL, url)
  159. video_id = mobj.group('id')
  160. part_id = mobj.group('part_id')
  161. baseurl = mobj.group('baseurl')
  162. webpage = self._download_webpage(url, video_id)
  163. title = self._html_search_meta(
  164. 'title', webpage, 'title')
  165. description = self._html_search_meta(
  166. 'description', webpage, 'description')
  167. thumbnail = self._html_search_regex(
  168. r'data-posterimage="([^"]+)"',
  169. webpage, 'thumbnail', fatal=False)
  170. upload_date = unified_strdate(self._html_search_meta(
  171. 'rightsfrom', webpage, 'upload date', fatal=False))
  172. duration = float_or_none(self._html_search_regex(
  173. r'data-duration="([^"]+)"',
  174. webpage, 'duration', fatal=False))
  175. # playlist
  176. parts = re.findall(
  177. r'<a href="#del=(\d+)"[^>]+data-argument="([^"]+)">([^<]+)</a>', webpage)
  178. if parts:
  179. entries = []
  180. for current_part_id, stream_url, part_title in parts:
  181. if part_id and current_part_id != part_id:
  182. continue
  183. video_part_id = '%s-part%s' % (video_id, current_part_id)
  184. formats = self._extract_f4m(stream_url, video_part_id)
  185. entries.append({
  186. 'id': video_part_id,
  187. 'title': part_title,
  188. 'description': description,
  189. 'thumbnail': thumbnail,
  190. 'upload_date': upload_date,
  191. 'formats': formats,
  192. })
  193. if part_id:
  194. if entries:
  195. return entries[0]
  196. else:
  197. playlist = self.playlist_result(entries, video_id, title, description)
  198. playlist.update({
  199. 'thumbnail': thumbnail,
  200. 'upload_date': upload_date,
  201. 'duration': duration,
  202. })
  203. return playlist
  204. formats = []
  205. f4m_url = re.search(r'data-media="([^"]+)"', webpage)
  206. if f4m_url:
  207. formats.extend(self._extract_f4m(f4m_url.group(1), video_id))
  208. m3u8_url = re.search(r'data-hls-media="([^"]+)"', webpage)
  209. if m3u8_url:
  210. formats.extend(self._extract_m3u8_formats(m3u8_url.group(1), video_id, 'mp4'))
  211. self._sort_formats(formats)
  212. subtitles_url = self._html_search_regex(
  213. r'data-subtitlesurl[ ]*=[ ]*"([^"]+)"',
  214. webpage, 'subtitle URL', default=None)
  215. subtitles = None
  216. if subtitles_url:
  217. subtitles = self._extract_captions(subtitles_url, video_id, baseurl)
  218. if self._downloader.params.get('listsubtitles', False):
  219. self._list_available_subtitles(video_id, subtitles)
  220. return
  221. return {
  222. 'id': video_id,
  223. 'title': title,
  224. 'description': description,
  225. 'thumbnail': thumbnail,
  226. 'upload_date': upload_date,
  227. 'duration': duration,
  228. 'formats': formats,
  229. 'subtitles': subtitles,
  230. }