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.

294 lines
10 KiB

Switch codebase to use sanitized_Request instead of compat_urllib_request.Request [downloader/dash] Use sanitized_Request [downloader/http] Use sanitized_Request [atresplayer] Use sanitized_Request [bambuser] Use sanitized_Request [bliptv] Use sanitized_Request [brightcove] Use sanitized_Request [cbs] Use sanitized_Request [ceskatelevize] Use sanitized_Request [collegerama] Use sanitized_Request [extractor/common] Use sanitized_Request [crunchyroll] Use sanitized_Request [dailymotion] Use sanitized_Request [dcn] Use sanitized_Request [dramafever] Use sanitized_Request [dumpert] Use sanitized_Request [eitb] Use sanitized_Request [escapist] Use sanitized_Request [everyonesmixtape] Use sanitized_Request [extremetube] Use sanitized_Request [facebook] Use sanitized_Request [fc2] Use sanitized_Request [flickr] Use sanitized_Request [4tube] Use sanitized_Request [gdcvault] Use sanitized_Request [extractor/generic] Use sanitized_Request [hearthisat] Use sanitized_Request [hotnewhiphop] Use sanitized_Request [hypem] Use sanitized_Request [iprima] Use sanitized_Request [ivi] Use sanitized_Request [keezmovies] Use sanitized_Request [letv] Use sanitized_Request [lynda] Use sanitized_Request [metacafe] Use sanitized_Request [minhateca] Use sanitized_Request [miomio] Use sanitized_Request [meovideo] Use sanitized_Request [mofosex] Use sanitized_Request [moniker] Use sanitized_Request [mooshare] Use sanitized_Request [movieclips] Use sanitized_Request [mtv] Use sanitized_Request [myvideo] Use sanitized_Request [neteasemusic] Use sanitized_Request [nfb] Use sanitized_Request [niconico] Use sanitized_Request [noco] Use sanitized_Request [nosvideo] Use sanitized_Request [novamov] Use sanitized_Request [nowness] Use sanitized_Request [nuvid] Use sanitized_Request [played] Use sanitized_Request [pluralsight] Use sanitized_Request [pornhub] Use sanitized_Request [pornotube] Use sanitized_Request [primesharetv] Use sanitized_Request [promptfile] Use sanitized_Request [qqmusic] Use sanitized_Request [rtve] Use sanitized_Request [safari] Use sanitized_Request [sandia] Use sanitized_Request [shared] Use sanitized_Request [sharesix] Use sanitized_Request [sina] Use sanitized_Request [smotri] Use sanitized_Request [sohu] Use sanitized_Request [spankwire] Use sanitized_Request [sportdeutschland] Use sanitized_Request [streamcloud] Use sanitized_Request [streamcz] Use sanitized_Request [tapely] Use sanitized_Request [tube8] Use sanitized_Request [tubitv] Use sanitized_Request [twitch] Use sanitized_Request [twitter] Use sanitized_Request [udemy] Use sanitized_Request [vbox7] Use sanitized_Request [veoh] Use sanitized_Request [vessel] Use sanitized_Request [vevo] Use sanitized_Request [viddler] Use sanitized_Request [videomega] Use sanitized_Request [viewvster] Use sanitized_Request [viki] Use sanitized_Request [vk] Use sanitized_Request [vodlocker] Use sanitized_Request [voicerepublic] Use sanitized_Request [wistia] Use sanitized_Request [xfileshare] Use sanitized_Request [xtube] Use sanitized_Request [xvideos] Use sanitized_Request [yandexmusic] Use sanitized_Request [youku] Use sanitized_Request [youporn] Use sanitized_Request [youtube] Use sanitized_Request [patreon] Use sanitized_Request [extractor/common] Remove unused import [nfb] PEP 8
9 years ago
Switch codebase to use sanitized_Request instead of compat_urllib_request.Request [downloader/dash] Use sanitized_Request [downloader/http] Use sanitized_Request [atresplayer] Use sanitized_Request [bambuser] Use sanitized_Request [bliptv] Use sanitized_Request [brightcove] Use sanitized_Request [cbs] Use sanitized_Request [ceskatelevize] Use sanitized_Request [collegerama] Use sanitized_Request [extractor/common] Use sanitized_Request [crunchyroll] Use sanitized_Request [dailymotion] Use sanitized_Request [dcn] Use sanitized_Request [dramafever] Use sanitized_Request [dumpert] Use sanitized_Request [eitb] Use sanitized_Request [escapist] Use sanitized_Request [everyonesmixtape] Use sanitized_Request [extremetube] Use sanitized_Request [facebook] Use sanitized_Request [fc2] Use sanitized_Request [flickr] Use sanitized_Request [4tube] Use sanitized_Request [gdcvault] Use sanitized_Request [extractor/generic] Use sanitized_Request [hearthisat] Use sanitized_Request [hotnewhiphop] Use sanitized_Request [hypem] Use sanitized_Request [iprima] Use sanitized_Request [ivi] Use sanitized_Request [keezmovies] Use sanitized_Request [letv] Use sanitized_Request [lynda] Use sanitized_Request [metacafe] Use sanitized_Request [minhateca] Use sanitized_Request [miomio] Use sanitized_Request [meovideo] Use sanitized_Request [mofosex] Use sanitized_Request [moniker] Use sanitized_Request [mooshare] Use sanitized_Request [movieclips] Use sanitized_Request [mtv] Use sanitized_Request [myvideo] Use sanitized_Request [neteasemusic] Use sanitized_Request [nfb] Use sanitized_Request [niconico] Use sanitized_Request [noco] Use sanitized_Request [nosvideo] Use sanitized_Request [novamov] Use sanitized_Request [nowness] Use sanitized_Request [nuvid] Use sanitized_Request [played] Use sanitized_Request [pluralsight] Use sanitized_Request [pornhub] Use sanitized_Request [pornotube] Use sanitized_Request [primesharetv] Use sanitized_Request [promptfile] Use sanitized_Request [qqmusic] Use sanitized_Request [rtve] Use sanitized_Request [safari] Use sanitized_Request [sandia] Use sanitized_Request [shared] Use sanitized_Request [sharesix] Use sanitized_Request [sina] Use sanitized_Request [smotri] Use sanitized_Request [sohu] Use sanitized_Request [spankwire] Use sanitized_Request [sportdeutschland] Use sanitized_Request [streamcloud] Use sanitized_Request [streamcz] Use sanitized_Request [tapely] Use sanitized_Request [tube8] Use sanitized_Request [tubitv] Use sanitized_Request [twitch] Use sanitized_Request [twitter] Use sanitized_Request [udemy] Use sanitized_Request [vbox7] Use sanitized_Request [veoh] Use sanitized_Request [vessel] Use sanitized_Request [vevo] Use sanitized_Request [viddler] Use sanitized_Request [videomega] Use sanitized_Request [viewvster] Use sanitized_Request [viki] Use sanitized_Request [vk] Use sanitized_Request [vodlocker] Use sanitized_Request [voicerepublic] Use sanitized_Request [wistia] Use sanitized_Request [xfileshare] Use sanitized_Request [xtube] Use sanitized_Request [xvideos] Use sanitized_Request [yandexmusic] Use sanitized_Request [youku] Use sanitized_Request [youporn] Use sanitized_Request [youtube] Use sanitized_Request [patreon] Use sanitized_Request [extractor/common] Remove unused import [nfb] PEP 8
9 years ago
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. float_or_none,
  7. xpath_text,
  8. remove_end,
  9. int_or_none,
  10. ExtractorError,
  11. sanitized_Request,
  12. )
  13. class TwitterBaseIE(InfoExtractor):
  14. def _get_vmap_video_url(self, vmap_url, video_id):
  15. vmap_data = self._download_xml(vmap_url, video_id)
  16. return xpath_text(vmap_data, './/MediaFile').strip()
  17. class TwitterCardIE(TwitterBaseIE):
  18. IE_NAME = 'twitter:card'
  19. _VALID_URL = r'https?://(?:www\.)?twitter\.com/i/cards/tfw/v1/(?P<id>\d+)'
  20. _TESTS = [
  21. {
  22. 'url': 'https://twitter.com/i/cards/tfw/v1/560070183650213889',
  23. # MD5 checksums are different in different places
  24. 'info_dict': {
  25. 'id': '560070183650213889',
  26. 'ext': 'mp4',
  27. 'title': 'TwitterCard',
  28. 'thumbnail': 're:^https?://.*\.jpg$',
  29. 'duration': 30.033,
  30. }
  31. },
  32. {
  33. 'url': 'https://twitter.com/i/cards/tfw/v1/623160978427936768',
  34. 'md5': '7ee2a553b63d1bccba97fbed97d9e1c8',
  35. 'info_dict': {
  36. 'id': '623160978427936768',
  37. 'ext': 'mp4',
  38. 'title': 'TwitterCard',
  39. 'thumbnail': 're:^https?://.*\.jpg',
  40. 'duration': 80.155,
  41. },
  42. },
  43. {
  44. 'url': 'https://twitter.com/i/cards/tfw/v1/654001591733886977',
  45. 'md5': 'd4724ffe6d2437886d004fa5de1043b3',
  46. 'info_dict': {
  47. 'id': 'dq4Oj5quskI',
  48. 'ext': 'mp4',
  49. 'title': 'Ubuntu 11.10 Overview',
  50. 'description': 'Take a quick peek at what\'s new and improved in Ubuntu 11.10.\n\nOnce installed take a look at 10 Things to Do After Installing: http://www.omgubuntu.co.uk/2011/10/10-things-to-do-after-installing-ubuntu-11-10/',
  51. 'upload_date': '20111013',
  52. 'uploader': 'OMG! Ubuntu!',
  53. 'uploader_id': 'omgubuntu',
  54. },
  55. 'add_ie': ['Youtube'],
  56. },
  57. {
  58. 'url': 'https://twitter.com/i/cards/tfw/v1/665289828897005568',
  59. 'md5': 'ab2745d0b0ce53319a534fccaa986439',
  60. 'info_dict': {
  61. 'id': 'iBb2x00UVlv',
  62. 'ext': 'mp4',
  63. 'upload_date': '20151113',
  64. 'uploader_id': '1189339351084113920',
  65. 'uploader': 'ArsenalTerje',
  66. 'title': 'Vine by ArsenalTerje',
  67. },
  68. 'add_ie': ['Vine'],
  69. }
  70. ]
  71. def _real_extract(self, url):
  72. video_id = self._match_id(url)
  73. # Different formats served for different User-Agents
  74. USER_AGENTS = [
  75. 'Mozilla/5.0 (X11; Linux x86_64; rv:10.0) Gecko/20150101 Firefox/20.0 (Chrome)', # mp4
  76. 'Mozilla/5.0 (Windows NT 5.2; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0', # webm
  77. ]
  78. config = None
  79. formats = []
  80. for user_agent in USER_AGENTS:
  81. request = sanitized_Request(url)
  82. request.add_header('User-Agent', user_agent)
  83. webpage = self._download_webpage(request, video_id)
  84. iframe_url = self._html_search_regex(
  85. r'<iframe[^>]+src="((?:https?:)?//(?:www.youtube.com/embed/[^"]+|(?:www\.)?vine\.co/v/\w+/card))"',
  86. webpage, 'video iframe', default=None)
  87. if iframe_url:
  88. return self.url_result(iframe_url)
  89. config = self._parse_json(self._html_search_regex(
  90. r'data-player-config="([^"]+)"', webpage, 'data player config'),
  91. video_id)
  92. if 'playlist' not in config:
  93. if 'vmapUrl' in config:
  94. formats.append({
  95. 'url': self._get_vmap_video_url(config['vmapUrl'], video_id),
  96. })
  97. break # same video regardless of UA
  98. continue
  99. video_url = config['playlist'][0]['source']
  100. f = {
  101. 'url': video_url,
  102. }
  103. m = re.search(r'/(?P<width>\d+)x(?P<height>\d+)/', video_url)
  104. if m:
  105. f.update({
  106. 'width': int(m.group('width')),
  107. 'height': int(m.group('height')),
  108. })
  109. formats.append(f)
  110. self._sort_formats(formats)
  111. thumbnail = config.get('posterImageUrl')
  112. duration = float_or_none(config.get('duration'))
  113. return {
  114. 'id': video_id,
  115. 'title': 'TwitterCard',
  116. 'thumbnail': thumbnail,
  117. 'duration': duration,
  118. 'formats': formats,
  119. }
  120. class TwitterIE(InfoExtractor):
  121. IE_NAME = 'twitter'
  122. _VALID_URL = r'https?://(?:www\.|m\.|mobile\.)?twitter\.com/(?P<user_id>[^/]+)/status/(?P<id>\d+)'
  123. _TEMPLATE_URL = 'https://twitter.com/%s/status/%s'
  124. _TESTS = [{
  125. 'url': 'https://twitter.com/freethenipple/status/643211948184596480',
  126. # MD5 checksums are different in different places
  127. 'info_dict': {
  128. 'id': '643211948184596480',
  129. 'ext': 'mp4',
  130. 'title': 'FREE THE NIPPLE - FTN supporters on Hollywood Blvd today!',
  131. 'thumbnail': 're:^https?://.*\.jpg',
  132. 'duration': 12.922,
  133. 'description': 'FREE THE NIPPLE on Twitter: "FTN supporters on Hollywood Blvd today! http://t.co/c7jHH749xJ"',
  134. 'uploader': 'FREE THE NIPPLE',
  135. 'uploader_id': 'freethenipple',
  136. },
  137. }, {
  138. 'url': 'https://twitter.com/giphz/status/657991469417025536/photo/1',
  139. 'md5': 'f36dcd5fb92bf7057f155e7d927eeb42',
  140. 'info_dict': {
  141. 'id': '657991469417025536',
  142. 'ext': 'mp4',
  143. 'title': 'Gifs - tu vai cai tu vai cai tu nao eh capaz disso tu vai cai',
  144. 'description': 'Gifs on Twitter: "tu vai cai tu vai cai tu nao eh capaz disso tu vai cai https://t.co/tM46VHFlO5"',
  145. 'thumbnail': 're:^https?://.*\.png',
  146. 'uploader': 'Gifs',
  147. 'uploader_id': 'giphz',
  148. },
  149. 'expected_warnings': ['height', 'width'],
  150. }, {
  151. 'url': 'https://twitter.com/starwars/status/665052190608723968',
  152. 'md5': '39b7199856dee6cd4432e72c74bc69d4',
  153. 'info_dict': {
  154. 'id': '665052190608723968',
  155. 'ext': 'mp4',
  156. 'title': 'Star Wars - A new beginning is coming December 18. Watch the official 60 second #TV spot for #StarWars: #TheForceAwakens.',
  157. 'description': 'Star Wars on Twitter: "A new beginning is coming December 18. Watch the official 60 second #TV spot for #StarWars: #TheForceAwakens."',
  158. 'uploader_id': 'starwars',
  159. 'uploader': 'Star Wars',
  160. },
  161. }]
  162. def _real_extract(self, url):
  163. mobj = re.match(self._VALID_URL, url)
  164. user_id = mobj.group('user_id')
  165. twid = mobj.group('id')
  166. webpage = self._download_webpage(self._TEMPLATE_URL % (user_id, twid), twid)
  167. username = remove_end(self._og_search_title(webpage), ' on Twitter')
  168. title = description = self._og_search_description(webpage).strip('').replace('\n', ' ').strip('“”')
  169. # strip 'https -_t.co_BJYgOjSeGA' junk from filenames
  170. title = re.sub(r'\s+(https?://[^ ]+)', '', title)
  171. info = {
  172. 'uploader_id': user_id,
  173. 'uploader': username,
  174. 'webpage_url': url,
  175. 'description': '%s on Twitter: "%s"' % (username, description),
  176. 'title': username + ' - ' + title,
  177. }
  178. card_id = self._search_regex(
  179. r'["\']/i/cards/tfw/v1/(\d+)', webpage, 'twitter card url', default=None)
  180. if card_id:
  181. card_url = 'https://twitter.com/i/cards/tfw/v1/' + card_id
  182. info.update({
  183. '_type': 'url_transparent',
  184. 'ie_key': 'TwitterCard',
  185. 'url': card_url,
  186. })
  187. return info
  188. mobj = re.search(r'''(?x)
  189. <video[^>]+class="animated-gif"(?P<more_info>[^>]+)>\s*
  190. <source[^>]+video-src="(?P<url>[^"]+)"
  191. ''', webpage)
  192. if mobj:
  193. more_info = mobj.group('more_info')
  194. height = int_or_none(self._search_regex(
  195. r'data-height="(\d+)"', more_info, 'height', fatal=False))
  196. width = int_or_none(self._search_regex(
  197. r'data-width="(\d+)"', more_info, 'width', fatal=False))
  198. thumbnail = self._search_regex(
  199. r'poster="([^"]+)"', more_info, 'poster', fatal=False)
  200. info.update({
  201. 'id': twid,
  202. 'url': mobj.group('url'),
  203. 'height': height,
  204. 'width': width,
  205. 'thumbnail': thumbnail,
  206. })
  207. return info
  208. raise ExtractorError('There\'s no video in this tweet.')
  209. class TwitterAmplifyIE(TwitterBaseIE):
  210. IE_NAME = 'twitter:amplify'
  211. _VALID_URL = 'https?://amp\.twimg\.com/v/(?P<id>[0-9a-f\-]{36})'
  212. _TEST = {
  213. 'url': 'https://amp.twimg.com/v/0ba0c3c7-0af3-4c0a-bed5-7efd1ffa2951',
  214. 'md5': '7df102d0b9fd7066b86f3159f8e81bf6',
  215. 'info_dict': {
  216. 'id': '0ba0c3c7-0af3-4c0a-bed5-7efd1ffa2951',
  217. 'ext': 'mp4',
  218. 'title': 'Twitter Video',
  219. 'thumbnail': 're:^https?://.*',
  220. },
  221. }
  222. def _real_extract(self, url):
  223. video_id = self._match_id(url)
  224. webpage = self._download_webpage(url, video_id)
  225. vmap_url = self._html_search_meta(
  226. 'twitter:amplify:vmap', webpage, 'vmap url')
  227. video_url = self._get_vmap_video_url(vmap_url, video_id)
  228. thumbnails = []
  229. thumbnail = self._html_search_meta(
  230. 'twitter:image:src', webpage, 'thumbnail', fatal=False)
  231. def _find_dimension(target):
  232. w = int_or_none(self._html_search_meta(
  233. 'twitter:%s:width' % target, webpage, fatal=False))
  234. h = int_or_none(self._html_search_meta(
  235. 'twitter:%s:height' % target, webpage, fatal=False))
  236. return w, h
  237. if thumbnail:
  238. thumbnail_w, thumbnail_h = _find_dimension('image')
  239. thumbnails.append({
  240. 'url': thumbnail,
  241. 'width': thumbnail_w,
  242. 'height': thumbnail_h,
  243. })
  244. video_w, video_h = _find_dimension('player')
  245. formats = [{
  246. 'url': video_url,
  247. 'width': video_w,
  248. 'height': video_h,
  249. }]
  250. return {
  251. 'id': video_id,
  252. 'title': 'Twitter Video',
  253. 'formats': formats,
  254. 'thumbnails': thumbnails,
  255. }