mirror of
https://gitlab.com/jeancf/twoot.git
synced 2025-02-25 01:18:41 +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
|
||||
: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")
|
||||
# Download page with thread
|
||||
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')
|
||||
|
||||
# Build timeline of tuples
|
||||
timeline = []
|
||||
previous_tweet_url = thread_url
|
||||
for item in list:
|
||||
timeline.append((previous_tweet_url, item))
|
||||
# Get the url of the tweet
|
||||
previous_tweet_url = item.find('a', class_='tweet-link')
|
||||
if previous_tweet_url is None:
|
||||
tweet_link_tag = item.find('a', class_='tweet-link')
|
||||
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')
|
||||
|
||||
# return timeline in reverse chronological order
|
||||
timeline.reverse()
|
||||
return timeline
|
||||
|
||||
|
||||
@ -289,12 +296,10 @@ def get_timeline(nitter_url):
|
||||
# Get the url of the tweet
|
||||
thread_link_tag = item.find('a', class_='tweet-link')
|
||||
if thread_link_tag is not None:
|
||||
thread_url = thread_link_tag.get('href')
|
||||
|
||||
timeline.append((thread_url, first_item))
|
||||
thread_url = thread_link_tag.get('href').strip('#m')
|
||||
|
||||
# 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
|
||||
@ -959,7 +964,7 @@ def main(argv):
|
||||
tweets = []
|
||||
out_date_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
|
||||
tweet_id = status.find('a', class_='tweet-link').get('href').strip('#m')
|
||||
status_id = tweet_id.split('/')[3]
|
||||
@ -1122,7 +1127,7 @@ def main(argv):
|
||||
"tweet_text": tweet_text,
|
||||
"video": video_file,
|
||||
"photos": photos,
|
||||
"reply-to": reply_to,
|
||||
"replied_to_tweet": replied_to_tweet,
|
||||
}
|
||||
tweets.append(tweet)
|
||||
|
||||
@ -1190,13 +1195,25 @@ def main(argv):
|
||||
TypeError): # Media cannot be uploaded (invalid format, dead link, etc.)
|
||||
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
|
||||
toot = {}
|
||||
try:
|
||||
if len(media_ids) == 0:
|
||||
toot = mastodon.status_post(tweet['tweet_text'])
|
||||
toot = mastodon.status_post(tweet['tweet_text'], replied_to_toot)
|
||||
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:
|
||||
# Assuming this is an:
|
||||
|
Loading…
x
Reference in New Issue
Block a user