From 66c5298507f2911341a67ffc9b44e44514093f8b Mon Sep 17 00:00:00 2001 From: cquest Date: Sat, 5 Nov 2022 09:45:06 +0100 Subject: [PATCH] support RSS feeds --- tootbot.py | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/tootbot.py b/tootbot.py index c114334..f8ed4b2 100755 --- a/tootbot.py +++ b/tootbot.py @@ -76,6 +76,69 @@ except: if source[:4] == 'http': d = feedparser.parse(source) twitter = None + for t in reversed(d.entries): + # check if this tweet has been processed + if id in t: + id = t.id + else: + id = t.title + db.execute('SELECT * FROM tweets WHERE tweet = ? AND twitter = ? and mastodon = ? and instance = ?', (id, source, mastodon, instance)) # noqa + last = db.fetchone() + dt = t.published_parsed + age = datetime.now()-datetime(dt.tm_year, dt.tm_mon, dt.tm_mday, + dt.tm_hour, dt.tm_min, dt.tm_sec) + # process only unprocessed tweets less than 1 day old, after delay + if last is None and age < timedelta(days=days) and age > timedelta(days=delay): + c = t.title + if twitter and t.author.lower() != ('(@%s)' % twitter).lower(): + c = ("RT https://twitter.com/%s\n" % t.author[2:-1]) + c + toot_media = [] + # get the pictures... + if 'summary' in t: + for p in re.finditer(r"https://pbs.twimg.com/[^ \xa0\"]*", t.summary): + 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']) + + if 'links' in t: + for l in t.links: + if l.type in ('image/jpg', 'image/png'): + media = requests.get(l.url) + media_posted = mastodon_api.media_post( + media.content, mime_type=media.headers.get('content-type')) + toot_media.append(media_posted['id']) + + # replace short links by original URL + m = re.search(r"http[^ \xa0]*", c) + if m is not None: + l = m.group(0) + r = requests.get(l, allow_redirects=False) + if r.status_code in {301, 302}: + c = c.replace(l, r.headers.get('Location')) + + # remove ellipsis + c = c.replace('\xa0…', ' ') + + if 'authors' in t: + c = c + '\nSource: ' + t.authors[0].name + c = c + '\n\n' + t.link + + if tags: + c = c + '\n' + tags + + if toot_media is not None: + toot = mastodon_api.status_post(c, + in_reply_to_id=None, + media_ids=toot_media, + sensitive=False, + visibility='public', + spoiler_text=None) + if "id" in toot: + db.execute("INSERT INTO tweets VALUES ( ? , ? , ? , ? , ? )", + (id, toot["id"], source, mastodon, instance)) + sql.commit() + else: d = feedparser.parse('http://twitrss.me/twitter_user_to_rss/?user='+source) twitter = source