support RSS feeds

This commit is contained in:
cquest 2022-11-05 09:45:06 +01:00
parent c5614c6071
commit 66c5298507

View File

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