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.

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