nrk.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. 'params': {
  99. 'proxy': '127.0.0.1:8118',
  100. },
  101. },
  102. {
  103. 'url': 'http://tv.nrk.no/serie/tour-de-ski/MSPO40010515/06-01-2015',
  104. 'playlist': [
  105. {
  106. 'md5': '9480285eff92d64f06e02a5367970a7a',
  107. 'info_dict': {
  108. 'id': 'MSPO40010515-part1',
  109. 'ext': 'flv',
  110. 'title': 'Tour de Ski: Sprint fri teknikk, kvinner og menn 06.01.2015 (del 1:2)',
  111. 'description': 'md5:238b67b97a4ac7d7b4bf0edf8cc57d26',
  112. 'upload_date': '20150106',
  113. },
  114. },
  115. {
  116. 'md5': 'adbd1dbd813edaf532b0a253780719c2',
  117. 'info_dict': {
  118. 'id': 'MSPO40010515-part2',
  119. 'ext': 'flv',
  120. 'title': 'Tour de Ski: Sprint fri teknikk, kvinner og menn 06.01.2015 (del 2:2)',
  121. 'description': 'md5:238b67b97a4ac7d7b4bf0edf8cc57d26',
  122. 'upload_date': '20150106',
  123. },
  124. },
  125. ],
  126. 'info_dict': {
  127. 'id': 'MSPO40010515',
  128. 'title': 'Tour de Ski: Sprint fri teknikk, kvinner og menn',
  129. 'description': 'md5:238b67b97a4ac7d7b4bf0edf8cc57d26',
  130. 'upload_date': '20150106',
  131. 'duration': 6947.5199999999995,
  132. },
  133. 'skip': 'Only works from Norway',
  134. 'params': {
  135. 'proxy': '127.0.0.1:8118',
  136. },
  137. }
  138. ]
  139. def _extract_f4m(self, manifest_url, video_id):
  140. return self._extract_f4m_formats(manifest_url + '?hdcore=3.1.1&plugin=aasp-3.1.1.69.124', video_id)
  141. def _real_extract(self, url):
  142. mobj = re.match(self._VALID_URL, url)
  143. video_id = mobj.group('id')
  144. part_id = mobj.group('part_id')
  145. webpage = self._download_webpage(url, video_id)
  146. title = self._html_search_meta(
  147. 'title', webpage, 'title')
  148. description = self._html_search_meta(
  149. 'description', webpage, 'description')
  150. thumbnail = self._html_search_regex(
  151. r'data-posterimage="([^"]+)"',
  152. webpage, 'thumbnail', fatal=False)
  153. upload_date = unified_strdate(self._html_search_meta(
  154. 'rightsfrom', webpage, 'upload date', fatal=False))
  155. duration = float_or_none(self._html_search_regex(
  156. r'data-duration="([^"]+)"',
  157. webpage, 'duration', fatal=False))
  158. # playlist
  159. parts = re.findall(
  160. r'<a href="#del=(\d+)"[^>]+data-argument="([^"]+)">([^<]+)</a>', webpage)
  161. if parts:
  162. entries = []
  163. for current_part_id, stream_url, part_title in parts:
  164. if part_id and current_part_id != part_id:
  165. continue
  166. video_part_id = '%s-part%s' % (video_id, current_part_id)
  167. formats = self._extract_f4m(stream_url, video_part_id)
  168. entries.append({
  169. 'id': video_part_id,
  170. 'title': part_title,
  171. 'description': description,
  172. 'thumbnail': thumbnail,
  173. 'upload_date': upload_date,
  174. 'formats': formats,
  175. })
  176. if part_id:
  177. if entries:
  178. return entries[0]
  179. else:
  180. playlist = self.playlist_result(entries, video_id, title, description)
  181. playlist.update({
  182. 'thumbnail': thumbnail,
  183. 'upload_date': upload_date,
  184. 'duration': duration,
  185. })
  186. return playlist
  187. formats = []
  188. f4m_url = re.search(r'data-media="([^"]+)"', webpage)
  189. if f4m_url:
  190. formats.extend(self._extract_f4m(f4m_url.group(1), video_id))
  191. m3u8_url = re.search(r'data-hls-media="([^"]+)"', webpage)
  192. if m3u8_url:
  193. formats.extend(self._extract_m3u8_formats(m3u8_url.group(1), video_id, 'mp4'))
  194. self._sort_formats(formats)
  195. return {
  196. 'id': video_id,
  197. 'title': title,
  198. 'description': description,
  199. 'thumbnail': thumbnail,
  200. 'upload_date': upload_date,
  201. 'duration': duration,
  202. 'formats': formats,
  203. }