logo

youtube-dl

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

kinja.py (8568B)


  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..compat import (
  6. compat_str,
  7. compat_urllib_parse_unquote,
  8. )
  9. from ..utils import (
  10. int_or_none,
  11. parse_iso8601,
  12. strip_or_none,
  13. try_get,
  14. unescapeHTML,
  15. urljoin,
  16. )
  17. class KinjaEmbedIE(InfoExtractor):
  18. IENAME = 'kinja:embed'
  19. _DOMAIN_REGEX = r'''(?:[^.]+\.)?
  20. (?:
  21. avclub|
  22. clickhole|
  23. deadspin|
  24. gizmodo|
  25. jalopnik|
  26. jezebel|
  27. kinja|
  28. kotaku|
  29. lifehacker|
  30. splinternews|
  31. the(?:inventory|onion|root|takeout)
  32. )\.com'''
  33. _COMMON_REGEX = r'''/
  34. (?:
  35. ajax/inset|
  36. embed/video
  37. )/iframe\?.*?\bid='''
  38. _VALID_URL = r'''(?x)https?://%s%s
  39. (?P<type>
  40. fb|
  41. imgur|
  42. instagram|
  43. jwp(?:layer)?-video|
  44. kinjavideo|
  45. mcp|
  46. megaphone|
  47. ooyala|
  48. soundcloud(?:-playlist)?|
  49. tumblr-post|
  50. twitch-stream|
  51. twitter|
  52. ustream-channel|
  53. vimeo|
  54. vine|
  55. youtube-(?:list|video)
  56. )-(?P<id>[^&]+)''' % (_DOMAIN_REGEX, _COMMON_REGEX)
  57. _TESTS = [{
  58. 'url': 'https://kinja.com/ajax/inset/iframe?id=fb-10103303356633621',
  59. 'only_matching': True,
  60. }, {
  61. 'url': 'https://kinja.com/ajax/inset/iframe?id=kinjavideo-100313',
  62. 'only_matching': True,
  63. }, {
  64. 'url': 'https://kinja.com/ajax/inset/iframe?id=megaphone-PPY1300931075',
  65. 'only_matching': True,
  66. }, {
  67. 'url': 'https://kinja.com/ajax/inset/iframe?id=ooyala-xzMXhleDpopuT0u1ijt_qZj3Va-34pEX%2FZTIxYmJjZDM2NWYzZDViZGRiOWJjYzc5',
  68. 'only_matching': True,
  69. }, {
  70. 'url': 'https://kinja.com/ajax/inset/iframe?id=soundcloud-128574047',
  71. 'only_matching': True,
  72. }, {
  73. 'url': 'https://kinja.com/ajax/inset/iframe?id=soundcloud-playlist-317413750',
  74. 'only_matching': True,
  75. }, {
  76. 'url': 'https://kinja.com/ajax/inset/iframe?id=tumblr-post-160130699814-daydreams-at-midnight',
  77. 'only_matching': True,
  78. }, {
  79. 'url': 'https://kinja.com/ajax/inset/iframe?id=twitch-stream-libratus_extra',
  80. 'only_matching': True,
  81. }, {
  82. 'url': 'https://kinja.com/ajax/inset/iframe?id=twitter-1068875942473404422',
  83. 'only_matching': True,
  84. }, {
  85. 'url': 'https://kinja.com/ajax/inset/iframe?id=ustream-channel-10414700',
  86. 'only_matching': True,
  87. }, {
  88. 'url': 'https://kinja.com/ajax/inset/iframe?id=vimeo-120153502',
  89. 'only_matching': True,
  90. }, {
  91. 'url': 'https://kinja.com/ajax/inset/iframe?id=vine-5BlvV5qqPrD',
  92. 'only_matching': True,
  93. }, {
  94. 'url': 'https://kinja.com/ajax/inset/iframe?id=youtube-list-BCQ3KyrPjgA/PLE6509247C270A72E',
  95. 'only_matching': True,
  96. }, {
  97. 'url': 'https://kinja.com/ajax/inset/iframe?id=youtube-video-00QyL0AgPAE',
  98. 'only_matching': True,
  99. }]
  100. _JWPLATFORM_PROVIDER = ('cdn.jwplayer.com/v2/media/', 'JWPlatform')
  101. _PROVIDER_MAP = {
  102. 'fb': ('facebook.com/video.php?v=', 'Facebook'),
  103. 'imgur': ('imgur.com/', 'Imgur'),
  104. 'instagram': ('instagram.com/p/', 'Instagram'),
  105. 'jwplayer-video': _JWPLATFORM_PROVIDER,
  106. 'jwp-video': _JWPLATFORM_PROVIDER,
  107. 'megaphone': ('player.megaphone.fm/', 'Generic'),
  108. 'ooyala': ('player.ooyala.com/player.js?embedCode=', 'Ooyala'),
  109. 'soundcloud': ('api.soundcloud.com/tracks/', 'Soundcloud'),
  110. 'soundcloud-playlist': ('api.soundcloud.com/playlists/', 'SoundcloudPlaylist'),
  111. 'tumblr-post': ('%s.tumblr.com/post/%s', 'Tumblr'),
  112. 'twitch-stream': ('twitch.tv/', 'TwitchStream'),
  113. 'twitter': ('twitter.com/i/cards/tfw/v1/', 'TwitterCard'),
  114. 'ustream-channel': ('ustream.tv/embed/', 'Ustream'),
  115. 'vimeo': ('vimeo.com/', 'Vimeo'),
  116. 'vine': ('vine.co/v/', 'Vine'),
  117. 'youtube-list': ('youtube.com/embed/%s?list=%s', 'YoutubePlaylist'),
  118. 'youtube-video': ('youtube.com/embed/', 'Youtube'),
  119. }
  120. @staticmethod
  121. def _extract_urls(webpage, url):
  122. return [urljoin(url, unescapeHTML(mobj.group('url'))) for mobj in re.finditer(
  123. r'(?x)<iframe[^>]+?src=(?P<q>["\'])(?P<url>(?:(?:https?:)?//%s)?%s(?:(?!\1).)+)\1' % (KinjaEmbedIE._DOMAIN_REGEX, KinjaEmbedIE._COMMON_REGEX),
  124. webpage)]
  125. def _real_extract(self, url):
  126. video_type, video_id = re.match(self._VALID_URL, url).groups()
  127. provider = self._PROVIDER_MAP.get(video_type)
  128. if provider:
  129. video_id = compat_urllib_parse_unquote(video_id)
  130. if video_type == 'tumblr-post':
  131. video_id, blog = video_id.split('-', 1)
  132. result_url = provider[0] % (blog, video_id)
  133. elif video_type == 'youtube-list':
  134. video_id, playlist_id = video_id.split('/')
  135. result_url = provider[0] % (video_id, playlist_id)
  136. else:
  137. if video_type == 'ooyala':
  138. video_id = video_id.split('/')[0]
  139. result_url = provider[0] + video_id
  140. return self.url_result('http://' + result_url, provider[1])
  141. if video_type == 'kinjavideo':
  142. data = self._download_json(
  143. 'https://kinja.com/api/core/video/views/videoById',
  144. video_id, query={'videoId': video_id})['data']
  145. title = data['title']
  146. formats = []
  147. for k in ('signedPlaylist', 'streaming'):
  148. m3u8_url = data.get(k + 'Url')
  149. if m3u8_url:
  150. formats.extend(self._extract_m3u8_formats(
  151. m3u8_url, video_id, 'mp4', 'm3u8_native',
  152. m3u8_id='hls', fatal=False))
  153. self._sort_formats(formats)
  154. thumbnail = None
  155. poster = data.get('poster') or {}
  156. poster_id = poster.get('id')
  157. if poster_id:
  158. thumbnail = 'https://i.kinja-img.com/gawker-media/image/upload/%s.%s' % (poster_id, poster.get('format') or 'jpg')
  159. return {
  160. 'id': video_id,
  161. 'title': title,
  162. 'description': strip_or_none(data.get('description')),
  163. 'formats': formats,
  164. 'tags': data.get('tags'),
  165. 'timestamp': int_or_none(try_get(
  166. data, lambda x: x['postInfo']['publishTimeMillis']), 1000),
  167. 'thumbnail': thumbnail,
  168. 'uploader': data.get('network'),
  169. }
  170. else:
  171. video_data = self._download_json(
  172. 'https://api.vmh.univision.com/metadata/v1/content/' + video_id,
  173. video_id)['videoMetadata']
  174. iptc = video_data['photoVideoMetadataIPTC']
  175. title = iptc['title']['en']
  176. fmg = video_data.get('photoVideoMetadata_fmg') or {}
  177. tvss_domain = fmg.get('tvssDomain') or 'https://auth.univision.com'
  178. data = self._download_json(
  179. tvss_domain + '/api/v3/video-auth/url-signature-tokens',
  180. video_id, query={'mcpids': video_id})['data'][0]
  181. formats = []
  182. rendition_url = data.get('renditionUrl')
  183. if rendition_url:
  184. formats = self._extract_m3u8_formats(
  185. rendition_url, video_id, 'mp4',
  186. 'm3u8_native', m3u8_id='hls', fatal=False)
  187. fallback_rendition_url = data.get('fallbackRenditionUrl')
  188. if fallback_rendition_url:
  189. formats.append({
  190. 'format_id': 'fallback',
  191. 'tbr': int_or_none(self._search_regex(
  192. r'_(\d+)\.mp4', fallback_rendition_url,
  193. 'bitrate', default=None)),
  194. 'url': fallback_rendition_url,
  195. })
  196. self._sort_formats(formats)
  197. return {
  198. 'id': video_id,
  199. 'title': title,
  200. 'thumbnail': try_get(iptc, lambda x: x['cloudinaryLink']['link'], compat_str),
  201. 'uploader': fmg.get('network'),
  202. 'duration': int_or_none(iptc.get('fileDuration')),
  203. 'formats': formats,
  204. 'description': try_get(iptc, lambda x: x['description']['en'], compat_str),
  205. 'timestamp': parse_iso8601(iptc.get('dateReleased')),
  206. }