Add previews for reddit galleries

This commit is contained in:
Tim Van Baak 2021-03-08 13:13:41 -08:00
parent 77ad88124f
commit 5d0c5b1ae3
1 changed files with 11 additions and 0 deletions

View File

@ -181,6 +181,17 @@ class RedditScraper:
parts.append(f'<img src="{preview["url"]}">') parts.append(f'<img src="{preview["url"]}">')
except: except:
pass pass
if getattr(post, 'is_gallery', False):
try:
for gallery_item in post.gallery_data['items']:
media_id = gallery_item['media_id']
metadata = post.media_metadata[media_id]
small_previews = [p for p in metadata['p'] if p['x'] < 800]
preview = sorted(small_previews, key=lambda p:-p['x'])[0]
parts.append(f'<i>link:</i> <a href="{metadata["s"]["u"]}">{metadata["s"]["u"]}</a>')
parts.append(f'<img src="{preview["u"]}">')
except:
pass
if post.selftext: if post.selftext:
limit = post.selftext[1024:].find(' ') limit = post.selftext[1024:].find(' ')
preview_body = post.selftext[:1024 + limit] preview_body = post.selftext[:1024 + limit]