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.

258 lines
9.7 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'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(InfoExtractor):
  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 _debug_print(self, txt):
  136. if self._downloader.params.get('verbose', False):
  137. self.to_screen('[debug] %s' % txt)
  138. def _get_subtitles(self, subtitlesurl, video_id, baseurl):
  139. url = "%s%s" % (baseurl, subtitlesurl)
  140. self._debug_print('%s: Subtitle url: %s' % (video_id, url))
  141. captions = self._download_xml(
  142. url, video_id, 'Downloading subtitles',
  143. transform_source=lambda s: s.replace(r'<br />', '\r\n'))
  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._subtitles_timecode(begin)
  151. endtime = self._subtitles_timecode(begin + duration)
  152. srt += '%s\r\n%s --> %s\r\n%s\r\n\r\n' % (compat_str(pos), starttime, endtime, p.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. }