2016-10-06 19:12:07 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2018-01-30 22:49:16 +01:00
|
|
|
# Copyright 2016-2018 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.
|
|
|
|
|
|
|
|
"""Extract images from https://twitter.com/"""
|
|
|
|
|
|
|
|
from .common import Extractor, Message
|
2018-10-05 17:58:15 +02:00
|
|
|
from .. import text
|
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"
|
|
|
|
directory_fmt = ["{category}", "{user}"]
|
2017-09-10 22:20:47 +02:00
|
|
|
filename_fmt = "{tweet_id}_{num}.{extension}"
|
2018-08-18 18:58:10 +02:00
|
|
|
archive_fmt = "{tweet_id}_{retweet_id}_{num}"
|
2018-08-17 20:04:11 +02:00
|
|
|
root = "https://twitter.com"
|
|
|
|
|
2018-08-19 20:36:33 +02:00
|
|
|
def __init__(self, match):
|
2018-08-17 20:04:11 +02:00
|
|
|
Extractor.__init__(self)
|
2018-08-19 20:36:33 +02:00
|
|
|
self.user = match.group(1)
|
|
|
|
self.retweets = self.config("retweets", True)
|
2018-09-30 18:41:39 +02:00
|
|
|
self.videos = self.config("videos", False)
|
|
|
|
|
2018-08-17 20:04:11 +02:00
|
|
|
def items(self):
|
|
|
|
yield Message.Version, 1
|
|
|
|
yield Message.Directory, self.metadata()
|
|
|
|
|
|
|
|
for tweet in self.tweets():
|
|
|
|
data = self._data_from_tweet(tweet)
|
|
|
|
if not self.retweets and data["retweet_id"]:
|
|
|
|
continue
|
|
|
|
|
2018-09-30 18:41:39 +02:00
|
|
|
images = text.extract_iter(
|
|
|
|
tweet, 'data-image-url="', '"')
|
2018-08-17 20:04:11 +02:00
|
|
|
for data["num"], url in enumerate(images, 1):
|
|
|
|
text.nameext_from_url(url, data)
|
|
|
|
yield Message.Url, url + ":orig", data
|
|
|
|
|
2018-09-30 18:41:39 +02:00
|
|
|
if self.videos and "-videoContainer" in tweet:
|
2018-10-05 17:58:15 +02:00
|
|
|
data["num"] = 1
|
|
|
|
url = "ytdl:{}/{}/status/{}".format(
|
2018-09-30 18:41:39 +02:00
|
|
|
self.root, data["user"], data["tweet_id"])
|
2018-10-05 17:58:15 +02:00
|
|
|
yield Message.Url, url, data
|
2018-09-30 18:41:39 +02:00
|
|
|
|
2018-08-17 20:04:11 +02:00
|
|
|
def metadata(self):
|
|
|
|
"""Return general metadata"""
|
2018-08-19 20:36:33 +02:00
|
|
|
return {"user": self.user}
|
2018-08-17 20:04:11 +02:00
|
|
|
|
|
|
|
def tweets(self):
|
|
|
|
"""Yield HTML content of all relevant tweets"""
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _data_from_tweet(tweet):
|
|
|
|
data = text.extract_all(tweet, (
|
|
|
|
("tweet_id" , 'data-tweet-id="' , '"'),
|
|
|
|
("retweet_id", 'data-retweet-id="' , '"'),
|
|
|
|
("retweeter" , 'data-retweeter="' , '"'),
|
|
|
|
("user" , 'data-screen-name="', '"'),
|
|
|
|
("username" , 'data-name="' , '"'),
|
2018-08-18 18:58:10 +02:00
|
|
|
("user_id" , 'data-user-id="' , '"'),
|
2018-08-17 20:04:11 +02:00
|
|
|
))[0]
|
2018-08-18 18:58:10 +02:00
|
|
|
for key in ("tweet_id", "retweet_id", "user_id"):
|
2018-08-17 20:04:11 +02:00
|
|
|
data[key] = text.parse_int(data[key])
|
|
|
|
data["retweeter"] = data["retweeter"] or ""
|
|
|
|
return data
|
|
|
|
|
2018-08-19 20:36:33 +02:00
|
|
|
def _tweets_from_api(self, url):
|
2018-08-18 18:58:10 +02:00
|
|
|
params = {
|
|
|
|
"include_available_features": "1",
|
|
|
|
"include_entities": "1",
|
|
|
|
"reset_error_state": "false",
|
|
|
|
"lang": "en",
|
|
|
|
}
|
|
|
|
headers = {
|
|
|
|
"X-Requested-With": "XMLHttpRequest",
|
|
|
|
"X-Twitter-Active-User": "yes",
|
|
|
|
"Referer": "{}/{}".format(self.root, self.user)
|
|
|
|
}
|
2018-08-17 20:04:11 +02:00
|
|
|
|
|
|
|
while True:
|
2018-08-18 18:58:10 +02:00
|
|
|
data = self.request(url, params=params, headers=headers).json()
|
2018-11-14 11:48:09 +01:00
|
|
|
if "inner" in data:
|
|
|
|
data = data["inner"]
|
2018-08-17 20:04:11 +02:00
|
|
|
|
|
|
|
for tweet in text.extract_iter(
|
2018-08-18 18:58:10 +02:00
|
|
|
data["items_html"], '<div class="tweet ', '\n</li>'):
|
2018-08-17 20:04:11 +02:00
|
|
|
yield tweet
|
|
|
|
|
2018-08-18 18:58:10 +02:00
|
|
|
if not data["has_more_items"]:
|
2018-08-17 20:04:11 +02:00
|
|
|
return
|
|
|
|
params["max_position"] = text.extract(
|
|
|
|
tweet, 'data-tweet-id="', '"')[0]
|
|
|
|
|
|
|
|
|
2018-08-19 20:36:33 +02:00
|
|
|
class TwitterTimelineExtractor(TwitterExtractor):
|
|
|
|
"""Extractor for all images from a user's timeline"""
|
|
|
|
subcategory = "timeline"
|
|
|
|
pattern = [r"(?:https?://)?(?:www\.|mobile\.)?twitter\.com"
|
|
|
|
r"/([^/?&#]+)/?$"]
|
|
|
|
test = [("https://twitter.com/PicturesEarth", {
|
2018-10-08 23:30:06 +02:00
|
|
|
"range": "1-40",
|
2018-08-19 20:36:33 +02:00
|
|
|
"url": "2f4d51cbba81e56c1c755677b3ad58fc167c9771",
|
|
|
|
"keyword": "cbae53b6f4ba133078bb13c95dbd3cbb4fa40b9f",
|
|
|
|
})]
|
|
|
|
|
|
|
|
def tweets(self):
|
|
|
|
url = "{}/i/profiles/show/{}/timeline/tweets".format(
|
|
|
|
self.root, self.user)
|
|
|
|
return self._tweets_from_api(url)
|
|
|
|
|
|
|
|
|
|
|
|
class TwitterMediaExtractor(TwitterExtractor):
|
|
|
|
"""Extractor for all images from a user's Media Tweets"""
|
|
|
|
subcategory = "media"
|
|
|
|
pattern = [r"(?:https?://)?(?:www\.|mobile\.)?twitter\.com"
|
|
|
|
r"/([^/?&#]+)/media(?!\w)"]
|
|
|
|
test = [("https://twitter.com/PicturesEarth/media", {
|
2018-10-08 23:30:06 +02:00
|
|
|
"range": "1-40",
|
2018-08-19 20:36:33 +02:00
|
|
|
"url": "2f4d51cbba81e56c1c755677b3ad58fc167c9771",
|
|
|
|
})]
|
|
|
|
|
|
|
|
def tweets(self):
|
|
|
|
url = "{}/i/profiles/show/{}/media_timeline".format(
|
|
|
|
self.root, self.user)
|
|
|
|
return self._tweets_from_api(url)
|
|
|
|
|
|
|
|
|
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"
|
|
|
|
pattern = [r"(?:https?://)?(?:www\.|mobile\.)?twitter\.com"
|
|
|
|
r"/([^/?&#]+)/status/(\d+)"]
|
2017-08-06 13:43:08 +02:00
|
|
|
test = [
|
|
|
|
("https://twitter.com/PicturesEarth/status/672897688871018500", {
|
|
|
|
"url": "d9e68d41301d2fe382eb27711dea28366be03b1a",
|
2018-08-18 18:58:10 +02:00
|
|
|
"keyword": "46c8e739a892000848a8a2184da91346c9cbe4bf",
|
2017-08-06 13:43:08 +02:00
|
|
|
"content": "a1f2f04cb2d8df24b1afa7a39910afda23484342",
|
|
|
|
}),
|
|
|
|
("https://twitter.com/perrypumas/status/894001459754180609", {
|
|
|
|
"url": "c8a262a9698cb733fb27870f5a8f75faf77d79f6",
|
2018-08-18 18:58:10 +02:00
|
|
|
"keyword": "7729cd3ff16a5647b0b5ffdec9d428c91eedafbe",
|
2017-08-06 13:43:08 +02: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 metadata(self):
|
|
|
|
return {"user": self.user, "tweet_id": self.tweet_id}
|
2016-10-06 19:12:07 +02:00
|
|
|
|
2018-08-17 20:04:11 +02:00
|
|
|
def tweets(self):
|
|
|
|
url = "{}/{}/status/{}".format(self.root, self.user, self.tweet_id)
|
|
|
|
page = self.request(url).text
|
|
|
|
return (text.extract(
|
|
|
|
page, '<div class="tweet ', '<ul class="stats')[0],)
|