add twitter views back.

This commit is contained in:
Avi Peretz 2019-11-14 16:41:25 +02:00
parent 7ed03ea77e
commit 3e1144a71d

View File

@ -2,6 +2,8 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import re import re
import time
from .common import InfoExtractor from .common import InfoExtractor
from ..compat import ( from ..compat import (
@ -20,6 +22,7 @@ from ..utils import (
unified_timestamp, unified_timestamp,
update_url_query, update_url_query,
xpath_text, xpath_text,
parse_count
) )
from .periscope import ( from .periscope import (
@ -79,11 +82,20 @@ class TwitterBaseIE(InfoExtractor):
headers = { headers = {
'Authorization': 'Bearer AAAAAAAAAAAAAAAAAAAAAPYXBAAAAAAACLXUNDekMxqa8h%2F40K4moUkGsoc%3DTYfbDKbT3jJPCEVnMYqilB28NHfOPqkca3qaAxGfsyKCs0wRbw', 'Authorization': 'Bearer AAAAAAAAAAAAAAAAAAAAAPYXBAAAAAAACLXUNDekMxqa8h%2F40K4moUkGsoc%3DTYfbDKbT3jJPCEVnMYqilB28NHfOPqkca3qaAxGfsyKCs0wRbw',
} }
ct0 = self._get_cookies(self._API_BASE).get('ct0')
if ct0:
headers['x-csrf-token'] = ct0.value
if not self._GUEST_TOKEN: if not self._GUEST_TOKEN:
self._GUEST_TOKEN = self._download_json( guest_token_c = self._get_cookies('http://api.twitter.com/').get('gt')
self._API_BASE + 'guest/activate.json', video_id, if guest_token_c:
'Downloading guest token', data=b'', self._GUEST_TOKEN = guest_token_c
headers=headers)['guest_token'] else:
self._GUEST_TOKEN = self._download_json(
self._API_BASE + 'guest/activate.json', video_id,
'Downloading guest token', data=b'',
headers=headers)['guest_token']
self._set_cookie('api.twitter.com', 'gt', self._GUEST_TOKEN, expire_time=time.time() + 3000)
headers['x-guest-token'] = self._GUEST_TOKEN headers['x-guest-token'] = self._GUEST_TOKEN
try: try:
return self._download_json( return self._download_json(
@ -380,6 +392,12 @@ class TwitterIE(TwitterBaseIE):
def _real_extract(self, url): def _real_extract(self, url):
twid = self._match_id(url) twid = self._match_id(url)
# Download page to fetch cookies
self._download_webpage(url, twid)
config = self._call_api(
'videos/tweet/config/%s.json' % twid, twid
)
status = self._call_api( status = self._call_api(
'statuses/show/%s.json' % twid, twid, { 'statuses/show/%s.json' % twid, twid, {
'cards_platform': 'Web-12', 'cards_platform': 'Web-12',
@ -389,6 +407,8 @@ class TwitterIE(TwitterBaseIE):
'tweet_mode': 'extended', 'tweet_mode': 'extended',
}) })
title = description = status['full_text'].replace('\n', ' ') title = description = status['full_text'].replace('\n', ' ')
# strip 'https -_t.co_BJYgOjSeGA' junk from filenames # strip 'https -_t.co_BJYgOjSeGA' junk from filenames
title = re.sub(r'\s+(https?://[^ ]+)', '', title) title = re.sub(r'\s+(https?://[^ ]+)', '', title)
@ -447,6 +467,7 @@ class TwitterIE(TwitterBaseIE):
'formats': formats, 'formats': formats,
'thumbnails': thumbnails, 'thumbnails': thumbnails,
'duration': float_or_none(video_info.get('duration_millis'), 1000), 'duration': float_or_none(video_info.get('duration_millis'), 1000),
'view_count': parse_count(config.get('track', {}).get('viewCount'))
}) })
else: else:
card = status.get('card') card = status.get('card')