kinja.py 8.0 KB

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