mirror of
https://gitlab.com/jeancf/twoot.git
synced 2025-02-25 09:28:43 +00:00
Compare commits
6 Commits
497d9f3a20
...
24e9bd5691
Author | SHA1 | Date | |
---|---|---|---|
|
24e9bd5691 | ||
|
061db3f729 | ||
|
37204c5202 | ||
|
b227c1bb04 | ||
|
8bd8aeec29 | ||
|
e62e5634e0 |
41
twoot.py
41
twoot.py
@ -173,7 +173,10 @@ Only used by `get_timeline()`.
|
|||||||
:param thread_url: url of the first tweet in thread
|
:param thread_url: url of the first tweet in thread
|
||||||
:return: list of tuples with url of tweet replied-to (or None) and content of tweet
|
:return: list of tuples with url of tweet replied-to (or None) and content of tweet
|
||||||
"""
|
"""
|
||||||
def _get_rest_of_thread(session, headers, nitter_url, thread_url):
|
def _get_rest_of_thread(session, headers, nitter_url, thread_url, first_item):
|
||||||
|
# Add first item to timeline
|
||||||
|
timeline = [(None, first_item)]
|
||||||
|
|
||||||
logging.debug("Downloading tweets in thread from separate page")
|
logging.debug("Downloading tweets in thread from separate page")
|
||||||
# Download page with thread
|
# Download page with thread
|
||||||
url = nitter_url + thread_url
|
url = nitter_url + thread_url
|
||||||
@ -206,15 +209,19 @@ def _get_rest_of_thread(session, headers, nitter_url, thread_url):
|
|||||||
list = after_tweet.find_all('div', class_='timeline-item')
|
list = after_tweet.find_all('div', class_='timeline-item')
|
||||||
|
|
||||||
# Build timeline of tuples
|
# Build timeline of tuples
|
||||||
timeline = []
|
|
||||||
previous_tweet_url = thread_url
|
previous_tweet_url = thread_url
|
||||||
for item in list:
|
for item in list:
|
||||||
timeline.append((previous_tweet_url, item))
|
timeline.append((previous_tweet_url, item))
|
||||||
# Get the url of the tweet
|
# Get the url of the tweet
|
||||||
previous_tweet_url = item.find('a', class_='tweet-link')
|
tweet_link_tag = item.find('a', class_='tweet-link')
|
||||||
if previous_tweet_url is None:
|
if tweet_link_tag is not None:
|
||||||
|
previous_tweet_url = tweet_link_tag.get('href').strip('#m')
|
||||||
|
else:
|
||||||
|
previous_tweet_url = None
|
||||||
logging.error('Thread tweet is missing link tag')
|
logging.error('Thread tweet is missing link tag')
|
||||||
|
|
||||||
|
# return timeline in reverse chronological order
|
||||||
|
timeline.reverse()
|
||||||
return timeline
|
return timeline
|
||||||
|
|
||||||
|
|
||||||
@ -289,12 +296,10 @@ def get_timeline(nitter_url):
|
|||||||
# Get the url of the tweet
|
# Get the url of the tweet
|
||||||
thread_link_tag = item.find('a', class_='tweet-link')
|
thread_link_tag = item.find('a', class_='tweet-link')
|
||||||
if thread_link_tag is not None:
|
if thread_link_tag is not None:
|
||||||
thread_url = thread_link_tag.get('href')
|
thread_url = thread_link_tag.get('href').strip('#m')
|
||||||
|
|
||||||
timeline.append((thread_url, first_item))
|
|
||||||
|
|
||||||
# Get the rest of the items of the thread
|
# Get the rest of the items of the thread
|
||||||
timeline.extend(_get_rest_of_thread(session, headers, nitter_url, thread_url))
|
timeline.extend(_get_rest_of_thread(session, headers, nitter_url, thread_url, first_item))
|
||||||
else:
|
else:
|
||||||
# Ignore other classes
|
# Ignore other classes
|
||||||
continue
|
continue
|
||||||
@ -959,7 +964,7 @@ def main(argv):
|
|||||||
tweets = []
|
tweets = []
|
||||||
out_date_cnt = 0
|
out_date_cnt = 0
|
||||||
in_db_cnt = 0
|
in_db_cnt = 0
|
||||||
for reply_to, status in timeline:
|
for replied_to_tweet, status in timeline:
|
||||||
# Extract tweet ID and status ID
|
# Extract tweet ID and status ID
|
||||||
tweet_id = status.find('a', class_='tweet-link').get('href').strip('#m')
|
tweet_id = status.find('a', class_='tweet-link').get('href').strip('#m')
|
||||||
status_id = tweet_id.split('/')[3]
|
status_id = tweet_id.split('/')[3]
|
||||||
@ -1122,7 +1127,7 @@ def main(argv):
|
|||||||
"tweet_text": tweet_text,
|
"tweet_text": tweet_text,
|
||||||
"video": video_file,
|
"video": video_file,
|
||||||
"photos": photos,
|
"photos": photos,
|
||||||
"reply-to": reply_to,
|
"replied_to_tweet": replied_to_tweet,
|
||||||
}
|
}
|
||||||
tweets.append(tweet)
|
tweets.append(tweet)
|
||||||
|
|
||||||
@ -1190,13 +1195,25 @@ def main(argv):
|
|||||||
TypeError): # Media cannot be uploaded (invalid format, dead link, etc.)
|
TypeError): # Media cannot be uploaded (invalid format, dead link, etc.)
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
# Find in database toot id of replied_to_tweet
|
||||||
|
replied_to_toot = None
|
||||||
|
if tweet['replied_to_tweet'] is not None:
|
||||||
|
logging.debug("Searching db for toot corresponding to replied-to-tweet " + tweet['replied_to_tweet'])
|
||||||
|
db.execute("SELECT toot_id FROM toots WHERE tweet_id=?", [tweet['replied_to_tweet']])
|
||||||
|
replied_to_toot = db.fetchone()
|
||||||
|
|
||||||
|
if replied_to_toot is None:
|
||||||
|
logging.warning('Replied-to tweet %s not found in database', tweet['replied_to_tweet'])
|
||||||
|
else:
|
||||||
|
logging.debug("toot %s found", replied_to_toot)
|
||||||
|
|
||||||
# Post toot
|
# Post toot
|
||||||
toot = {}
|
toot = {}
|
||||||
try:
|
try:
|
||||||
if len(media_ids) == 0:
|
if len(media_ids) == 0:
|
||||||
toot = mastodon.status_post(tweet['tweet_text'])
|
toot = mastodon.status_post(tweet['tweet_text'], replied_to_toot)
|
||||||
else:
|
else:
|
||||||
toot = mastodon.status_post(tweet['tweet_text'], media_ids=media_ids)
|
toot = mastodon.status_post(tweet['tweet_text'], replied_to_toot, media_ids=media_ids)
|
||||||
|
|
||||||
except MastodonAPIError:
|
except MastodonAPIError:
|
||||||
# Assuming this is an:
|
# Assuming this is an:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user