twitter.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. }, {
  150. 'url': 'https://twitter.com/starwars/status/665052190608723968',
  151. 'md5': '39b7199856dee6cd4432e72c74bc69d4',
  152. 'info_dict': {
  153. 'id': '665052190608723968',
  154. 'ext': 'mp4',
  155. 'title': 'Star Wars - A new beginning is coming December 18. Watch the official 60 second #TV spot for #StarWars: #TheForceAwakens.',
  156. 'description': 'Star Wars on Twitter: "A new beginning is coming December 18. Watch the official 60 second #TV spot for #StarWars: #TheForceAwakens."',
  157. 'uploader_id': 'starwars',
  158. 'uploader': 'Star Wars',
  159. },
  160. }]
  161. def _real_extract(self, url):
  162. mobj = re.match(self._VALID_URL, url)
  163. user_id = mobj.group('user_id')
  164. twid = mobj.group('id')
  165. webpage = self._download_webpage(self._TEMPLATE_URL % (user_id, twid), twid)
  166. username = remove_end(self._og_search_title(webpage), ' on Twitter')
  167. title = description = self._og_search_description(webpage).strip('').replace('\n', ' ').strip('“”')
  168. # strip 'https -_t.co_BJYgOjSeGA' junk from filenames
  169. title = re.sub(r'\s+(https?://[^ ]+)', '', title)
  170. info = {
  171. 'uploader_id': user_id,
  172. 'uploader': username,
  173. 'webpage_url': url,
  174. 'description': '%s on Twitter: "%s"' % (username, description),
  175. 'title': username + ' - ' + title,
  176. }
  177. card_id = self._search_regex(
  178. r'["\']/i/cards/tfw/v1/(\d+)', webpage, 'twitter card url', default=None)
  179. if card_id:
  180. card_url = 'https://twitter.com/i/cards/tfw/v1/' + card_id
  181. info.update({
  182. '_type': 'url_transparent',
  183. 'ie_key': 'TwitterCard',
  184. 'url': card_url,
  185. })
  186. return info
  187. mobj = re.search(r'''(?x)
  188. <video[^>]+class="animated-gif"[^>]+
  189. (?:data-height="(?P<height>\d+)")?[^>]+
  190. (?:data-width="(?P<width>\d+)")?[^>]+
  191. (?:poster="(?P<poster>[^"]+)")?[^>]*>\s*
  192. <source[^>]+video-src="(?P<url>[^"]+)"
  193. ''', webpage)
  194. if mobj:
  195. info.update({
  196. 'id': twid,
  197. 'url': mobj.group('url'),
  198. 'height': int_or_none(mobj.group('height')),
  199. 'width': int_or_none(mobj.group('width')),
  200. 'thumbnail': mobj.group('poster'),
  201. })
  202. return info
  203. raise ExtractorError('There\'s not video in this tweet.')
  204. class TwitterAmplifyIE(TwitterBaseIE):
  205. IE_NAME = 'twitter:amplify'
  206. _VALID_URL = 'https?://amp\.twimg\.com/v/(?P<id>[0-9a-f\-]{36})'
  207. _TEST = {
  208. 'url': 'https://amp.twimg.com/v/0ba0c3c7-0af3-4c0a-bed5-7efd1ffa2951',
  209. 'md5': '7df102d0b9fd7066b86f3159f8e81bf6',
  210. 'info_dict': {
  211. 'id': '0ba0c3c7-0af3-4c0a-bed5-7efd1ffa2951',
  212. 'ext': 'mp4',
  213. 'title': 'Twitter Video',
  214. },
  215. }
  216. def _real_extract(self, url):
  217. video_id = self._match_id(url)
  218. webpage = self._download_webpage(url, video_id)
  219. vmap_url = self._html_search_meta(
  220. 'twitter:amplify:vmap', webpage, 'vmap url')
  221. video_url = self._get_vmap_video_url(vmap_url, video_id)
  222. return {
  223. 'id': video_id,
  224. 'title': 'Twitter Video',
  225. 'url': video_url,
  226. }