mirror of
https://gitlab.com/jeancf/twoot.git
synced 2025-02-25 09:28:43 +00:00
Compare commits
No commits in common. "24e9bd5691677cd5b8f21e0e46699daba972feb1" and "497d9f3a2069e8dedfabd7498ef81e6e9a473872" have entirely different histories.
24e9bd5691
...
497d9f3a20
41
twoot.py
41
twoot.py
@ -173,10 +173,7 @@ 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, first_item):
|
def _get_rest_of_thread(session, headers, nitter_url, thread_url):
|
||||||
# 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
|
||||||
@ -209,19 +206,15 @@ def _get_rest_of_thread(session, headers, nitter_url, thread_url, first_item):
|
|||||||
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
|
||||||
tweet_link_tag = item.find('a', class_='tweet-link')
|
previous_tweet_url = item.find('a', class_='tweet-link')
|
||||||
if tweet_link_tag is not None:
|
if previous_tweet_url is 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
|
||||||
|
|
||||||
|
|
||||||
@ -296,10 +289,12 @@ 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').strip('#m')
|
thread_url = thread_link_tag.get('href')
|
||||||
|
|
||||||
|
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, first_item))
|
timeline.extend(_get_rest_of_thread(session, headers, nitter_url, thread_url))
|
||||||
else:
|
else:
|
||||||
# Ignore other classes
|
# Ignore other classes
|
||||||
continue
|
continue
|
||||||
@ -964,7 +959,7 @@ def main(argv):
|
|||||||
tweets = []
|
tweets = []
|
||||||
out_date_cnt = 0
|
out_date_cnt = 0
|
||||||
in_db_cnt = 0
|
in_db_cnt = 0
|
||||||
for replied_to_tweet, status in timeline:
|
for reply_to, 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]
|
||||||
@ -1127,7 +1122,7 @@ def main(argv):
|
|||||||
"tweet_text": tweet_text,
|
"tweet_text": tweet_text,
|
||||||
"video": video_file,
|
"video": video_file,
|
||||||
"photos": photos,
|
"photos": photos,
|
||||||
"replied_to_tweet": replied_to_tweet,
|
"reply-to": reply_to,
|
||||||
}
|
}
|
||||||
tweets.append(tweet)
|
tweets.append(tweet)
|
||||||
|
|
||||||
@ -1195,25 +1190,13 @@ 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'], replied_to_toot)
|
toot = mastodon.status_post(tweet['tweet_text'])
|
||||||
else:
|
else:
|
||||||
toot = mastodon.status_post(tweet['tweet_text'], replied_to_toot, media_ids=media_ids)
|
toot = mastodon.status_post(tweet['tweet_text'], 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