Correct order of thread tweets

This commit is contained in:
jeancf 2023-07-16 15:43:13 +02:00
parent 37204c5202
commit 061db3f729

View File

@ -173,7 +173,10 @@ Only used by `get_timeline()`.
: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
"""
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 = [(thread_url, first_item)]
logging.debug("Downloading tweets in thread from separate page")
# Download page with thread
url = nitter_url + thread_url
@ -206,7 +209,6 @@ def _get_rest_of_thread(session, headers, nitter_url, thread_url):
list = after_tweet.find_all('div', class_='timeline-item')
# Build timeline of tuples
timeline = []
previous_tweet_url = thread_url
for item in list:
timeline.append((previous_tweet_url, item))
@ -216,7 +218,8 @@ def _get_rest_of_thread(session, headers, nitter_url, thread_url):
previous_tweet_url = previous_tweet_tag.get('href').strip('#m')
logging.error('Thread tweet is missing link tag')
return timeline
# return timeline in reverse chronological order
return timeline.reverse()
"""
@ -292,10 +295,8 @@ def get_timeline(nitter_url):
if thread_link_tag is not None:
thread_url = thread_link_tag.get('href').strip('#m')
timeline.append((thread_url, first_item))
# 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:
# Ignore other classes
continue
@ -1195,11 +1196,11 @@ def main(argv):
replied_to_toot = None
if tweet['replied_to_tweet'] is not None:
logging.debug("Searching db for replied-to-tweet " + tweet['replied_to_tweet'])
db.execute("SELECT toot_id FROM toots WHERE tweet_id=?", (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', replied_to_tweet)
logging.warning('Replied-to tweet %s not found in database', tweet['replied_to_tweet'])
# Post toot
toot = {}