2016-10-06 19:12:07 +02:00
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
2021-01-11 22:12:40 +01:00
|
|
|
|
# Copyright 2016-2021 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
|
2020-10-15 00:43:26 +02:00
|
|
|
|
from .. import text, util, exception
|
2020-06-10 20:58:42 +02:00
|
|
|
|
from ..cache import cache
|
2020-11-13 06:47:45 +01:00
|
|
|
|
import json
|
2017-02-01 00:53:19 +01:00
|
|
|
|
|
2020-07-13 23:48:42 +02:00
|
|
|
|
BASE_PATTERN = (
|
|
|
|
|
r"(?:https?://)?(?:www\.|mobile\.)?"
|
|
|
|
|
r"(?:twitter\.com|nitter\.net)"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
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"
|
2021-01-25 14:52:22 +01:00
|
|
|
|
cookienames = ("auth_token",)
|
2018-08-17 20:04:11 +02:00
|
|
|
|
root = "https://twitter.com"
|
|
|
|
|
|
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-06-24 21:13:16 +02:00
|
|
|
|
self.quoted = self.config("quoted", True)
|
2020-02-14 01:03:42 +01:00
|
|
|
|
self.videos = self.config("videos", True)
|
2020-10-22 21:33:53 +02:00
|
|
|
|
self.cards = self.config("cards", False)
|
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-07-11 00:41:50 +02:00
|
|
|
|
if not self.retweets and "retweeted_status_id_str" in tweet:
|
|
|
|
|
self.log.debug("Skipping %s (retweet)", tweet["id_str"])
|
|
|
|
|
continue
|
|
|
|
|
if not self.replies and "in_reply_to_user_id_str" in tweet:
|
|
|
|
|
self.log.debug("Skipping %s (reply)", tweet["id_str"])
|
|
|
|
|
continue
|
|
|
|
|
if not self.quoted and "quoted" in tweet:
|
|
|
|
|
self.log.debug("Skipping %s (quoted tweet)", tweet["id_str"])
|
2020-06-03 20:51:29 +02:00
|
|
|
|
continue
|
|
|
|
|
|
2020-10-22 21:33:53 +02:00
|
|
|
|
files = []
|
|
|
|
|
if "extended_entities" in tweet:
|
|
|
|
|
self._extract_media(tweet, files)
|
|
|
|
|
if "card" in tweet and self.cards:
|
|
|
|
|
self._extract_card(tweet, files)
|
2020-06-04 01:22:34 +02:00
|
|
|
|
if self.twitpic:
|
2020-10-22 21:33:53 +02:00
|
|
|
|
self._extract_twitpic(tweet, files)
|
|
|
|
|
if not files:
|
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)
|
|
|
|
|
yield Message.Directory, tdata
|
2020-10-22 21:33:53 +02:00
|
|
|
|
for tdata["num"], file in enumerate(files, 1):
|
|
|
|
|
file.update(tdata)
|
|
|
|
|
url = file.pop("url")
|
|
|
|
|
if "extension" not in file:
|
|
|
|
|
text.nameext_from_url(url, file)
|
|
|
|
|
yield Message.Url, url, file
|
|
|
|
|
|
|
|
|
|
def _extract_media(self, tweet, files):
|
|
|
|
|
for media in tweet["extended_entities"]["media"]:
|
2020-11-05 22:53:29 +01:00
|
|
|
|
width = media["original_info"].get("width", 0)
|
|
|
|
|
height = media["original_info"].get("height", 0)
|
2020-10-22 21:33:53 +02:00
|
|
|
|
|
|
|
|
|
if "video_info" in media:
|
|
|
|
|
if self.videos == "ytdl":
|
|
|
|
|
files.append({
|
|
|
|
|
"url": "ytdl:{}/i/web/status/{}".format(
|
|
|
|
|
self.root, tweet["id_str"]),
|
|
|
|
|
"width" : width,
|
|
|
|
|
"height" : height,
|
|
|
|
|
"extension": None,
|
|
|
|
|
})
|
|
|
|
|
elif self.videos:
|
|
|
|
|
video_info = media["video_info"]
|
|
|
|
|
variant = max(
|
|
|
|
|
video_info["variants"],
|
|
|
|
|
key=lambda v: v.get("bitrate", 0),
|
|
|
|
|
)
|
|
|
|
|
files.append({
|
|
|
|
|
"url" : variant["url"],
|
|
|
|
|
"width" : width,
|
|
|
|
|
"height" : height,
|
|
|
|
|
"bitrate" : variant.get("bitrate", 0),
|
|
|
|
|
"duration": video_info.get(
|
|
|
|
|
"duration_millis", 0) / 1000,
|
|
|
|
|
})
|
|
|
|
|
elif "media_url_https" in media:
|
|
|
|
|
url = media["media_url_https"]
|
2020-12-01 11:53:51 +01:00
|
|
|
|
base, _, fmt = url.rpartition(".")
|
|
|
|
|
base += "?format=" + fmt + "&name="
|
2020-10-22 21:33:53 +02:00
|
|
|
|
files.append(text.nameext_from_url(url, {
|
2020-12-01 11:53:51 +01:00
|
|
|
|
"url" : base + "orig",
|
2020-10-22 21:33:53 +02:00
|
|
|
|
"width" : width,
|
|
|
|
|
"height" : height,
|
2021-04-02 02:49:53 +02:00
|
|
|
|
"_fallback": self._image_fallback(base, url + ":"),
|
2020-10-22 21:33:53 +02:00
|
|
|
|
}))
|
|
|
|
|
else:
|
|
|
|
|
files.append({"url": media["media_url"]})
|
|
|
|
|
|
2020-12-01 11:53:51 +01:00
|
|
|
|
@staticmethod
|
2021-04-02 02:49:53 +02:00
|
|
|
|
def _image_fallback(new, old):
|
|
|
|
|
yield old + "orig"
|
2020-12-01 11:53:51 +01:00
|
|
|
|
|
|
|
|
|
for size in ("large", "medium", "small"):
|
2021-04-02 02:49:53 +02:00
|
|
|
|
yield new + size
|
|
|
|
|
yield old + size
|
2020-12-01 11:53:51 +01:00
|
|
|
|
|
2020-10-22 21:33:53 +02:00
|
|
|
|
def _extract_card(self, tweet, files):
|
|
|
|
|
card = tweet["card"]
|
|
|
|
|
if card["name"] in ("summary", "summary_large_image"):
|
|
|
|
|
bvals = card["binding_values"]
|
|
|
|
|
for prefix in ("photo_image_full_size_",
|
|
|
|
|
"summary_photo_image_",
|
|
|
|
|
"thumbnail_image_"):
|
|
|
|
|
for size in ("original", "x_large", "large", "small"):
|
|
|
|
|
key = prefix + size
|
|
|
|
|
if key in bvals:
|
|
|
|
|
files.append(bvals[key]["image_value"])
|
|
|
|
|
return
|
2021-04-01 14:26:08 +02:00
|
|
|
|
elif self.videos:
|
2020-10-22 21:33:53 +02:00
|
|
|
|
url = "ytdl:{}/i/web/status/{}".format(self.root, tweet["id_str"])
|
|
|
|
|
files.append({"url": url})
|
|
|
|
|
|
|
|
|
|
def _extract_twitpic(self, tweet, files):
|
2020-06-04 01:22:34 +02:00
|
|
|
|
for url in tweet["entities"].get("urls", ()):
|
|
|
|
|
url = url["expanded_url"]
|
2020-09-21 22:21:16 +02:00
|
|
|
|
if "//twitpic.com/" in url and "/photos/" not in url:
|
2020-06-04 01:22:34 +02:00
|
|
|
|
response = self.request(url, fatal=False)
|
|
|
|
|
if response.status_code >= 400:
|
|
|
|
|
continue
|
|
|
|
|
url = text.extract(
|
|
|
|
|
response.text, 'name="twitter:image" value="', '"')[0]
|
2020-09-21 22:21:16 +02:00
|
|
|
|
if url:
|
2020-10-22 21:33:53 +02:00
|
|
|
|
files.append({"url": url})
|
2020-06-04 01:22:34 +02:00
|
|
|
|
|
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 "author" in tweet:
|
|
|
|
|
tdata["author"] = self._transform_user(tweet["author"])
|
2020-06-18 00:12:36 +02:00
|
|
|
|
else:
|
|
|
|
|
tdata["author"] = tdata["user"]
|
2020-06-06 23:51:54 +02:00
|
|
|
|
|
|
|
|
|
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]
|
|
|
|
|
|
2021-03-15 22:55:24 +01:00
|
|
|
|
def _users_result(self, users):
|
2021-03-20 01:31:12 +01:00
|
|
|
|
userfmt = self.config("users")
|
|
|
|
|
if not userfmt or userfmt == "timeline":
|
|
|
|
|
cls = TwitterTimelineExtractor
|
|
|
|
|
fmt = (self.root + "/i/user/{rest_id}").format_map
|
|
|
|
|
elif userfmt == "media":
|
2021-03-15 22:55:24 +01:00
|
|
|
|
cls = TwitterMediaExtractor
|
2021-03-20 01:31:12 +01:00
|
|
|
|
fmt = (self.root + "/id:{rest_id}/media").format_map
|
2021-03-15 22:55:24 +01:00
|
|
|
|
else:
|
2021-03-20 01:31:12 +01:00
|
|
|
|
cls = None
|
|
|
|
|
fmt = userfmt.format_map
|
2021-03-15 22:55:24 +01:00
|
|
|
|
|
|
|
|
|
for user in users:
|
|
|
|
|
user["_extractor"] = cls
|
2021-03-20 01:31:12 +01:00
|
|
|
|
yield Message.Queue, fmt(user), user
|
2021-03-15 22:55:24 +01:00
|
|
|
|
|
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):
|
2021-01-25 14:52:22 +01:00
|
|
|
|
if not self._check_cookies(self.cookienames):
|
|
|
|
|
username, password = self._get_auth_info()
|
|
|
|
|
if username:
|
|
|
|
|
self._update_cookies(self._login_impl(username, password))
|
2019-04-07 23:06:57 +02:00
|
|
|
|
|
|
|
|
|
@cache(maxage=360*24*3600, keyarg=1)
|
|
|
|
|
def _login_impl(self, username, password):
|
|
|
|
|
self.log.info("Logging in as %s", username)
|
|
|
|
|
|
2021-01-11 22:12:40 +01:00
|
|
|
|
token = util.generate_token()
|
2020-12-28 15:54:47 +01:00
|
|
|
|
self.session.cookies.clear()
|
|
|
|
|
self.request(self.root + "/login")
|
2020-06-04 00:07:12 +02:00
|
|
|
|
|
2020-12-28 15:54:47 +01:00
|
|
|
|
url = self.root + "/sessions"
|
|
|
|
|
cookies = {
|
|
|
|
|
"_mb_tk": token,
|
|
|
|
|
}
|
2019-04-07 23:06:57 +02:00
|
|
|
|
data = {
|
2020-12-28 15:54:47 +01:00
|
|
|
|
"redirect_after_login" : "/",
|
|
|
|
|
"remember_me" : "1",
|
2020-06-04 00:07:12 +02:00
|
|
|
|
"authenticity_token" : token,
|
2020-12-28 15:54:47 +01:00
|
|
|
|
"wfa" : "1",
|
|
|
|
|
"ui_metrics" : "{}",
|
2019-04-07 23:06:57 +02:00
|
|
|
|
"session[username_or_email]": username,
|
|
|
|
|
"session[password]" : password,
|
|
|
|
|
}
|
2020-12-28 15:54:47 +01:00
|
|
|
|
response = self.request(
|
|
|
|
|
url, method="POST", cookies=cookies, data=data)
|
|
|
|
|
|
2021-03-26 21:52:55 +01:00
|
|
|
|
if "/account/login_verification" in response.url:
|
|
|
|
|
raise exception.AuthenticationError(
|
|
|
|
|
"Login with two-factor authentication is not supported")
|
|
|
|
|
|
2020-06-04 00:07:12 +02:00
|
|
|
|
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 "/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"
|
2021-01-20 00:33:57 +01:00
|
|
|
|
pattern = (BASE_PATTERN + r"/(?!search)(?:([^/?#]+)/?(?:$|[?#])"
|
|
|
|
|
r"|i(?:/user/|ntent/user\?user_id=)(\d+))")
|
2019-09-01 17:37:48 +02:00
|
|
|
|
test = (
|
|
|
|
|
("https://twitter.com/supernaturepics", {
|
|
|
|
|
"range": "1-40",
|
2020-12-01 11:53:51 +01:00
|
|
|
|
"url": "c570ac1aae38ed1463be726cc46f31cac3d82a40",
|
2019-09-01 17:37:48 +02:00
|
|
|
|
}),
|
|
|
|
|
("https://mobile.twitter.com/supernaturepics?p=i"),
|
2020-09-08 22:56:52 +02:00
|
|
|
|
("https://www.twitter.com/id:2976459548"),
|
2021-01-20 00:33:57 +01:00
|
|
|
|
("https://twitter.com/i/user/2976459548"),
|
2020-09-08 23:17:50 +02:00
|
|
|
|
("https://twitter.com/intent/user?user_id=2976459548"),
|
2019-09-01 17:37:48 +02:00
|
|
|
|
)
|
2018-08-19 20:36:33 +02:00
|
|
|
|
|
2020-09-08 23:17:50 +02:00
|
|
|
|
def __init__(self, match):
|
|
|
|
|
TwitterExtractor.__init__(self, match)
|
2021-01-20 00:33:57 +01:00
|
|
|
|
user_id = match.group(2)
|
|
|
|
|
if user_id:
|
|
|
|
|
self.user = "id:" + user_id
|
2020-09-08 23:17:50 +02:00
|
|
|
|
|
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"
|
2020-10-22 23:12:59 +02:00
|
|
|
|
pattern = BASE_PATTERN + r"/(?!search)([^/?#]+)/media(?!\w)"
|
2019-09-01 17:37:48 +02:00
|
|
|
|
test = (
|
|
|
|
|
("https://twitter.com/supernaturepics/media", {
|
|
|
|
|
"range": "1-40",
|
2020-12-01 11:53:51 +01:00
|
|
|
|
"url": "c570ac1aae38ed1463be726cc46f31cac3d82a40",
|
2019-09-01 17:37:48 +02:00
|
|
|
|
}),
|
|
|
|
|
("https://mobile.twitter.com/supernaturepics/media#t"),
|
2020-09-08 22:56:52 +02:00
|
|
|
|
("https://www.twitter.com/id:2976459548/media"),
|
2019-09-01 17:37:48 +02:00
|
|
|
|
)
|
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
|
|
|
|
|
2020-06-16 14:27:22 +02:00
|
|
|
|
class TwitterLikesExtractor(TwitterExtractor):
|
|
|
|
|
"""Extractor for liked tweets"""
|
|
|
|
|
subcategory = "likes"
|
2020-10-22 23:12:59 +02:00
|
|
|
|
pattern = BASE_PATTERN + r"/(?!search)([^/?#]+)/likes(?!\w)"
|
2020-06-16 14:27:22 +02:00
|
|
|
|
test = ("https://twitter.com/supernaturepics/likes",)
|
|
|
|
|
|
2021-04-02 02:52:01 +02:00
|
|
|
|
def metadata(self):
|
|
|
|
|
return {"user_likes": self.user}
|
|
|
|
|
|
2020-06-16 14:27:22 +02:00
|
|
|
|
def tweets(self):
|
|
|
|
|
return TwitterAPI(self).timeline_favorites(self.user)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TwitterBookmarkExtractor(TwitterExtractor):
|
|
|
|
|
"""Extractor for bookmarked tweets"""
|
|
|
|
|
subcategory = "bookmark"
|
2020-07-13 23:48:42 +02:00
|
|
|
|
pattern = BASE_PATTERN + r"/i/bookmarks()"
|
2020-06-16 14:27:22 +02:00
|
|
|
|
test = ("https://twitter.com/i/bookmarks",)
|
|
|
|
|
|
|
|
|
|
def tweets(self):
|
|
|
|
|
return TwitterAPI(self).timeline_bookmark()
|
|
|
|
|
|
|
|
|
|
|
2020-11-05 22:55:38 +01:00
|
|
|
|
class TwitterListExtractor(TwitterExtractor):
|
|
|
|
|
"""Extractor for Twitter lists"""
|
|
|
|
|
subcategory = "list"
|
2020-11-13 06:47:45 +01:00
|
|
|
|
pattern = BASE_PATTERN + r"/i/lists/(\d+)/?$"
|
2020-11-05 22:55:38 +01:00
|
|
|
|
test = ("https://twitter.com/i/lists/784214683683127296", {
|
|
|
|
|
"range": "1-40",
|
|
|
|
|
"count": 40,
|
|
|
|
|
"archive": False,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
def tweets(self):
|
|
|
|
|
return TwitterAPI(self).timeline_list(self.user)
|
|
|
|
|
|
|
|
|
|
|
2020-11-13 06:47:45 +01:00
|
|
|
|
class TwitterListMembersExtractor(TwitterExtractor):
|
|
|
|
|
"""Extractor for members of a Twitter list"""
|
|
|
|
|
subcategory = "list-members"
|
|
|
|
|
pattern = BASE_PATTERN + r"/i/lists/(\d+)/members"
|
|
|
|
|
test = ("https://twitter.com/i/lists/784214683683127296/members",)
|
|
|
|
|
|
|
|
|
|
def items(self):
|
|
|
|
|
self.login()
|
2021-03-15 22:55:24 +01:00
|
|
|
|
return self._users_result(TwitterAPI(self).list_members(self.user))
|
2020-11-13 06:47:45 +01:00
|
|
|
|
|
|
|
|
|
|
2021-02-22 18:18:33 +01:00
|
|
|
|
class TwitterFollowingExtractor(TwitterExtractor):
|
|
|
|
|
"""Extractor for followed users"""
|
|
|
|
|
subcategory = "following"
|
|
|
|
|
pattern = BASE_PATTERN + r"/(?!search)([^/?#]+)/following(?!\w)"
|
|
|
|
|
test = (
|
|
|
|
|
("https://twitter.com/supernaturepics/following"),
|
|
|
|
|
("https://www.twitter.com/id:2976459548/following"),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def items(self):
|
|
|
|
|
self.login()
|
2021-03-15 22:55:24 +01:00
|
|
|
|
return self._users_result(TwitterAPI(self).user_following(self.user))
|
2021-02-22 18:18:33 +01: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}")
|
2020-07-13 23:48:42 +02:00
|
|
|
|
pattern = BASE_PATTERN + r"/search/?\?(?:[^&#]+&)*q=([^&#]+)"
|
2019-10-17 18:34:07 +02:00
|
|
|
|
test = ("https://twitter.com/search?q=nature", {
|
|
|
|
|
"range": "1-40",
|
|
|
|
|
"count": 40,
|
2020-10-03 19:24:19 +02:00
|
|
|
|
"archive": False,
|
2019-10-17 18:34:07 +02:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
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-21 15:43:27 +02:00
|
|
|
|
return TwitterAPI(self).search(text.unquote(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"
|
2020-10-22 23:12:59 +02:00
|
|
|
|
pattern = BASE_PATTERN + 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", {
|
2020-12-01 11:53:51 +01:00
|
|
|
|
"url": "88a40f7d25529c2501c46f2218f9e0de9aa634b4",
|
2019-05-09 10:17:55 +02:00
|
|
|
|
"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", {
|
2020-12-01 11:53:51 +01:00
|
|
|
|
"url": "3a2a43dc5fb79dd5432c701d8e55e87c4e551f47",
|
2019-04-21 15:41:22 +02:00
|
|
|
|
}),
|
|
|
|
|
# video
|
|
|
|
|
("https://twitter.com/perrypumas/status/1065692031626829824", {
|
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
|
|
|
|
}),
|
2020-06-19 18:12:57 +02:00
|
|
|
|
# Reply to deleted tweet (#403, #838)
|
|
|
|
|
("https://twitter.com/i/web/status/1170041925560258560", {
|
2020-12-01 11:53:51 +01:00
|
|
|
|
"pattern": r"https://pbs.twimg.com/media/EDzS7VrU0AAFL4_",
|
2019-09-01 17:37:48 +02:00
|
|
|
|
}),
|
2020-04-29 23:11:24 +02:00
|
|
|
|
# 'replies' option (#705)
|
2020-06-19 18:12:57 +02:00
|
|
|
|
("https://twitter.com/i/web/status/1170041925560258560", {
|
2020-04-29 23:11:24 +02:00
|
|
|
|
"options": (("replies", False),),
|
|
|
|
|
"count": 0,
|
|
|
|
|
}),
|
2020-06-24 21:13:16 +02:00
|
|
|
|
# quoted tweet (#526, #854)
|
|
|
|
|
("https://twitter.com/StobiesGalaxy/status/1270755918330896395", {
|
2020-12-01 11:53:51 +01:00
|
|
|
|
"pattern": r"https://pbs\.twimg\.com/media/Ea[KG].+=jpg",
|
2020-06-24 21:13:16 +02:00
|
|
|
|
"count": 8,
|
|
|
|
|
}),
|
|
|
|
|
# "quoted" option (#854)
|
|
|
|
|
("https://twitter.com/StobiesGalaxy/status/1270755918330896395", {
|
|
|
|
|
"options": (("quoted", False),),
|
2020-12-01 11:53:51 +01:00
|
|
|
|
"pattern": r"https://pbs\.twimg\.com/media/EaK.+=jpg",
|
2020-06-24 21:13:16 +02:00
|
|
|
|
"count": 4,
|
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,
|
|
|
|
|
}),
|
2020-10-22 21:33:53 +02:00
|
|
|
|
# Nitter tweet (#890)
|
2020-07-13 23:48:42 +02:00
|
|
|
|
("https://nitter.net/ed1conf/status/1163841619336007680", {
|
2020-12-01 11:53:51 +01:00
|
|
|
|
"url": "4a9ea898b14d3c112f98562d0df75c9785e239d9",
|
2020-07-13 23:48:42 +02:00
|
|
|
|
"content": "f29501e44d88437fe460f5c927b7543fda0f6e34",
|
|
|
|
|
}),
|
2020-10-22 21:33:53 +02:00
|
|
|
|
# Twitter card (#1005)
|
|
|
|
|
("https://twitter.com/billboard/status/1306599586602135555", {
|
|
|
|
|
"options": (("cards", True),),
|
2020-11-05 22:53:29 +01:00
|
|
|
|
"pattern": r"https://pbs.twimg.com/card_img/\d+/",
|
2020-10-22 21:33:53 +02:00
|
|
|
|
}),
|
2020-09-28 23:03:35 +02:00
|
|
|
|
# original retweets (#1026)
|
|
|
|
|
("https://twitter.com/jessica_3978/status/1296304589591810048", {
|
|
|
|
|
"options": (("retweets", "original"),),
|
|
|
|
|
"count": 2,
|
|
|
|
|
"keyword": {
|
|
|
|
|
"tweet_id": 1296296016002547713,
|
|
|
|
|
"date" : "dt:2020-08-20 04:00:28",
|
|
|
|
|
},
|
|
|
|
|
}),
|
2021-02-26 13:50:46 +01:00
|
|
|
|
# all Tweets from a conversation (#1319)
|
|
|
|
|
("https://twitter.com/BlankArts_/status/1323314488611872769", {
|
|
|
|
|
"options": (("conversations", True),),
|
|
|
|
|
"count": ">= 50",
|
|
|
|
|
}),
|
2021-05-14 22:46:06 +02:00
|
|
|
|
# retweet with missing media entities (#1555)
|
|
|
|
|
("https://twitter.com/morino_ya/status/1392763691599237121", {
|
|
|
|
|
"count": 4,
|
|
|
|
|
}),
|
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):
|
2021-02-26 13:50:46 +01:00
|
|
|
|
if self.config("conversations", False):
|
|
|
|
|
return TwitterAPI(self).conversation(self.tweet_id)
|
2020-06-03 20:51:29 +02:00
|
|
|
|
return TwitterAPI(self).tweet(self.tweet_id)
|
2020-01-04 23:46:29 +01:00
|
|
|
|
|
|
|
|
|
|
2021-04-02 02:45:23 +02:00
|
|
|
|
class TwitterImageExtractor(Extractor):
|
|
|
|
|
category = "twitter"
|
|
|
|
|
subcategory = "image"
|
|
|
|
|
pattern = r"https?://pbs\.twimg\.com/media/([\w-]+)(?:\?format=|\.)(\w+)"
|
|
|
|
|
test = (
|
|
|
|
|
("https://pbs.twimg.com/media/EqcpviCVoAAG-QG?format=jpg%name=orig"),
|
|
|
|
|
("https://pbs.twimg.com/media/EqcpviCVoAAG-QG.jpg:orig"),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def __init__(self, match):
|
|
|
|
|
Extractor.__init__(self, match)
|
|
|
|
|
self.id, self.fmt = match.groups()
|
|
|
|
|
|
|
|
|
|
def items(self):
|
|
|
|
|
base = "https://pbs.twimg.com/media/" + self.id
|
2021-04-02 02:49:53 +02:00
|
|
|
|
new = base + "?format=" + self.fmt + "&name="
|
|
|
|
|
old = base + "." + self.fmt + ":"
|
2021-04-02 02:45:23 +02:00
|
|
|
|
|
|
|
|
|
data = {
|
|
|
|
|
"filename": self.id,
|
|
|
|
|
"extension": self.fmt,
|
2021-04-02 02:49:53 +02:00
|
|
|
|
"_fallback": TwitterExtractor._image_fallback(new, old),
|
2021-04-02 02:45:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
yield Message.Directory, data
|
2021-04-02 02:49:53 +02:00
|
|
|
|
yield Message.Url, new + "orig", data
|
2021-04-02 02:45:23 +02:00
|
|
|
|
|
|
|
|
|
|
2020-06-03 20:51:29 +02:00
|
|
|
|
class TwitterAPI():
|
|
|
|
|
|
|
|
|
|
def __init__(self, extractor):
|
|
|
|
|
self.extractor = extractor
|
2020-12-28 22:05:48 +01:00
|
|
|
|
|
|
|
|
|
self.root = "https://twitter.com/i/api"
|
2020-06-03 20:51:29 +02:00
|
|
|
|
self.headers = {
|
|
|
|
|
"authorization": "Bearer AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejR"
|
|
|
|
|
"COuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu"
|
|
|
|
|
"4FA33AGWWjCpTnA",
|
|
|
|
|
"x-guest-token": None,
|
2020-12-28 22:05:48 +01:00
|
|
|
|
"x-twitter-auth-type": None,
|
2020-06-03 20:51:29 +02:00
|
|
|
|
"x-twitter-client-language": "en",
|
|
|
|
|
"x-twitter-active-user": "yes",
|
|
|
|
|
"x-csrf-token": None,
|
|
|
|
|
"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_ext_alt_text": "true",
|
2020-12-28 22:05:48 +01:00
|
|
|
|
"include_quote_count": "true",
|
2020-03-05 22:55:26 +01:00
|
|
|
|
"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",
|
2020-03-05 22:55:26 +01:00
|
|
|
|
"count": "100",
|
|
|
|
|
"cursor": None,
|
2020-12-28 22:05:48 +01:00
|
|
|
|
"ext": "mediaStats,highlightedLabel",
|
2020-03-05 22:55:26 +01:00
|
|
|
|
}
|
2020-06-03 20:51:29 +02:00
|
|
|
|
|
|
|
|
|
cookies = self.extractor.session.cookies
|
2020-12-11 13:40:57 +01:00
|
|
|
|
cookiedomain = ".twitter.com"
|
2020-06-03 20:51:29 +02:00
|
|
|
|
|
|
|
|
|
# CSRF
|
2020-12-11 13:40:57 +01:00
|
|
|
|
csrf_token = cookies.get("ct0", domain=cookiedomain)
|
|
|
|
|
if not csrf_token:
|
2021-01-11 22:12:40 +01:00
|
|
|
|
csrf_token = util.generate_token()
|
2020-12-11 13:40:57 +01:00
|
|
|
|
cookies.set("ct0", csrf_token, domain=cookiedomain)
|
|
|
|
|
self.headers["x-csrf-token"] = csrf_token
|
2020-06-03 20:51:29 +02:00
|
|
|
|
|
2020-12-11 13:40:57 +01:00
|
|
|
|
if cookies.get("auth_token", domain=cookiedomain):
|
2020-11-16 11:26:37 +01:00
|
|
|
|
# logged in
|
2020-06-03 20:51:29 +02:00
|
|
|
|
self.headers["x-twitter-auth-type"] = "OAuth2Session"
|
|
|
|
|
else:
|
2020-11-16 11:26:37 +01:00
|
|
|
|
# guest
|
2020-06-18 00:28:38 +02:00
|
|
|
|
guest_token = self._guest_token()
|
2020-12-11 13:40:57 +01:00
|
|
|
|
cookies.set("gt", guest_token, domain=cookiedomain)
|
2020-06-03 20:51:29 +02:00
|
|
|
|
self.headers["x-guest-token"] = guest_token
|
|
|
|
|
|
|
|
|
|
def tweet(self, tweet_id):
|
2020-12-28 22:05:48 +01:00
|
|
|
|
endpoint = "/2/timeline/conversation/{}.json".format(tweet_id)
|
2020-06-24 21:08:04 +02:00
|
|
|
|
tweets = []
|
2020-06-03 20:51:29 +02:00
|
|
|
|
for tweet in self._pagination(endpoint):
|
2020-09-28 23:03:35 +02:00
|
|
|
|
if tweet["id_str"] == tweet_id or \
|
|
|
|
|
tweet.get("_retweet_id_str") == tweet_id:
|
2020-06-24 21:08:04 +02:00
|
|
|
|
tweets.append(tweet)
|
|
|
|
|
if "quoted_status_id_str" in tweet:
|
|
|
|
|
tweet_id = tweet["quoted_status_id_str"]
|
|
|
|
|
else:
|
|
|
|
|
break
|
|
|
|
|
return tweets
|
2020-06-03 20:51:29 +02:00
|
|
|
|
|
2021-02-26 13:50:46 +01:00
|
|
|
|
def conversation(self, conversation_id):
|
|
|
|
|
endpoint = "/2/timeline/conversation/{}.json".format(conversation_id)
|
|
|
|
|
return self._pagination(endpoint)
|
|
|
|
|
|
2020-06-03 20:51:29 +02:00
|
|
|
|
def timeline_profile(self, screen_name):
|
2020-09-08 22:56:52 +02:00
|
|
|
|
user_id = self._user_id_by_screen_name(screen_name)
|
2020-12-28 22:05:48 +01:00
|
|
|
|
endpoint = "/2/timeline/profile/{}.json".format(user_id)
|
|
|
|
|
params = self.params.copy()
|
|
|
|
|
params["include_tweet_replies"] = "false"
|
|
|
|
|
return self._pagination(endpoint, params)
|
2020-06-03 20:51:29 +02:00
|
|
|
|
|
|
|
|
|
def timeline_media(self, screen_name):
|
2020-09-08 22:56:52 +02:00
|
|
|
|
user_id = self._user_id_by_screen_name(screen_name)
|
2020-12-28 22:05:48 +01:00
|
|
|
|
endpoint = "/2/timeline/media/{}.json".format(user_id)
|
2020-06-03 20:51:29 +02:00
|
|
|
|
return self._pagination(endpoint)
|
|
|
|
|
|
2020-06-16 14:27:22 +02:00
|
|
|
|
def timeline_favorites(self, screen_name):
|
2020-09-08 22:56:52 +02:00
|
|
|
|
user_id = self._user_id_by_screen_name(screen_name)
|
2020-12-28 22:05:48 +01:00
|
|
|
|
endpoint = "/2/timeline/favorites/{}.json".format(user_id)
|
|
|
|
|
params = self.params.copy()
|
|
|
|
|
params["sorted_by_time"] = "true"
|
2020-06-16 14:27:22 +02:00
|
|
|
|
return self._pagination(endpoint)
|
|
|
|
|
|
|
|
|
|
def timeline_bookmark(self):
|
2020-12-28 22:05:48 +01:00
|
|
|
|
endpoint = "/2/timeline/bookmark.json"
|
2020-06-16 14:27:22 +02:00
|
|
|
|
return self._pagination(endpoint)
|
|
|
|
|
|
2020-11-05 22:55:38 +01:00
|
|
|
|
def timeline_list(self, list_id):
|
2020-12-28 22:05:48 +01:00
|
|
|
|
endpoint = "/2/timeline/list.json"
|
2020-11-05 22:55:38 +01:00
|
|
|
|
params = self.params.copy()
|
|
|
|
|
params["list_id"] = list_id
|
|
|
|
|
params["ranking_mode"] = "reverse_chronological"
|
|
|
|
|
return self._pagination(endpoint, params)
|
|
|
|
|
|
2020-06-03 20:51:29 +02:00
|
|
|
|
def search(self, query):
|
2020-12-28 22:05:48 +01:00
|
|
|
|
endpoint = "/2/search/adaptive.json"
|
2020-06-03 20:51:29 +02:00
|
|
|
|
params = self.params.copy()
|
2020-06-21 15:43:27 +02:00
|
|
|
|
params["q"] = query
|
|
|
|
|
params["tweet_search_mode"] = "live"
|
|
|
|
|
params["query_source"] = "typed_query"
|
|
|
|
|
params["pc"] = "1"
|
|
|
|
|
params["spelling_corrections"] = "1"
|
2020-12-28 23:34:46 +01:00
|
|
|
|
return self._pagination(endpoint, params)
|
2020-06-03 20:51:29 +02:00
|
|
|
|
|
2020-11-05 22:55:38 +01:00
|
|
|
|
def list_by_rest_id(self, list_id):
|
2021-02-20 02:09:17 +01:00
|
|
|
|
endpoint = "/graphql/18MAHTcDU-TdJSjWWmoH7w/ListByRestId"
|
2020-11-05 22:55:38 +01:00
|
|
|
|
params = {"variables": '{"listId":"' + list_id + '"'
|
|
|
|
|
',"withUserResult":false}'}
|
|
|
|
|
try:
|
|
|
|
|
return self._call(endpoint, params)["data"]["list"]
|
|
|
|
|
except KeyError:
|
|
|
|
|
raise exception.NotFoundError("list")
|
|
|
|
|
|
2021-02-22 18:18:33 +01:00
|
|
|
|
def list_members(self, list_id):
|
|
|
|
|
endpoint = "/graphql/tA7h9hy4U0Yc9COfIOh3qQ/ListMembers"
|
|
|
|
|
variables = {
|
|
|
|
|
"listId": list_id,
|
|
|
|
|
"count" : 100,
|
|
|
|
|
"withTweetResult": False,
|
|
|
|
|
"withUserResult" : False,
|
|
|
|
|
}
|
|
|
|
|
return self._pagination_graphql(
|
|
|
|
|
endpoint, variables, "list", "members_timeline")
|
|
|
|
|
|
|
|
|
|
def user_following(self, screen_name):
|
|
|
|
|
endpoint = "/graphql/Q_QTiPvoXwsA13eoA7okIQ/Following"
|
|
|
|
|
variables = {
|
|
|
|
|
"userId": self._user_id_by_screen_name(screen_name),
|
|
|
|
|
"count" : 100,
|
|
|
|
|
"withTweetResult": False,
|
|
|
|
|
"withUserResult" : False,
|
|
|
|
|
"withTweetQuoteCount" : False,
|
|
|
|
|
"withHighlightedLabel" : False,
|
|
|
|
|
"includePromotedContent": False,
|
|
|
|
|
}
|
|
|
|
|
return self._pagination_graphql(
|
|
|
|
|
endpoint, variables, "user", "following_timeline")
|
|
|
|
|
|
2020-06-03 20:51:29 +02:00
|
|
|
|
def user_by_screen_name(self, screen_name):
|
2021-02-20 02:09:17 +01:00
|
|
|
|
endpoint = "/graphql/hc-pka9A7gyS3xODIafnrQ/UserByScreenName"
|
2020-11-05 22:53:29 +01:00
|
|
|
|
params = {"variables": '{"screen_name":"' + screen_name + '"'
|
|
|
|
|
',"withHighlightedLabel":true}'}
|
2020-07-14 16:47:25 +02:00
|
|
|
|
try:
|
|
|
|
|
return self._call(endpoint, params)["data"]["user"]
|
|
|
|
|
except KeyError:
|
|
|
|
|
raise exception.NotFoundError("user")
|
2020-06-03 20:51:29 +02:00
|
|
|
|
|
2020-09-08 22:56:52 +02:00
|
|
|
|
def _user_id_by_screen_name(self, screen_name):
|
|
|
|
|
if screen_name.startswith("id:"):
|
|
|
|
|
return screen_name[3:]
|
|
|
|
|
return self.user_by_screen_name(screen_name)["rest_id"]
|
|
|
|
|
|
2020-06-18 00:28:38 +02:00
|
|
|
|
@cache(maxage=3600)
|
|
|
|
|
def _guest_token(self):
|
2020-12-28 22:05:48 +01:00
|
|
|
|
root = "https://api.twitter.com"
|
|
|
|
|
endpoint = "/1.1/guest/activate.json"
|
|
|
|
|
return self._call(endpoint, None, root, "POST")["guest_token"]
|
2020-06-18 00:28:38 +02:00
|
|
|
|
|
2020-12-28 22:05:48 +01:00
|
|
|
|
def _call(self, endpoint, params, root=None, method="GET"):
|
|
|
|
|
if root is None:
|
|
|
|
|
root = self.root
|
2020-07-06 23:13:05 +02:00
|
|
|
|
|
2021-01-19 23:15:57 +01:00
|
|
|
|
while True:
|
|
|
|
|
response = self.extractor.request(
|
|
|
|
|
root + endpoint, method=method, params=params,
|
|
|
|
|
headers=self.headers, fatal=None)
|
|
|
|
|
|
|
|
|
|
# update 'x-csrf-token' header (#1170)
|
|
|
|
|
csrf_token = response.cookies.get("ct0")
|
|
|
|
|
if csrf_token:
|
|
|
|
|
self.headers["x-csrf-token"] = csrf_token
|
|
|
|
|
|
|
|
|
|
if response.status_code < 400:
|
|
|
|
|
return response.json()
|
|
|
|
|
if response.status_code == 429:
|
|
|
|
|
until = response.headers.get("x-rate-limit-reset")
|
|
|
|
|
seconds = None if until else 60
|
|
|
|
|
self.extractor.wait(until=until, seconds=seconds)
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
msg = ", ".join(
|
|
|
|
|
'"' + error["message"] + '"'
|
|
|
|
|
for error in response.json()["errors"]
|
|
|
|
|
)
|
|
|
|
|
except Exception:
|
|
|
|
|
msg = response.text
|
|
|
|
|
raise exception.StopExtraction(
|
|
|
|
|
"%s %s (%s)", response.status_code, response.reason, msg)
|
2020-06-03 20:51:29 +02:00
|
|
|
|
|
2020-12-28 23:34:46 +01:00
|
|
|
|
def _pagination(self, endpoint, params=None):
|
2020-06-03 20:51:29 +02:00
|
|
|
|
if params is None:
|
|
|
|
|
params = self.params.copy()
|
2020-09-28 23:03:35 +02:00
|
|
|
|
original_retweets = (self.extractor.retweets == "original")
|
2020-12-29 16:27:43 +01:00
|
|
|
|
pinned_tweet = True
|
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-12-28 23:34:46 +01:00
|
|
|
|
tweet_ids = []
|
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-12-29 16:27:43 +01:00
|
|
|
|
if pinned_tweet:
|
|
|
|
|
if "pinEntry" in instr[-1]:
|
|
|
|
|
tweet_ids.append(instr[-1]["pinEntry"]["entry"]["content"]
|
|
|
|
|
["item"]["content"]["tweet"]["id"])
|
|
|
|
|
pinned_tweet = False
|
|
|
|
|
|
2020-12-28 23:34:46 +01:00
|
|
|
|
# collect tweet IDs and cursor value
|
2020-06-07 03:10:09 +02:00
|
|
|
|
for entry in instr[0]["addEntries"]["entries"]:
|
2020-12-28 23:34:46 +01:00
|
|
|
|
entry_startswith = entry["entryId"].startswith
|
|
|
|
|
|
|
|
|
|
if entry_startswith(("tweet-", "sq-I-t-")):
|
|
|
|
|
tweet_ids.append(
|
|
|
|
|
entry["content"]["item"]["content"]["tweet"]["id"])
|
2020-06-03 20:51:29 +02:00
|
|
|
|
|
2020-12-28 23:34:46 +01:00
|
|
|
|
elif entry_startswith("homeConversation-"):
|
|
|
|
|
tweet_ids.extend(
|
|
|
|
|
entry["content"]["timelineModule"]["metadata"]
|
|
|
|
|
["conversationMetadata"]["allTweetIds"][::-1])
|
|
|
|
|
|
|
|
|
|
elif entry_startswith(("cursor-bottom-", "sq-cursor-bottom")):
|
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"]
|
|
|
|
|
|
2021-02-26 13:50:46 +01:00
|
|
|
|
elif entry_startswith("conversationThread-"):
|
|
|
|
|
tweet_ids.extend(
|
|
|
|
|
item["entryId"][6:]
|
|
|
|
|
for item in entry["content"]["timelineModule"]["items"]
|
|
|
|
|
if item["entryId"].startswith("tweet-")
|
|
|
|
|
)
|
|
|
|
|
|
2020-12-28 23:34:46 +01:00
|
|
|
|
# process tweets
|
|
|
|
|
for tweet_id in tweet_ids:
|
|
|
|
|
try:
|
|
|
|
|
tweet = tweets[tweet_id]
|
|
|
|
|
except KeyError:
|
|
|
|
|
self.extractor.log.debug("Skipping %s (deleted)", tweet_id)
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
if "retweeted_status_id_str" in tweet:
|
|
|
|
|
retweet = tweets.get(tweet["retweeted_status_id_str"])
|
|
|
|
|
if original_retweets:
|
|
|
|
|
if not retweet:
|
|
|
|
|
continue
|
|
|
|
|
retweet["_retweet_id_str"] = tweet["id_str"]
|
|
|
|
|
tweet = retweet
|
|
|
|
|
elif retweet:
|
|
|
|
|
tweet["author"] = users[retweet["user_id_str"]]
|
2021-05-14 22:46:06 +02:00
|
|
|
|
if "extended_entities" in retweet and \
|
|
|
|
|
"extended_entities" not in tweet:
|
|
|
|
|
tweet["extended_entities"] = \
|
|
|
|
|
retweet["extended_entities"]
|
2020-12-28 23:34:46 +01:00
|
|
|
|
tweet["user"] = users[tweet["user_id_str"]]
|
|
|
|
|
yield tweet
|
|
|
|
|
|
|
|
|
|
if "quoted_status_id_str" in tweet:
|
|
|
|
|
quoted = tweets.get(tweet["quoted_status_id_str"])
|
|
|
|
|
if quoted:
|
|
|
|
|
quoted["author"] = users[quoted["user_id_str"]]
|
|
|
|
|
quoted["user"] = tweet["user"]
|
|
|
|
|
quoted["quoted"] = True
|
|
|
|
|
yield quoted
|
|
|
|
|
|
|
|
|
|
# update cursor value
|
2020-06-07 03:10:09 +02:00
|
|
|
|
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-11-13 06:47:45 +01:00
|
|
|
|
|
2021-02-22 18:18:33 +01:00
|
|
|
|
def _pagination_graphql(self, endpoint, variables, key, timeline):
|
2020-11-13 06:47:45 +01:00
|
|
|
|
while True:
|
|
|
|
|
cursor = entry = stop = None
|
|
|
|
|
params = {"variables": json.dumps(variables)}
|
|
|
|
|
data = self._call(endpoint, params)
|
|
|
|
|
|
|
|
|
|
try:
|
2021-02-22 18:18:33 +01:00
|
|
|
|
instructions = \
|
|
|
|
|
data["data"][key][timeline]["timeline"]["instructions"]
|
2020-11-13 06:47:45 +01:00
|
|
|
|
except KeyError:
|
|
|
|
|
raise exception.AuthorizationError()
|
|
|
|
|
|
|
|
|
|
for instr in instructions:
|
|
|
|
|
if instr["type"] == "TimelineAddEntries":
|
|
|
|
|
for entry in instr["entries"]:
|
|
|
|
|
if entry["entryId"].startswith("user-"):
|
|
|
|
|
yield entry["content"]["itemContent"]["user"]
|
|
|
|
|
elif entry["entryId"].startswith("cursor-bottom-"):
|
|
|
|
|
cursor = entry["content"]["value"]
|
|
|
|
|
elif instr["type"] == "TimelineTerminateTimeline":
|
|
|
|
|
if instr["direction"] == "Bottom":
|
|
|
|
|
stop = True
|
|
|
|
|
|
|
|
|
|
if stop or not cursor or not entry:
|
|
|
|
|
return
|
|
|
|
|
variables["cursor"] = cursor
|