Compare commits

..

6 Commits

Author SHA1 Message Date
jeancf
24e9bd5691 Squash bugs 2023-07-16 16:15:50 +02:00
jeancf
061db3f729 Correct order of thread tweets 2023-07-16 15:43:13 +02:00
jeancf
37204c5202 Correct previous_tweet_url 2023-07-16 15:42:58 +02:00
jeancf
b227c1bb04 Modify status_post. Code complete. 2023-07-16 12:56:17 +02:00
jeancf
8bd8aeec29 Moved db lookup to the right section 2023-07-16 12:48:26 +02:00
jeancf
e62e5634e0 Added db lookup for replied_to_tweet 2023-07-16 12:37:26 +02:00

View File

@ -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: