2016-10-06 19:12:07 +02:00
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
2020-02-02 17:19:14 +01:00
|
|
|
|
# Copyright 2016-2020 Mike Fährmann
|
2016-10-06 19:12:07 +02:00
|
|
|
|
#
|
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
|
# it under the terms of the GNU General Public License version 2 as
|
|
|
|
|
# published by the Free Software Foundation.
|
|
|
|
|
|
2020-02-02 17:19:14 +01:00
|
|
|
|
"""Extractors for https://twitter.com/"""
|
2016-10-06 19:12:07 +02:00
|
|
|
|
|
|
|
|
|
from .common import Extractor, Message
|
2019-04-07 23:06:57 +02:00
|
|
|
|
from .. import text, exception
|
2019-10-25 13:41:36 +02:00
|
|
|
|
from ..cache import cache, memcache
|
2020-06-03 20:51:29 +02:00
|
|
|
|
import hashlib
|
|
|
|
|
import time
|
2016-10-06 19:12:07 +02:00
|
|
|
|
|
2017-02-01 00:53:19 +01:00
|
|
|
|
|
2018-08-17 20:04:11 +02:00
|
|
|
|
class TwitterExtractor(Extractor):
|
|
|
|
|
"""Base class for twitter extractors"""
|
2016-10-06 19:12:07 +02:00
|
|
|
|
category = "twitter"
|
2020-06-06 23:51:54 +02:00
|
|
|
|
directory_fmt = ("{category}", "{user[name]}")
|
|
|
|
|
filename_fmt = "{tweet_id}_{num}.{extension}"
|
|
|
|
|
archive_fmt = "{tweet_id}_{retweet_id}_{num}"
|
2020-03-12 22:02:12 +01:00
|
|
|
|
cookiedomain = ".twitter.com"
|
2018-08-17 20:04:11 +02:00
|
|
|
|
root = "https://twitter.com"
|
2019-04-30 15:43:43 +02:00
|
|
|
|
sizes = (":orig", ":large", ":medium", ":small")
|
2018-08-17 20:04:11 +02:00
|
|
|
|
|
2018-08-19 20:36:33 +02:00
|
|
|
|
def __init__(self, match):
|
2019-02-11 13:31:10 +01:00
|
|
|
|
Extractor.__init__(self, match)
|
2018-08-19 20:36:33 +02:00
|
|
|
|
self.user = match.group(1)
|
|
|
|
|
self.retweets = self.config("retweets", True)
|
2020-04-29 23:11:24 +02:00
|
|
|
|
self.replies = self.config("replies", True)
|
2020-01-18 21:26:46 +01:00
|
|
|
|
self.twitpic = self.config("twitpic", False)
|
2020-02-14 01:03:42 +01:00
|
|
|
|
self.videos = self.config("videos", True)
|
2020-06-06 23:51:54 +02:00
|
|
|
|
self._user_cache = {}
|
2018-09-30 18:41:39 +02:00
|
|
|
|
|
2018-08-17 20:04:11 +02:00
|
|
|
|
def items(self):
|
2019-04-07 23:06:57 +02:00
|
|
|
|
self.login()
|
2019-11-30 21:51:08 +01:00
|
|
|
|
metadata = self.metadata()
|
2018-08-17 20:04:11 +02:00
|
|
|
|
yield Message.Version, 1
|
|
|
|
|
|
|
|
|
|
for tweet in self.tweets():
|
2020-06-04 01:22:34 +02:00
|
|
|
|
|
2020-06-03 20:51:29 +02:00
|
|
|
|
if not self.retweets and "retweeted_status_id_str" in tweet or \
|
|
|
|
|
not self.replies and "in_reply_to_user_id_str" in tweet:
|
|
|
|
|
continue
|
|
|
|
|
|
2020-06-04 01:22:34 +02:00
|
|
|
|
if self.twitpic:
|
|
|
|
|
self._extract_twitpic(tweet)
|
2020-06-03 20:51:29 +02:00
|
|
|
|
if "extended_entities" not in tweet:
|
2018-08-17 20:04:11 +02:00
|
|
|
|
continue
|
|
|
|
|
|
2020-06-06 23:51:54 +02:00
|
|
|
|
tdata = self._transform_tweet(tweet)
|
|
|
|
|
tdata.update(metadata)
|
2020-06-04 18:21:54 +02:00
|
|
|
|
|
2020-06-06 23:51:54 +02:00
|
|
|
|
yield Message.Directory, tdata
|
|
|
|
|
for tdata["num"], media in enumerate(
|
|
|
|
|
tweet["extended_entities"]["media"], 1):
|
2020-06-04 18:21:54 +02:00
|
|
|
|
|
2020-06-06 23:51:54 +02:00
|
|
|
|
tdata["width"] = media["original_info"].get("width", 0)
|
|
|
|
|
tdata["height"] = media["original_info"].get("height", 0)
|
2020-06-03 20:51:29 +02:00
|
|
|
|
|
|
|
|
|
if "video_info" in media and self.videos:
|
|
|
|
|
|
|
|
|
|
if self.videos == "ytdl":
|
|
|
|
|
url = "ytdl:{}/i/web/status/{}".format(
|
|
|
|
|
self.root, tweet["id_str"])
|
2020-06-06 23:51:54 +02:00
|
|
|
|
tdata["extension"] = None
|
|
|
|
|
yield Message.Url, url, tdata
|
2020-06-03 20:51:29 +02:00
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
video_info = media["video_info"]
|
|
|
|
|
variant = max(
|
|
|
|
|
video_info["variants"],
|
|
|
|
|
key=lambda v: v.get("bitrate", 0),
|
|
|
|
|
)
|
2020-06-06 23:51:54 +02:00
|
|
|
|
tdata["duration"] = video_info.get(
|
2020-06-03 20:51:29 +02:00
|
|
|
|
"duration_millis", 0) / 1000
|
2020-06-06 23:51:54 +02:00
|
|
|
|
tdata["bitrate"] = variant.get("bitrate", 0)
|
2020-06-03 20:51:29 +02:00
|
|
|
|
|
|
|
|
|
url = variant["url"]
|
2020-06-06 23:51:54 +02:00
|
|
|
|
text.nameext_from_url(url, tdata)
|
|
|
|
|
yield Message.Url, url, tdata
|
2019-11-30 21:51:08 +01:00
|
|
|
|
|
2020-06-04 01:22:34 +02:00
|
|
|
|
elif "media_url_https" in media:
|
2020-06-03 20:51:29 +02:00
|
|
|
|
url = media["media_url_https"]
|
2019-11-30 21:51:08 +01:00
|
|
|
|
urls = [url + size for size in self.sizes]
|
2020-06-06 23:51:54 +02:00
|
|
|
|
text.nameext_from_url(url, tdata)
|
|
|
|
|
yield Message.Urllist, urls, tdata
|
2020-01-18 21:26:46 +01:00
|
|
|
|
|
2020-06-04 01:22:34 +02:00
|
|
|
|
else:
|
|
|
|
|
url = media["media_url"]
|
2020-06-06 23:51:54 +02:00
|
|
|
|
text.nameext_from_url(url, tdata)
|
|
|
|
|
yield Message.Url, url, tdata
|
2020-06-04 01:22:34 +02:00
|
|
|
|
|
|
|
|
|
def _extract_twitpic(self, tweet):
|
|
|
|
|
twitpics = []
|
|
|
|
|
for url in tweet["entities"].get("urls", ()):
|
|
|
|
|
url = url["expanded_url"]
|
|
|
|
|
if "//twitpic.com/" in url:
|
|
|
|
|
response = self.request(url, fatal=False)
|
|
|
|
|
if response.status_code >= 400:
|
|
|
|
|
continue
|
|
|
|
|
url = text.extract(
|
|
|
|
|
response.text, 'name="twitter:image" value="', '"')[0]
|
|
|
|
|
twitpics.append({
|
|
|
|
|
"original_info": {},
|
|
|
|
|
"media_url" : url,
|
|
|
|
|
})
|
|
|
|
|
if twitpics:
|
|
|
|
|
if "extended_entities" in tweet:
|
|
|
|
|
tweet["extended_entities"]["media"].extend(twitpics)
|
|
|
|
|
else:
|
|
|
|
|
tweet["extended_entities"] = {"media": twitpics}
|
|
|
|
|
|
2020-06-06 23:51:54 +02:00
|
|
|
|
def _transform_tweet(self, tweet):
|
|
|
|
|
entities = tweet["entities"]
|
|
|
|
|
tdata = {
|
|
|
|
|
"tweet_id" : text.parse_int(tweet["id_str"]),
|
|
|
|
|
"retweet_id" : text.parse_int(
|
|
|
|
|
tweet.get("retweeted_status_id_str")),
|
|
|
|
|
"quote_id" : text.parse_int(
|
|
|
|
|
tweet.get("quoted_status_id_str")),
|
|
|
|
|
"reply_id" : text.parse_int(
|
|
|
|
|
tweet.get("in_reply_to_status_id_str")),
|
|
|
|
|
"date" : text.parse_datetime(
|
|
|
|
|
tweet["created_at"], "%a %b %d %H:%M:%S %z %Y"),
|
|
|
|
|
"user" : self._transform_user(tweet["user"]),
|
|
|
|
|
"lang" : tweet["lang"],
|
|
|
|
|
"content" : tweet["full_text"],
|
|
|
|
|
"favorite_count": tweet["favorite_count"],
|
|
|
|
|
"quote_count" : tweet["quote_count"],
|
|
|
|
|
"reply_count" : tweet["reply_count"],
|
|
|
|
|
"retweet_count" : tweet["retweet_count"],
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hashtags = entities.get("hashtags")
|
|
|
|
|
if hashtags:
|
|
|
|
|
tdata["hashtags"] = [t["text"] for t in hashtags]
|
|
|
|
|
|
|
|
|
|
mentions = entities.get("user_mentions")
|
|
|
|
|
if mentions:
|
|
|
|
|
tdata["mentions"] = [{
|
|
|
|
|
"id": text.parse_int(u["id_str"]),
|
|
|
|
|
"name": u["screen_name"],
|
|
|
|
|
"nick": u["name"],
|
|
|
|
|
} for u in mentions]
|
|
|
|
|
|
2020-06-09 21:48:04 +02:00
|
|
|
|
if "in_reply_to_screen_name" in tweet:
|
|
|
|
|
tdata["reply_to"] = tweet["in_reply_to_screen_name"]
|
|
|
|
|
|
2020-06-06 23:51:54 +02:00
|
|
|
|
if "full_text_quoted" in tweet:
|
|
|
|
|
tdata["content_quoted"] = tweet["full_text_quoted"]
|
|
|
|
|
|
|
|
|
|
if "author" in tweet:
|
|
|
|
|
tdata["author"] = self._transform_user(tweet["author"])
|
|
|
|
|
|
|
|
|
|
return tdata
|
|
|
|
|
|
|
|
|
|
def _transform_user(self, user):
|
|
|
|
|
uid = user["id_str"]
|
|
|
|
|
cache = self._user_cache
|
|
|
|
|
|
|
|
|
|
if uid not in cache:
|
|
|
|
|
cache[uid] = {
|
|
|
|
|
"id" : text.parse_int(uid),
|
|
|
|
|
"name" : user["screen_name"],
|
|
|
|
|
"nick" : user["name"],
|
|
|
|
|
"description" : user["description"],
|
|
|
|
|
"location" : user["location"],
|
|
|
|
|
"date" : text.parse_datetime(
|
|
|
|
|
user["created_at"], "%a %b %d %H:%M:%S %z %Y"),
|
|
|
|
|
"verified" : user.get("verified", False),
|
|
|
|
|
"profile_banner" : user.get("profile_banner_url", ""),
|
|
|
|
|
"profile_image" : user.get(
|
|
|
|
|
"profile_image_url_https", "").replace("_normal.", "."),
|
|
|
|
|
"favourites_count": user["favourites_count"],
|
|
|
|
|
"followers_count" : user["followers_count"],
|
|
|
|
|
"friends_count" : user["friends_count"],
|
|
|
|
|
"listed_count" : user["listed_count"],
|
|
|
|
|
"media_count" : user["media_count"],
|
|
|
|
|
"statuses_count" : user["statuses_count"],
|
|
|
|
|
}
|
|
|
|
|
return cache[uid]
|
|
|
|
|
|
2018-08-17 20:04:11 +02:00
|
|
|
|
def metadata(self):
|
|
|
|
|
"""Return general metadata"""
|
2019-11-30 21:51:08 +01:00
|
|
|
|
return {}
|
2018-08-17 20:04:11 +02:00
|
|
|
|
|
|
|
|
|
def tweets(self):
|
2020-06-03 20:51:29 +02:00
|
|
|
|
"""Yield all relevant tweet objects"""
|
2018-08-17 20:04:11 +02:00
|
|
|
|
|
2019-04-07 23:06:57 +02:00
|
|
|
|
def login(self):
|
|
|
|
|
username, password = self._get_auth_info()
|
|
|
|
|
if username:
|
|
|
|
|
self._update_cookies(self._login_impl(username, password))
|
|
|
|
|
|
|
|
|
|
@cache(maxage=360*24*3600, keyarg=1)
|
|
|
|
|
def _login_impl(self, username, password):
|
|
|
|
|
self.log.info("Logging in as %s", username)
|
|
|
|
|
|
2020-06-04 00:07:12 +02:00
|
|
|
|
url = "https://mobile.twitter.com/i/nojs_router"
|
|
|
|
|
params = {"path": "/login"}
|
|
|
|
|
headers = {"Referer": self.root + "/", "Origin": self.root}
|
|
|
|
|
page = self.request(
|
|
|
|
|
url, method="POST", params=params, headers=headers, data={}).text
|
|
|
|
|
|
2019-04-07 23:06:57 +02:00
|
|
|
|
pos = page.index('name="authenticity_token"')
|
2020-06-04 00:07:12 +02:00
|
|
|
|
token = text.extract(page, 'value="', '"', pos)[0]
|
2019-04-07 23:06:57 +02:00
|
|
|
|
|
2020-06-04 00:07:12 +02:00
|
|
|
|
url = "https://mobile.twitter.com/sessions"
|
2019-04-07 23:06:57 +02:00
|
|
|
|
data = {
|
2020-06-04 00:07:12 +02:00
|
|
|
|
"authenticity_token" : token,
|
2019-04-07 23:06:57 +02:00
|
|
|
|
"session[username_or_email]": username,
|
|
|
|
|
"session[password]" : password,
|
|
|
|
|
"remember_me" : "1",
|
2020-06-04 00:07:12 +02:00
|
|
|
|
"wfa" : "1",
|
|
|
|
|
"commit" : "+Log+in+",
|
|
|
|
|
"ui_metrics" : "",
|
2019-04-07 23:06:57 +02:00
|
|
|
|
}
|
2020-06-04 00:07:12 +02:00
|
|
|
|
response = self.request(url, method="POST", data=data)
|
|
|
|
|
cookies = {
|
2020-03-12 22:02:12 +01:00
|
|
|
|
cookie.name: cookie.value
|
|
|
|
|
for cookie in self.session.cookies
|
2020-06-04 00:07:12 +02:00
|
|
|
|
if cookie.domain == self.cookiedomain
|
2020-03-12 22:02:12 +01:00
|
|
|
|
}
|
2020-06-04 00:07:12 +02:00
|
|
|
|
|
|
|
|
|
if "/error" in response.url or "auth_token" not in cookies:
|
|
|
|
|
raise exception.AuthenticationError()
|
|
|
|
|
return cookies
|
2018-08-17 20:04:11 +02:00
|
|
|
|
|
|
|
|
|
|
2018-08-19 20:36:33 +02:00
|
|
|
|
class TwitterTimelineExtractor(TwitterExtractor):
|
|
|
|
|
"""Extractor for all images from a user's timeline"""
|
|
|
|
|
subcategory = "timeline"
|
2019-02-08 13:45:40 +01:00
|
|
|
|
pattern = (r"(?:https?://)?(?:www\.|mobile\.)?twitter\.com"
|
2019-10-17 18:34:07 +02:00
|
|
|
|
r"/(?!search)([^/?&#]+)/?(?:$|[?#])")
|
2019-09-01 17:37:48 +02:00
|
|
|
|
test = (
|
|
|
|
|
("https://twitter.com/supernaturepics", {
|
|
|
|
|
"range": "1-40",
|
|
|
|
|
"url": "0106229d408f4111d9a52c8fd2ad687f64842aa4",
|
|
|
|
|
}),
|
|
|
|
|
("https://mobile.twitter.com/supernaturepics?p=i"),
|
|
|
|
|
)
|
2018-08-19 20:36:33 +02:00
|
|
|
|
|
|
|
|
|
def tweets(self):
|
2020-06-03 20:51:29 +02:00
|
|
|
|
return TwitterAPI(self).timeline_profile(self.user)
|
2018-08-19 20:36:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TwitterMediaExtractor(TwitterExtractor):
|
|
|
|
|
"""Extractor for all images from a user's Media Tweets"""
|
|
|
|
|
subcategory = "media"
|
2019-02-08 13:45:40 +01:00
|
|
|
|
pattern = (r"(?:https?://)?(?:www\.|mobile\.)?twitter\.com"
|
2019-10-17 18:34:07 +02:00
|
|
|
|
r"/(?!search)([^/?&#]+)/media(?!\w)")
|
2019-09-01 17:37:48 +02:00
|
|
|
|
test = (
|
|
|
|
|
("https://twitter.com/supernaturepics/media", {
|
|
|
|
|
"range": "1-40",
|
|
|
|
|
"url": "0106229d408f4111d9a52c8fd2ad687f64842aa4",
|
|
|
|
|
}),
|
|
|
|
|
("https://mobile.twitter.com/supernaturepics/media#t"),
|
|
|
|
|
)
|
2018-08-19 20:36:33 +02:00
|
|
|
|
|
|
|
|
|
def tweets(self):
|
2020-06-03 20:51:29 +02:00
|
|
|
|
return TwitterAPI(self).timeline_media(self.user)
|
2018-08-19 20:36:33 +02:00
|
|
|
|
|
2019-10-17 18:34:07 +02:00
|
|
|
|
|
2019-10-16 18:23:10 +02:00
|
|
|
|
class TwitterSearchExtractor(TwitterExtractor):
|
|
|
|
|
"""Extractor for all images from a search timeline"""
|
|
|
|
|
subcategory = "search"
|
2019-10-17 18:34:07 +02:00
|
|
|
|
directory_fmt = ("{category}", "Search", "{search}")
|
2019-10-16 18:23:10 +02:00
|
|
|
|
pattern = (r"(?:https?://)?(?:www\.|mobile\.)?twitter\.com"
|
2019-10-17 18:34:07 +02:00
|
|
|
|
r"/search/?\?(?:[^&#]+&)*q=([^&#]+)")
|
|
|
|
|
test = ("https://twitter.com/search?q=nature", {
|
|
|
|
|
"range": "1-40",
|
|
|
|
|
"count": 40,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
def metadata(self):
|
2020-06-07 03:10:09 +02:00
|
|
|
|
return {"search": text.unquote(self.user)}
|
2019-10-17 18:34:07 +02:00
|
|
|
|
|
2019-10-16 18:23:10 +02:00
|
|
|
|
def tweets(self):
|
2020-06-03 20:51:29 +02:00
|
|
|
|
return TwitterAPI(self).search(self.user)
|
2019-10-17 18:34:07 +02:00
|
|
|
|
|
2018-08-19 20:36:33 +02:00
|
|
|
|
|
2018-08-17 20:04:11 +02:00
|
|
|
|
class TwitterTweetExtractor(TwitterExtractor):
|
2018-08-18 18:58:10 +02:00
|
|
|
|
"""Extractor for images from individual tweets"""
|
2018-08-17 20:04:11 +02:00
|
|
|
|
subcategory = "tweet"
|
2019-02-08 13:45:40 +01:00
|
|
|
|
pattern = (r"(?:https?://)?(?:www\.|mobile\.)?twitter\.com"
|
2019-09-24 21:18:05 +02:00
|
|
|
|
r"/([^/?&#]+|i/web)/status/(\d+)")
|
2019-02-08 13:45:40 +01:00
|
|
|
|
test = (
|
2019-05-09 10:17:55 +02:00
|
|
|
|
("https://twitter.com/supernaturepics/status/604341487988576256", {
|
|
|
|
|
"url": "0e801d2f98142dd87c3630ded9e4be4a4d63b580",
|
|
|
|
|
"content": "ab05e1d8d21f8d43496df284d31e8b362cd3bcab",
|
2017-08-06 13:43:08 +02:00
|
|
|
|
}),
|
2019-04-21 15:41:22 +02:00
|
|
|
|
# 4 images
|
2017-08-06 13:43:08 +02:00
|
|
|
|
("https://twitter.com/perrypumas/status/894001459754180609", {
|
|
|
|
|
"url": "c8a262a9698cb733fb27870f5a8f75faf77d79f6",
|
2019-04-21 15:41:22 +02:00
|
|
|
|
}),
|
|
|
|
|
# video
|
|
|
|
|
("https://twitter.com/perrypumas/status/1065692031626829824", {
|
|
|
|
|
"options": (("videos", True),),
|
2020-06-03 20:51:29 +02:00
|
|
|
|
"pattern": r"https://video.twimg.com/ext_tw_video/.+\.mp4\?tag=5",
|
2017-08-06 13:43:08 +02:00
|
|
|
|
}),
|
2019-07-17 15:35:42 +02:00
|
|
|
|
# content with emoji, newlines, hashtags (#338)
|
2020-05-28 01:55:32 +02:00
|
|
|
|
("https://twitter.com/playpokemon/status/1263832915173048321", {
|
2020-06-06 23:51:54 +02:00
|
|
|
|
"keyword": {"content": (
|
2020-05-28 01:55:32 +02:00
|
|
|
|
r"re:Gear up for #PokemonSwordShieldEX with special Mystery "
|
|
|
|
|
"Gifts! \n\nYou’ll be able to receive four Galarian form "
|
|
|
|
|
"Pokémon with Hidden Abilities, plus some very useful items. "
|
|
|
|
|
"It’s our \\(Mystery\\) Gift to you, Trainers! \n\n❓🎁➡️ "
|
2020-02-22 02:59:56 +01:00
|
|
|
|
)},
|
2019-07-17 15:35:42 +02:00
|
|
|
|
}),
|
2019-09-01 17:37:48 +02:00
|
|
|
|
# Reply to another tweet (#403)
|
|
|
|
|
("https://twitter.com/tyson_hesse/status/1103767554424598528", {
|
2019-11-01 22:06:07 +01:00
|
|
|
|
"options": (("videos", "ytdl"),),
|
2019-12-25 17:28:55 +01:00
|
|
|
|
"pattern": r"ytdl:https://twitter.com/i/web.+/1103767554424598528",
|
2019-09-01 17:37:48 +02:00
|
|
|
|
}),
|
2020-04-29 23:11:24 +02:00
|
|
|
|
# 'replies' option (#705)
|
|
|
|
|
("https://twitter.com/tyson_hesse/status/1103767554424598528", {
|
|
|
|
|
"options": (("replies", False),),
|
|
|
|
|
"count": 0,
|
|
|
|
|
}),
|
2019-09-24 21:18:05 +02:00
|
|
|
|
# /i/web/ URL
|
|
|
|
|
("https://twitter.com/i/web/status/1155074198240292865", {
|
|
|
|
|
"pattern": r"https://pbs.twimg.com/media/EAel0vUUYAAZ4Bq.jpg:orig",
|
|
|
|
|
}),
|
2020-01-04 21:26:55 +01:00
|
|
|
|
# quoted tweet (#526)
|
2020-01-31 12:51:55 +01:00
|
|
|
|
("https://twitter.com/Pistachio/status/1222690391817932803", {
|
|
|
|
|
"pattern": r"https://pbs\.twimg\.com/media/EPfMfDUU8AAnByO\.jpg",
|
2020-01-04 21:26:55 +01:00
|
|
|
|
}),
|
2020-01-18 21:26:46 +01:00
|
|
|
|
# TwitPic embeds (#579)
|
|
|
|
|
("https://twitter.com/i/web/status/112900228289540096", {
|
|
|
|
|
"options": (("twitpic", True),),
|
|
|
|
|
"pattern": r"https://\w+.cloudfront.net/photos/large/\d+.jpg",
|
|
|
|
|
"count": 3,
|
|
|
|
|
}),
|
2019-02-08 13:45:40 +01:00
|
|
|
|
)
|
2016-10-06 19:12:07 +02:00
|
|
|
|
|
|
|
|
|
def __init__(self, match):
|
2018-08-19 20:36:33 +02:00
|
|
|
|
TwitterExtractor.__init__(self, match)
|
|
|
|
|
self.tweet_id = match.group(2)
|
2016-10-06 19:12:07 +02:00
|
|
|
|
|
2018-08-17 20:04:11 +02:00
|
|
|
|
def tweets(self):
|
2020-06-03 20:51:29 +02:00
|
|
|
|
return TwitterAPI(self).tweet(self.tweet_id)
|
2020-01-04 23:46:29 +01:00
|
|
|
|
|
|
|
|
|
|
2020-03-05 22:55:26 +01:00
|
|
|
|
class TwitterBookmarkExtractor(TwitterExtractor):
|
|
|
|
|
"""Extractor for bookmarked tweets"""
|
|
|
|
|
subcategory = "bookmark"
|
|
|
|
|
pattern = r"(?:https?://)?(?:www\.|mobile\.)?twitter\.com/i/bookmarks()"
|
|
|
|
|
test = ("https://twitter.com/i/bookmarks",)
|
|
|
|
|
|
2020-06-03 20:51:29 +02:00
|
|
|
|
def tweets(self):
|
|
|
|
|
return TwitterAPI(self).bookmarks()
|
2020-03-05 22:55:26 +01:00
|
|
|
|
|
2020-06-03 20:51:29 +02:00
|
|
|
|
|
|
|
|
|
class TwitterAPI():
|
|
|
|
|
|
|
|
|
|
def __init__(self, extractor):
|
|
|
|
|
self.extractor = extractor
|
|
|
|
|
self.headers = {
|
|
|
|
|
"authorization": "Bearer AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejR"
|
|
|
|
|
"COuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu"
|
|
|
|
|
"4FA33AGWWjCpTnA",
|
|
|
|
|
"x-guest-token": None,
|
|
|
|
|
"x-twitter-client-language": "en",
|
|
|
|
|
"x-twitter-active-user": "yes",
|
|
|
|
|
"x-csrf-token": None,
|
|
|
|
|
"Origin": "https://twitter.com",
|
|
|
|
|
"Referer": "https://twitter.com/",
|
|
|
|
|
}
|
|
|
|
|
self.params = {
|
2020-03-05 22:55:26 +01:00
|
|
|
|
"include_profile_interstitial_type": "1",
|
|
|
|
|
"include_blocking": "1",
|
|
|
|
|
"include_blocked_by": "1",
|
|
|
|
|
"include_followed_by": "1",
|
|
|
|
|
"include_want_retweets": "1",
|
|
|
|
|
"include_mute_edge": "1",
|
|
|
|
|
"include_can_dm": "1",
|
|
|
|
|
"include_can_media_tag": "1",
|
|
|
|
|
"skip_status": "1",
|
|
|
|
|
"cards_platform": "Web-12",
|
|
|
|
|
"include_cards": "1",
|
|
|
|
|
"include_composer_source": "true",
|
|
|
|
|
"include_ext_alt_text": "true",
|
|
|
|
|
"include_reply_count": "1",
|
|
|
|
|
"tweet_mode": "extended",
|
|
|
|
|
"include_entities": "true",
|
|
|
|
|
"include_user_entities": "true",
|
|
|
|
|
"include_ext_media_color": "true",
|
|
|
|
|
"include_ext_media_availability": "true",
|
|
|
|
|
"send_error_codes": "true",
|
2020-06-03 20:51:29 +02:00
|
|
|
|
"simple_quoted_tweet": "true",
|
|
|
|
|
# "count": "20",
|
2020-03-05 22:55:26 +01:00
|
|
|
|
"count": "100",
|
|
|
|
|
"cursor": None,
|
2020-06-07 03:10:09 +02:00
|
|
|
|
"ext": "mediaStats,highlightedLabel,cameraMoment",
|
2020-06-03 20:51:29 +02:00
|
|
|
|
"include_quote_count": "true",
|
2020-03-05 22:55:26 +01:00
|
|
|
|
}
|
2020-06-03 20:51:29 +02:00
|
|
|
|
|
|
|
|
|
cookies = self.extractor.session.cookies
|
|
|
|
|
|
|
|
|
|
# CSRF
|
|
|
|
|
csrf = hashlib.md5(str(time.time()).encode()).hexdigest()
|
|
|
|
|
self.headers["x-csrf-token"] = csrf
|
|
|
|
|
cookies.set("ct0", csrf, domain=".twitter.com")
|
|
|
|
|
|
|
|
|
|
if cookies.get("auth_token", domain=".twitter.com"):
|
|
|
|
|
self.headers["x-twitter-auth-type"] = "OAuth2Session"
|
|
|
|
|
else:
|
|
|
|
|
# guest token
|
|
|
|
|
guest_token = _guest_token(self.extractor, self.headers)
|
|
|
|
|
self.headers["x-guest-token"] = guest_token
|
|
|
|
|
cookies.set("gt", guest_token, domain=".twitter.com")
|
|
|
|
|
|
|
|
|
|
def tweet(self, tweet_id):
|
|
|
|
|
endpoint = "2/timeline/conversation/{}.json".format(tweet_id)
|
|
|
|
|
for tweet in self._pagination(endpoint):
|
|
|
|
|
if tweet["id_str"] == tweet_id:
|
|
|
|
|
return (tweet,)
|
|
|
|
|
return ()
|
|
|
|
|
|
|
|
|
|
def timeline_profile(self, screen_name):
|
|
|
|
|
user = self.user_by_screen_name(screen_name)
|
|
|
|
|
endpoint = "2/timeline/profile/{}.json".format(user["rest_id"])
|
|
|
|
|
return self._pagination(endpoint)
|
|
|
|
|
|
|
|
|
|
def timeline_media(self, screen_name):
|
|
|
|
|
user = self.user_by_screen_name(screen_name)
|
|
|
|
|
endpoint = "2/timeline/media/{}.json".format(user["rest_id"])
|
|
|
|
|
return self._pagination(endpoint)
|
|
|
|
|
|
|
|
|
|
def search(self, query):
|
|
|
|
|
endpoint = "2/search/adaptive.json"
|
|
|
|
|
params = self.params.copy()
|
2020-06-07 03:10:09 +02:00
|
|
|
|
params["q"] = text.unquote(query)
|
2020-06-03 20:51:29 +02:00
|
|
|
|
return self._pagination(
|
|
|
|
|
endpoint, params, "sq-I-t-", "sq-cursor-bottom")
|
|
|
|
|
|
|
|
|
|
def bookmarks(self):
|
|
|
|
|
endpoint = "2/timeline/bookmark.json"
|
|
|
|
|
return self._pagination(endpoint)
|
|
|
|
|
|
|
|
|
|
@memcache()
|
|
|
|
|
def user_by_screen_name(self, screen_name):
|
|
|
|
|
endpoint = "graphql/-xfUfZsnR_zqjFd-IfrN5A/UserByScreenName"
|
|
|
|
|
params = {
|
|
|
|
|
"variables": '{"screen_name":"' + screen_name + '"'
|
|
|
|
|
',"withHighlightedLabel":true}'
|
2020-03-05 22:55:26 +01:00
|
|
|
|
}
|
2020-06-03 20:51:29 +02:00
|
|
|
|
return self._call(endpoint, params)["data"]["user"]
|
|
|
|
|
|
|
|
|
|
def _call(self, endpoint, params):
|
|
|
|
|
url = "https://api.twitter.com/" + endpoint
|
|
|
|
|
response = self.extractor.request(
|
|
|
|
|
url, params=params, headers=self.headers, fatal=None)
|
|
|
|
|
if response.status_code < 400:
|
|
|
|
|
return response.json()
|
|
|
|
|
if response.status_code == 429:
|
|
|
|
|
self.extractor.wait(until=response.headers["x-rate-limit-reset"])
|
|
|
|
|
return self._call(endpoint, params)
|
|
|
|
|
raise exception.StopExtraction(
|
|
|
|
|
"%s %s (%s)", response.status_code, response.reason, response.text)
|
|
|
|
|
|
|
|
|
|
def _pagination(self, endpoint, params=None,
|
|
|
|
|
entry_tweet="tweet-", entry_cursor="cursor-bottom-"):
|
|
|
|
|
if params is None:
|
|
|
|
|
params = self.params.copy()
|
2020-03-05 22:55:26 +01:00
|
|
|
|
|
|
|
|
|
while True:
|
2020-06-07 03:10:09 +02:00
|
|
|
|
cursor = tweet = None
|
2020-06-03 20:51:29 +02:00
|
|
|
|
data = self._call(endpoint, params)
|
2020-06-07 03:10:09 +02:00
|
|
|
|
|
|
|
|
|
instr = data["timeline"]["instructions"]
|
|
|
|
|
if not instr:
|
|
|
|
|
return
|
2020-03-05 22:55:26 +01:00
|
|
|
|
tweets = data["globalObjects"]["tweets"]
|
2020-06-03 20:51:29 +02:00
|
|
|
|
users = data["globalObjects"]["users"]
|
|
|
|
|
|
2020-06-07 03:10:09 +02:00
|
|
|
|
for entry in instr[0]["addEntries"]["entries"]:
|
2020-06-03 20:51:29 +02:00
|
|
|
|
|
|
|
|
|
if entry["entryId"].startswith(entry_tweet):
|
|
|
|
|
tid = entry["content"]["item"]["content"]["tweet"]["id"]
|
2020-06-04 14:51:25 +02:00
|
|
|
|
if tid not in tweets:
|
|
|
|
|
self.extractor.log.debug(
|
|
|
|
|
"Skipping unavailable Tweet %s", tid)
|
|
|
|
|
continue
|
2020-06-03 20:51:29 +02:00
|
|
|
|
tweet = tweets[tid]
|
|
|
|
|
tweet["user"] = users[tweet["user_id_str"]]
|
|
|
|
|
|
|
|
|
|
if "quoted_status_id_str" in tweet:
|
2020-06-06 23:51:54 +02:00
|
|
|
|
quoted = tweets.get(tweet["quoted_status_id_str"])
|
|
|
|
|
if quoted:
|
|
|
|
|
tweet["full_text_quoted"] = quoted["full_text"]
|
|
|
|
|
if "extended_entities" in quoted:
|
|
|
|
|
tweet["extended_entities"] = \
|
|
|
|
|
quoted["extended_entities"]
|
2020-06-03 20:51:29 +02:00
|
|
|
|
elif "retweeted_status_id_str" in tweet:
|
2020-06-06 23:51:54 +02:00
|
|
|
|
retweet = tweets.get(tweet["retweeted_status_id_str"])
|
|
|
|
|
if retweet:
|
|
|
|
|
tweet["author"] = users[retweet["user_id_str"]]
|
2020-06-03 20:51:29 +02:00
|
|
|
|
|
|
|
|
|
yield tweet
|
|
|
|
|
|
|
|
|
|
elif entry["entryId"].startswith(entry_cursor):
|
2020-06-07 03:10:09 +02:00
|
|
|
|
cursor = entry["content"]["operation"]["cursor"]
|
|
|
|
|
if not cursor.get("stopOnEmptyResponse"):
|
|
|
|
|
# keep going even if there are no tweets
|
|
|
|
|
tweet = True
|
|
|
|
|
cursor = cursor["value"]
|
|
|
|
|
|
|
|
|
|
if "replaceEntry" in instr[-1] :
|
|
|
|
|
cursor = (instr[-1]["replaceEntry"]["entry"]
|
|
|
|
|
["content"]["operation"]["cursor"]["value"])
|
2020-06-03 20:51:29 +02:00
|
|
|
|
|
2020-06-07 03:10:09 +02:00
|
|
|
|
if not cursor or not tweet:
|
2020-03-05 22:55:26 +01:00
|
|
|
|
return
|
2020-06-03 20:51:29 +02:00
|
|
|
|
params["cursor"] = cursor
|
2020-03-05 22:55:26 +01:00
|
|
|
|
|
|
|
|
|
|
2020-06-03 20:51:29 +02:00
|
|
|
|
@cache(maxage=3600)
|
2020-01-04 23:46:29 +01:00
|
|
|
|
def _guest_token(extr, headers):
|
|
|
|
|
return extr.request(
|
|
|
|
|
"https://api.twitter.com/1.1/guest/activate.json",
|
|
|
|
|
method="POST", headers=headers,
|
|
|
|
|
).json().get("guest_token")
|