Add support for captions in Reddit albums

Albums with captions:
- https://redd.it/zlu89r
- https://redd.it/tejlvr
- https://redd.it/teju6i
- https://redd.it/tepyv9

Album with only one caption: https://redd.it/zmhu48
Album with no caption: https://redd.it/t3urgf
This commit is contained in:
blankie 2022-12-15 17:19:05 +07:00
parent a4519a1089
commit 82d662363c
Signed by: blankie
GPG Key ID: CC15FC822C7F61F5
1 changed files with 20 additions and 14 deletions

View File

@ -402,20 +402,26 @@ async def main():
elif getattr(random_post, 'is_gallery', None): elif getattr(random_post, 'is_gallery', None):
files = [] files = []
captions = [] captions = []
if getattr(random_post, 'gallery_data', None): gallery_data = sorted(random_post.gallery_data['items'], key=lambda i: i['id'])
gallery_keys = map(lambda i: i[1], sorted(map(lambda i: (i['id'], i['media_id']), random_post.gallery_data['items']), key=lambda i: i[0])) # no enumerate() because we want to update the count only if the media's status is valid
else: count = 1
gallery_keys = random_post.media_metadata.keys() for gallery_data_item in gallery_data:
for a, i in enumerate(gallery_keys): media_metadata_item = random_post.media_metadata[gallery_data_item['media_id']]
i = random_post.media_metadata[i] if media_metadata_item['status'] != 'valid':
if i['status'] == 'valid': continue
filename = os.path.join(tempdir, str(time.time())) filename = os.path.join(tempdir, str(time.time()))
for j in ('u', 'mp4', 'gif'): for media_type in ('u', 'mp4', 'gif'):
if j in i['s']: if media_type not in media_metadata_item['s']:
await _download_file(filename, i['s'][j]) continue
break await _download_file(filename, media_metadata_item['s'][media_type])
captions.append(f'{text}\n#{a + 1}') break
files.append(filename) caption = f'{text}\n#{count}'
real_caption = (gallery_data_item.get('caption') or '').strip()
if real_caption:
caption += f': {html.escape(real_caption)}'
captions.append(caption)
files.append(filename)
count += 1
url = None url = None
if url: if url:
parsed = list(urlparse(url)) parsed = list(urlparse(url))