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.

286 lines
10 KiB

9 years ago
  1. # encoding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..compat import compat_urlparse
  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:|https?://(?: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 rettigheter 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'https?://(?: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. IE_DESC = 'NRK TV and NRK Radio'
  99. _VALID_URL = r'(?P<baseurl>https?://(?:tv|radio)\.nrk(?:super)?\.no/)(?:serie/[^/]+|program)/(?P<id>[a-zA-Z]{4}\d{8})(?:/\d{2}-\d{2}-\d{4})?(?:#del=(?P<part_id>\d+))?'
  100. _TESTS = [
  101. {
  102. 'url': 'https://tv.nrk.no/serie/20-spoersmaal-tv/MUHH48000314/23-05-2014',
  103. 'md5': 'adf2c5454fa2bf032f47a9f8fb351342',
  104. 'info_dict': {
  105. 'id': 'MUHH48000314',
  106. 'ext': 'flv',
  107. 'title': '20 spørsmål',
  108. 'description': 'md5:bdea103bc35494c143c6a9acdd84887a',
  109. 'upload_date': '20140523',
  110. 'duration': 1741.52,
  111. },
  112. },
  113. {
  114. 'url': 'https://tv.nrk.no/program/mdfp15000514',
  115. 'md5': '383650ece2b25ecec996ad7b5bb2a384',
  116. 'info_dict': {
  117. 'id': 'mdfp15000514',
  118. 'ext': 'flv',
  119. 'title': 'Kunnskapskanalen: Grunnlovsjubiléet - Stor ståhei for ingenting',
  120. 'description': 'md5:654c12511f035aed1e42bdf5db3b206a',
  121. 'upload_date': '20140524',
  122. 'duration': 4605.0,
  123. },
  124. },
  125. {
  126. # single playlist video
  127. 'url': 'https://tv.nrk.no/serie/tour-de-ski/MSPO40010515/06-01-2015#del=2',
  128. 'md5': 'adbd1dbd813edaf532b0a253780719c2',
  129. 'info_dict': {
  130. 'id': 'MSPO40010515-part2',
  131. 'ext': 'flv',
  132. 'title': 'Tour de Ski: Sprint fri teknikk, kvinner og menn 06.01.2015 (del 2:2)',
  133. 'description': 'md5:238b67b97a4ac7d7b4bf0edf8cc57d26',
  134. 'upload_date': '20150106',
  135. },
  136. 'skip': 'Only works from Norway',
  137. },
  138. {
  139. 'url': 'https://tv.nrk.no/serie/tour-de-ski/MSPO40010515/06-01-2015',
  140. 'playlist': [
  141. {
  142. 'md5': '9480285eff92d64f06e02a5367970a7a',
  143. 'info_dict': {
  144. 'id': 'MSPO40010515-part1',
  145. 'ext': 'flv',
  146. 'title': 'Tour de Ski: Sprint fri teknikk, kvinner og menn 06.01.2015 (del 1:2)',
  147. 'description': 'md5:238b67b97a4ac7d7b4bf0edf8cc57d26',
  148. 'upload_date': '20150106',
  149. },
  150. },
  151. {
  152. 'md5': 'adbd1dbd813edaf532b0a253780719c2',
  153. 'info_dict': {
  154. 'id': 'MSPO40010515-part2',
  155. 'ext': 'flv',
  156. 'title': 'Tour de Ski: Sprint fri teknikk, kvinner og menn 06.01.2015 (del 2:2)',
  157. 'description': 'md5:238b67b97a4ac7d7b4bf0edf8cc57d26',
  158. 'upload_date': '20150106',
  159. },
  160. },
  161. ],
  162. 'info_dict': {
  163. 'id': 'MSPO40010515',
  164. 'title': 'Tour de Ski: Sprint fri teknikk, kvinner og menn',
  165. 'description': 'md5:238b67b97a4ac7d7b4bf0edf8cc57d26',
  166. 'upload_date': '20150106',
  167. 'duration': 6947.5199999999995,
  168. },
  169. 'skip': 'Only works from Norway',
  170. },
  171. {
  172. 'url': 'https://radio.nrk.no/serie/dagsnytt/NPUB21019315/12-07-2015#',
  173. 'only_matching': True,
  174. }
  175. ]
  176. def _extract_f4m(self, manifest_url, video_id):
  177. return self._extract_f4m_formats(
  178. manifest_url + '?hdcore=3.1.1&plugin=aasp-3.1.1.69.124', video_id, f4m_id='hds')
  179. def _real_extract(self, url):
  180. mobj = re.match(self._VALID_URL, url)
  181. video_id = mobj.group('id')
  182. part_id = mobj.group('part_id')
  183. base_url = mobj.group('baseurl')
  184. webpage = self._download_webpage(url, video_id)
  185. title = self._html_search_meta(
  186. 'title', webpage, 'title')
  187. description = self._html_search_meta(
  188. 'description', webpage, 'description')
  189. thumbnail = self._html_search_regex(
  190. r'data-posterimage="([^"]+)"',
  191. webpage, 'thumbnail', fatal=False)
  192. upload_date = unified_strdate(self._html_search_meta(
  193. 'rightsfrom', webpage, 'upload date', fatal=False))
  194. duration = float_or_none(self._html_search_regex(
  195. r'data-duration="([^"]+)"',
  196. webpage, 'duration', fatal=False))
  197. # playlist
  198. parts = re.findall(
  199. r'<a href="#del=(\d+)"[^>]+data-argument="([^"]+)">([^<]+)</a>', webpage)
  200. if parts:
  201. entries = []
  202. for current_part_id, stream_url, part_title in parts:
  203. if part_id and current_part_id != part_id:
  204. continue
  205. video_part_id = '%s-part%s' % (video_id, current_part_id)
  206. formats = self._extract_f4m(stream_url, video_part_id)
  207. entries.append({
  208. 'id': video_part_id,
  209. 'title': part_title,
  210. 'description': description,
  211. 'thumbnail': thumbnail,
  212. 'upload_date': upload_date,
  213. 'formats': formats,
  214. })
  215. if part_id:
  216. if entries:
  217. return entries[0]
  218. else:
  219. playlist = self.playlist_result(entries, video_id, title, description)
  220. playlist.update({
  221. 'thumbnail': thumbnail,
  222. 'upload_date': upload_date,
  223. 'duration': duration,
  224. })
  225. return playlist
  226. formats = []
  227. f4m_url = re.search(r'data-media="([^"]+)"', webpage)
  228. if f4m_url:
  229. formats.extend(self._extract_f4m(f4m_url.group(1), video_id))
  230. m3u8_url = re.search(r'data-hls-media="([^"]+)"', webpage)
  231. if m3u8_url:
  232. formats.extend(self._extract_m3u8_formats(m3u8_url.group(1), video_id, 'mp4', m3u8_id='hls'))
  233. self._sort_formats(formats)
  234. subtitles_url = self._html_search_regex(
  235. r'data-subtitlesurl\s*=\s*(["\'])(?P<url>.+?)\1',
  236. webpage, 'subtitle URL', default=None, group='url')
  237. subtitles = {}
  238. if subtitles_url:
  239. subtitles['no'] = [{
  240. 'ext': 'ttml',
  241. 'url': compat_urlparse.urljoin(base_url, subtitles_url),
  242. }]
  243. return {
  244. 'id': video_id,
  245. 'title': title,
  246. 'description': description,
  247. 'thumbnail': thumbnail,
  248. 'upload_date': upload_date,
  249. 'duration': duration,
  250. 'formats': formats,
  251. 'subtitles': subtitles,
  252. }