]> git.bitcoin.ninja Git - youtube-dl/blob - youtube_dl/extractor/twitter.py
b2fff73b94bb8403550816cd100a0ceb792cba8e
[youtube-dl] / youtube_dl / extractor / twitter.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..compat import compat_urllib_request
8 from ..utils import (
9     float_or_none,
10     unescapeHTML,
11 )
12
13
14 class TwitterCardIE(InfoExtractor):
15     _VALID_URL = r'https?://(?:www\.)?twitter\.com/i/cards/tfw/v1/(?P<id>\d+)'
16     _TESTS = [
17         {
18             'url': 'https://twitter.com/i/cards/tfw/v1/560070183650213889',
19             'md5': '7d2f6b4d2eb841a7ccc893d479bfceb4',
20             'info_dict': {
21                 'id': '560070183650213889',
22                 'ext': 'mp4',
23                 'title': 'TwitterCard',
24                 'thumbnail': 're:^https?://.*\.jpg$',
25                 'duration': 30.033,
26             }
27         },
28         {
29             'url': 'https://twitter.com/i/cards/tfw/v1/623160978427936768',
30             'md5': '7ee2a553b63d1bccba97fbed97d9e1c8',
31             'info_dict': {
32                 'id': '623160978427936768',
33                 'ext': 'mp4',
34                 'title': 'TwitterCard',
35                 'thumbnail': 're:^https?://.*\.jpg',
36                 'duration': 80.155,
37             },
38         }
39     ]
40
41     def _real_extract(self, url):
42         video_id = self._match_id(url)
43
44         # Different formats served for different User-Agents
45         USER_AGENTS = [
46             'Mozilla/5.0 (X11; Linux x86_64; rv:10.0) Gecko/20150101 Firefox/20.0 (Chrome)',  # mp4
47             'Mozilla/5.0 (Windows NT 5.2; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0',  # webm
48         ]
49
50         config = None
51         formats = []
52         for user_agent in USER_AGENTS:
53             request = compat_urllib_request.Request(url)
54             request.add_header('User-Agent', user_agent)
55             webpage = self._download_webpage(request, video_id)
56
57             config = self._parse_json(
58                 unescapeHTML(self._search_regex(
59                     r'data-player-config="([^"]+)"', webpage, 'data player config')),
60                 video_id)
61             if 'playlist' not in config:
62                 if 'vmapUrl' in config:
63                     webpage = self._download_webpage(config['vmapUrl'], video_id + ' (xml)')
64                     video_url = self._search_regex(
65                         r'<MediaFile>\s*<!\[CDATA\[(https?://.+?)\]\]>', webpage, 'data player config (xml)')
66                     f = {
67                         'url': video_url,
68                     }
69                     ext = re.search(r'\.([a-z0-9]{2,4})(\?.+)?$', video_url)
70                     if ext:
71                         f['ext'] = ext.group(1)
72                     formats.append(f)
73                     break   # same video regardless of UA
74                 continue
75
76             video_url = config['playlist'][0]['source']
77
78             f = {
79                 'url': video_url,
80             }
81
82             m = re.search(r'/(?P<width>\d+)x(?P<height>\d+)/', video_url)
83             if m:
84                 f.update({
85                     'width': int(m.group('width')),
86                     'height': int(m.group('height')),
87                 })
88             formats.append(f)
89         self._sort_formats(formats)
90
91         thumbnail = config.get('posterImageUrl')
92         duration = float_or_none(config.get('duration'))
93
94         return {
95             'id': video_id,
96             'title': 'TwitterCard',
97             'thumbnail': thumbnail,
98             'duration': duration,
99             'formats': formats,
100         }
101
102
103 class TwitterIE(TwitterCardIE):
104     _VALID_URL = r'https?://(?:www|m|mobile)?\.?twitter\.com/(?P<id>[^/]+/status/\d+)'
105
106     _TESTS = [{
107         'url': 'https://twitter.com/freethenipple/status/643211948184596480',
108         'md5': '31cd83a116fc41f99ae3d909d4caf6a0',
109         'info_dict': {
110             'id': '643211948184596480',
111             'ext': 'mp4',
112             'title': 'freethenipple - FTN supporters on Hollywood Blvd today!',
113             'thumbnail': 're:^https?://.*\.jpg',
114             'duration': 12.922,
115             'description': 'FREE THE NIPPLE on Twitter: "FTN supporters on Hollywood Blvd today! http://t.co/c7jHH749xJ"',
116             'uploader': 'FREE THE NIPPLE',
117             'uploader_id': 'freethenipple',
118         },
119     }]
120
121     def _real_extract(self, url):
122         id = self._match_id(url)
123         username, twid = re.match(r'([^/]+)/status/(\d+)', id).groups()
124         name = username
125         url = re.sub(r'https?://(m|mobile)\.', 'https://', url)
126         webpage = self._download_webpage(url, 'tweet: ' + url)
127         description = unescapeHTML(self._search_regex('<title>\s*(.+?)\s*</title>', webpage, 'title'))
128         title = description.replace('\n', ' ')
129         splitdesc = re.match(r'^(.+?)\s*on Twitter:\s* "(.+?)"$', title)
130         if splitdesc:
131             name, title = splitdesc.groups()
132         title = re.sub(r'\s*https?://[^ ]+', '', title)  # strip  'https -_t.co_BJYgOjSeGA' junk from filenames
133         card_id = self._search_regex(r'["\']/i/cards/tfw/v1/(\d+)', webpage, '/i/card/...')
134         card_url = 'https://twitter.com/i/cards/tfw/v1/' + card_id
135         return {
136             '_type': 'url_transparent',
137             'ie_key': 'TwitterCard',
138             'uploader_id': username,
139             'uploader': name,
140             'url': card_url,
141             'webpage_url': url,
142             'description': description,
143             'title': username + ' - ' + title,
144         }