Support for Reddit

This commit is contained in:
Horațiu Mlendea 2022-12-07 17:49:14 +02:00
parent 3b948146ff
commit 03da7875aa

View File

@ -100,6 +100,8 @@ if source[:4] == 'http':
c = ("RT https://twitter.com/%s\n" % t.author[2:-1]) + c
toot_media = []
# get the pictures...
hasMedia = False
if 'summary' in t:
for p in re.finditer(r"https://pbs.twimg.com/[^ \xa0\"]*", t.summary):
media = requests.get(p.group(0))
@ -107,21 +109,39 @@ if source[:4] == 'http':
media.content, mime_type=media.headers.get('content-type'))
toot_media.append(media_posted['id'])
for p in re.finditer(r"https://i.redd.it/[a-zA-Z0-9]*.(gif/jpg/mp4/png|webp)", t.summary):
mediaUrl = p.group(0)
try:
media = requests.get(mediaUrl)
media_posted = mastodon_api.media_post(
media.content, mime_type=media.headers.get('content-type'))
toot_media.append(media_posted['id'])
hasMedia = True
except:
print('Could not upload media to Mastodon! ' + mediaUrl)
if 'links' in t:
for l in t.links:
if l.type in ('image/jpg', 'image/png'):
if l.type in ('image/gif', 'image/jpg', 'image/png', 'image/webp'):
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'])
hasMedia = True
if toot_media is None:
hasMedia = False
# replace short links by original URL
m = re.search(r"http[^ \xa0]*", c)
if m is not None:
l = m.group(0)
try:
r = requests.get(l, allow_redirects=False)
if r.status_code in {301, 302}:
c = c.replace(l, r.headers.get('Location'))
except:
print('Cannot resolve link redirect: ' + l)
# remove ellipsis
c = c.replace('\xa0', ' ')
@ -130,6 +150,10 @@ if source[:4] == 'http':
c = c + '\nSource: ' + t.authors[0].name
c = c + '\n\n' + t.link
# replace links to reddit by libreddit ones
c = c.replace('old.reddit.com', 'libreddit.net')
c = c.replace('reddit.com', 'libreddit.net')
if tags:
c = c + '\n' + tags