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.

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