diff --git a/tootbot.py b/tootbot.py index 626d14f1675dcc42f5b16d685765e2ea1cbaac28..e4442b3367231396c24a1128e80fbd3a15fd16f7 100755 --- a/tootbot.py +++ b/tootbot.py @@ -16,15 +16,11 @@ mastodon_api = None # Set to 1 to get some messages, 0 for error messages only debug=0 -# Posting delay (s), wait between mastodon posts, reduces the "burst" effect on timoeline, and instance workload if you hit rate limiters +# Posting delay (s), wait between mastodon posts, reduces the "burst" effect on timeline, and instance workload if you hit rate limiters posting_delay=3 # Post only this much tweets per cycle -max_tweets=2 - -# URL substrings dedicated to source image hosting -twitter_pics_hosting="https://pbs.twitimg.com/" -nitter_pics_hosting="https://nitter.net/pic/" +max_tweets=15 # Program logic below this line @@ -39,6 +35,10 @@ days = int(sys.argv[3]) tags = sys.argv[4] delay = int(sys.argv[5]) +# URL substrings dedicated to source image hosting +twitter_pics_hosting="https://pbs.twitimg.com/" +nitter_pics_hosting=source.split('/search', 1)[0] + "/pic/" + # Returns the parameter from the specified file def get_config(parameter, file_path): # Check if secrets file exists @@ -155,13 +155,15 @@ for tweet in reversed(d.entries): # get the pictures... from nitter if nitter_pics_hosting in tweet.summary: - for p in re.finditer(r"https://nitter.net/pic/[^ \xa0\"]*", tweet.summary): - try: - media = requests.get(p.group(0)) - media_posted = mastodon_api.media_post(media.content, mime_type=media.headers.get('content-type')) - toot_media.append(media_posted['id']) - except: - print("ERROR: Can't get media !") + for line in tweet.summary.split('\n'): + if nitter_pics_hosting in line: + pic=re.search("(?P<url>https?://[^\s'\"]+)", line).group("url") + try: + media = requests.get(pic) + media_posted = mastodon_api.media_post(media.content, mime_type=media.headers.get('content-type')) + toot_media.append(media_posted['id']) + except: + print("ERROR: Can't get media !") # replace short links by original URL m = re.search(r"http[^ \xa0]*", toot_body)