logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

ruutu.py (9346B)


  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. from .common import InfoExtractor
  4. from ..compat import compat_urllib_parse_urlparse
  5. from ..utils import (
  6. determine_ext,
  7. ExtractorError,
  8. find_xpath_attr,
  9. int_or_none,
  10. unified_strdate,
  11. url_or_none,
  12. xpath_attr,
  13. xpath_text,
  14. )
  15. class RuutuIE(InfoExtractor):
  16. _VALID_URL = r'''(?x)
  17. https?://
  18. (?:
  19. (?:www\.)?(?:ruutu|supla)\.fi/(?:video|supla|audio)/|
  20. static\.nelonenmedia\.fi/player/misc/embed_player\.html\?.*?\bnid=
  21. )
  22. (?P<id>\d+)
  23. '''
  24. _TESTS = [
  25. {
  26. 'url': 'http://www.ruutu.fi/video/2058907',
  27. 'md5': 'ab2093f39be1ca8581963451b3c0234f',
  28. 'info_dict': {
  29. 'id': '2058907',
  30. 'ext': 'mp4',
  31. 'title': 'Oletko aina halunnut tietää mitä tapahtuu vain hetki ennen lähetystä? - Nyt se selvisi!',
  32. 'description': 'md5:cfc6ccf0e57a814360df464a91ff67d6',
  33. 'thumbnail': r're:^https?://.*\.jpg$',
  34. 'duration': 114,
  35. 'age_limit': 0,
  36. },
  37. },
  38. {
  39. 'url': 'http://www.ruutu.fi/video/2057306',
  40. 'md5': '065a10ae4d5b8cfd9d0c3d332465e3d9',
  41. 'info_dict': {
  42. 'id': '2057306',
  43. 'ext': 'mp4',
  44. 'title': 'Superpesis: katso koko kausi Ruudussa',
  45. 'description': 'md5:bfb7336df2a12dc21d18fa696c9f8f23',
  46. 'thumbnail': r're:^https?://.*\.jpg$',
  47. 'duration': 40,
  48. 'age_limit': 0,
  49. },
  50. },
  51. {
  52. 'url': 'http://www.supla.fi/supla/2231370',
  53. 'md5': 'df14e782d49a2c0df03d3be2a54ef949',
  54. 'info_dict': {
  55. 'id': '2231370',
  56. 'ext': 'mp4',
  57. 'title': 'Osa 1: Mikael Jungner',
  58. 'description': 'md5:7d90f358c47542e3072ff65d7b1bcffe',
  59. 'thumbnail': r're:^https?://.*\.jpg$',
  60. 'age_limit': 0,
  61. },
  62. },
  63. # Episode where <SourceFile> is "NOT-USED", but has other
  64. # downloadable sources available.
  65. {
  66. 'url': 'http://www.ruutu.fi/video/3193728',
  67. 'only_matching': True,
  68. },
  69. {
  70. # audio podcast
  71. 'url': 'https://www.supla.fi/supla/3382410',
  72. 'md5': 'b9d7155fed37b2ebf6021d74c4b8e908',
  73. 'info_dict': {
  74. 'id': '3382410',
  75. 'ext': 'mp3',
  76. 'title': 'Mikä ihmeen poltergeist?',
  77. 'description': 'md5:bbb6963df17dfd0ecd9eb9a61bf14b52',
  78. 'thumbnail': r're:^https?://.*\.jpg$',
  79. 'age_limit': 0,
  80. },
  81. 'expected_warnings': [
  82. 'HTTP Error 502: Bad Gateway',
  83. 'Failed to download m3u8 information',
  84. ],
  85. },
  86. {
  87. 'url': 'http://www.supla.fi/audio/2231370',
  88. 'only_matching': True,
  89. },
  90. {
  91. 'url': 'https://static.nelonenmedia.fi/player/misc/embed_player.html?nid=3618790',
  92. 'only_matching': True,
  93. },
  94. {
  95. # episode
  96. 'url': 'https://www.ruutu.fi/video/3401964',
  97. 'info_dict': {
  98. 'id': '3401964',
  99. 'ext': 'mp4',
  100. 'title': 'Temptation Island Suomi - Kausi 5 - Jakso 17',
  101. 'description': 'md5:87cf01d5e1e88adf0c8a2937d2bd42ba',
  102. 'thumbnail': r're:^https?://.*\.jpg$',
  103. 'duration': 2582,
  104. 'age_limit': 12,
  105. 'upload_date': '20190508',
  106. 'series': 'Temptation Island Suomi',
  107. 'season_number': 5,
  108. 'episode_number': 17,
  109. 'categories': ['Reality ja tositapahtumat', 'Kotimaiset suosikit', 'Romantiikka ja parisuhde'],
  110. },
  111. 'params': {
  112. 'skip_download': True,
  113. },
  114. },
  115. {
  116. # premium
  117. 'url': 'https://www.ruutu.fi/video/3618715',
  118. 'only_matching': True,
  119. },
  120. ]
  121. _API_BASE = 'https://gatling.nelonenmedia.fi'
  122. def _real_extract(self, url):
  123. video_id = self._match_id(url)
  124. video_xml = self._download_xml(
  125. '%s/media-xml-cache' % self._API_BASE, video_id,
  126. query={'id': video_id})
  127. formats = []
  128. processed_urls = []
  129. def extract_formats(node):
  130. for child in node:
  131. if child.tag.endswith('Files'):
  132. extract_formats(child)
  133. elif child.tag.endswith('File'):
  134. video_url = child.text
  135. if (not video_url or video_url in processed_urls
  136. or any(p in video_url for p in ('NOT_USED', 'NOT-USED'))):
  137. continue
  138. processed_urls.append(video_url)
  139. ext = determine_ext(video_url)
  140. auth_video_url = url_or_none(self._download_webpage(
  141. '%s/auth/access/v2' % self._API_BASE, video_id,
  142. note='Downloading authenticated %s stream URL' % ext,
  143. fatal=False, query={'stream': video_url}))
  144. if auth_video_url:
  145. processed_urls.append(auth_video_url)
  146. video_url = auth_video_url
  147. if ext == 'm3u8':
  148. formats.extend(self._extract_m3u8_formats(
  149. video_url, video_id, 'mp4',
  150. entry_protocol='m3u8_native', m3u8_id='hls',
  151. fatal=False))
  152. elif ext == 'f4m':
  153. formats.extend(self._extract_f4m_formats(
  154. video_url, video_id, f4m_id='hds', fatal=False))
  155. elif ext == 'mpd':
  156. # video-only and audio-only streams are of different
  157. # duration resulting in out of sync issue
  158. continue
  159. formats.extend(self._extract_mpd_formats(
  160. video_url, video_id, mpd_id='dash', fatal=False))
  161. elif ext == 'mp3' or child.tag == 'AudioMediaFile':
  162. formats.append({
  163. 'format_id': 'audio',
  164. 'url': video_url,
  165. 'vcodec': 'none',
  166. })
  167. else:
  168. proto = compat_urllib_parse_urlparse(video_url).scheme
  169. if not child.tag.startswith('HTTP') and proto != 'rtmp':
  170. continue
  171. preference = -1 if proto == 'rtmp' else 1
  172. label = child.get('label')
  173. tbr = int_or_none(child.get('bitrate'))
  174. format_id = '%s-%s' % (proto, label if label else tbr) if label or tbr else proto
  175. if not self._is_valid_url(video_url, video_id, format_id):
  176. continue
  177. width, height = [int_or_none(x) for x in child.get('resolution', 'x').split('x')[:2]]
  178. formats.append({
  179. 'format_id': format_id,
  180. 'url': video_url,
  181. 'width': width,
  182. 'height': height,
  183. 'tbr': tbr,
  184. 'preference': preference,
  185. })
  186. extract_formats(video_xml.find('./Clip'))
  187. def pv(name):
  188. node = find_xpath_attr(
  189. video_xml, './Clip/PassthroughVariables/variable', 'name', name)
  190. if node is not None:
  191. return node.get('value')
  192. if not formats:
  193. drm = xpath_text(video_xml, './Clip/DRM', default=None)
  194. if drm:
  195. raise ExtractorError('This video is DRM protected.', expected=True)
  196. ns_st_cds = pv('ns_st_cds')
  197. if ns_st_cds != 'free':
  198. raise ExtractorError('This video is %s.' % ns_st_cds, expected=True)
  199. self._sort_formats(formats)
  200. themes = pv('themes')
  201. return {
  202. 'id': video_id,
  203. 'title': xpath_attr(video_xml, './/Behavior/Program', 'program_name', 'title', fatal=True),
  204. 'description': xpath_attr(video_xml, './/Behavior/Program', 'description', 'description'),
  205. 'thumbnail': xpath_attr(video_xml, './/Behavior/Startpicture', 'href', 'thumbnail'),
  206. 'duration': int_or_none(xpath_text(video_xml, './/Runtime', 'duration')) or int_or_none(pv('runtime')),
  207. 'age_limit': int_or_none(xpath_text(video_xml, './/AgeLimit', 'age limit')),
  208. 'upload_date': unified_strdate(pv('date_start')),
  209. 'series': pv('series_name'),
  210. 'season_number': int_or_none(pv('season_number')),
  211. 'episode_number': int_or_none(pv('episode_number')),
  212. 'categories': themes.split(',') if themes else [],
  213. 'formats': formats,
  214. }