SaxyPandaBear / food-pics

Scheduled webhook that scrapes Reddit and posts to Discord
MIT License
0 stars 0 forks source link

[Bug] Image embeds on Discord do not render when referring to a Reddit image gallery #4

Closed SaxyPandaBear closed 2 years ago

SaxyPandaBear commented 2 years ago

For example. https://www.reddit.com/r/food/comments/r1g417/homemade_chocolate_babka/ does not render the embed in Discord properly. I believe this is because the post itself uses an album, which I don't think was a feature native to Reddit when I originally wrote all of this code.

SaxyPandaBear commented 2 years ago
>>> import pprint
>>> pprint.pprint(vars(sub))
{'_comments': <praw.models.comment_forest.CommentForest object at 0x7fee7058ab70>,
 '_comments_by_id': {'t1_hlydk16': Comment(id='hlydk16'),
                     't1_hlylbgn': Comment(id='hlylbgn'),
                     't1_hlylhfd': Comment(id='hlylhfd'),
                     't1_hlylr1w': Comment(id='hlylr1w'),
                     't1_hlylu04': Comment(id='hlylu04'),
                     't1_hlym6kd': Comment(id='hlym6kd'),
                     't1_hlymdsy': Comment(id='hlymdsy'),
                     't1_hlymdt1': Comment(id='hlymdt1'),
                     't1_hlymmzx': Comment(id='hlymmzx'),
                     't1_hlys4bo': Comment(id='hlys4bo'),
                     't1_hlytsek': Comment(id='hlytsek'),
                     't1_hlyxg6p': Comment(id='hlyxg6p'),
                     't1_hlz0n4i': Comment(id='hlz0n4i'),
                     't1_hlz2utf': Comment(id='hlz2utf'),
                     't1_hlz3710': Comment(id='hlz3710'),
                     't1_hlzl1u8': Comment(id='hlzl1u8'),
                     't1_hlzpcny': Comment(id='hlzpcny'),
                     't1_hlzt2w2': Comment(id='hlzt2w2'),
                     't1_hm062fw': Comment(id='hm062fw'),
                     't1_hm0o0ny': Comment(id='hm0o0ny'),
                     't1_hm0s6pr': Comment(id='hm0s6pr'),
                     't1_hm0sblq': Comment(id='hm0sblq'),
                     't1_hm1632z': Comment(id='hm1632z'),
                     't1_hm18fqy': Comment(id='hm18fqy'),
                     't1_hm195ox': Comment(id='hm195ox'),
                     't1_hm3ne38': Comment(id='hm3ne38')},
 '_fetched': True,
 '_reddit': <praw.reddit.Reddit object at 0x7fee6f1b3668>,
 'all_awardings': [{'award_sub_type': 'GLOBAL',
                    'award_type': 'global',
                    'awardings_required_to_grant_benefits': None,
                    'coin_price': 150,
                    'coin_reward': 0,
                    'count': 2,
                    'days_of_drip_extension': 0,
                    'days_of_premium': 0,
                    'description': 'Thank you stranger. Shows the award.',
                    'end_date': None,
                    'giver_coin_reward': None,
                    'icon_format': None,
                    'icon_height': 2048,
                    'icon_url': 'https://i.redd.it/award_images/t5_22cerq/klvxk1wggfd41_Helpful.png',
                    'icon_width': 2048,
                    'id': 'award_f44611f1-b89e-46dc-97fe-892280b13b82',
                    'is_enabled': True,
                    'is_new': False,
                    'name': 'Helpful',
                    'penny_donate': None,
                    'penny_price': None,
                    'resized_icons': [{'height': 16,
                                       'url': 'https://preview.redd.it/award_images/t5_22cerq/klvxk1wggfd41_Helpful.png?width=16&height=16&auto=webp&s=a5662dfbdb402bf67866c050aa76c31c147c2f45',
                                       'width': 16},
                                      {'height': 32,
                                       'url': 'https://preview.redd.it/award_images/t5_22cerq/klvxk1wggfd41_Helpful.png?width=32&height=32&auto=webp&s=a6882eb3f380e8e88009789f4d0072e17b8c59f1',
                                       'width': 32},
                                      {'height': 48,
                                       'url': 'https://preview.redd.it/award_images/t5_22cerq/klvxk1wggfd41_Helpful.png?width=48&height=48&auto=webp&s=e50064b090879e8a0b55e433f6ee61d5cb5fbe1d',
                                       'width': 48},
                                      {'height': 64,
                                       'url': 'https://preview.redd.it/award_images/t5_22cerq/klvxk1wggfd41_Helpful.png?width=64&height=64&auto=webp&s=8e5bb2e76683cb6b161830bcdd9642049d6adc11',
                                       'width': 64},
                                      {'height': 128,
                                       'url': 'https://preview.redd.it/award_images/t5_22cerq/klvxk1wggfd41_Helpful.png?width=128&height=128&auto=webp&s=eda4a9246f95f42ee6940cc0ec65306fd20de878',
                                       'width': 128}],
                    'resized_static_icons': [{'height': 16,
                                              'url': 'https://preview.redd.it/award_images/t5_22cerq/klvxk1wggfd41_Helpful.png?width=16&height=16&auto=webp&s=a5662dfbdb402bf67866c050aa76c31c147c2f45',
                                              'width': 16},
                                             {'height': 32,
                                              'url': 'https://preview.redd.it/award_images/t5_22cerq/klvxk1wggfd41_Helpful.png?width=32&height=32&auto=webp&s=a6882eb3f380e8e88009789f4d0072e17b8c59f1',
                                              'width': 32},
                                             {'height': 48,
                                              'url': 'https://preview.redd.it/award_images/t5_22cerq/klvxk1wggfd41_Helpful.png?width=48&height=48&auto=webp&s=e50064b090879e8a0b55e433f6ee61d5cb5fbe1d',
                                              'width': 48},
                                             {'height': 64,
                                              'url': 'https://preview.redd.it/award_images/t5_22cerq/klvxk1wggfd41_Helpful.png?width=64&height=64&auto=webp&s=8e5bb2e76683cb6b161830bcdd9642049d6adc11',
                                              'width': 64},
                                             {'height': 128,
                                              'url': 'https://preview.redd.it/award_images/t5_22cerq/klvxk1wggfd41_Helpful.png?width=128&height=128&auto=webp&s=eda4a9246f95f42ee6940cc0ec65306fd20de878',
                                              'width': 128}],
                    'start_date': None,
                    'static_icon_height': 2048,
                    'static_icon_url': 'https://i.redd.it/award_images/t5_22cerq/klvxk1wggfd41_Helpful.png',
                    'static_icon_width': 2048,
                    'subreddit_coin_reward': 0,
                    'subreddit_id': None,
                    'tiers_by_required_awardings': None},
                   {'award_sub_type': 'GLOBAL',
                    'award_type': 'global',
                    'awardings_required_to_grant_benefits': None,
                    'coin_price': 125,
                    'coin_reward': 0,
                    'count': 1,
                    'days_of_drip_extension': 0,
                    'days_of_premium': 0,
                    'description': 'When you come across a feel-good thing.',
                    'end_date': None,
                    'giver_coin_reward': None,
                    'icon_format': None,
                    'icon_height': 2048,
                    'icon_url': 'https://i.redd.it/award_images/t5_22cerq/5izbv4fn0md41_Wholesome.png',
                    'icon_width': 2048,
                    'id': 'award_5f123e3d-4f48-42f4-9c11-e98b566d5897',
                    'is_enabled': True,
                    'is_new': False,
                    'name': 'Wholesome',
                    'penny_donate': None,
                    'penny_price': None,
                    'resized_icons': [{'height': 16,
                                       'url': 'https://preview.redd.it/award_images/t5_22cerq/5izbv4fn0md41_Wholesome.png?width=16&height=16&auto=webp&s=92932f465d58e4c16b12b6eac4ca07d27e3d11c0',
                                       'width': 16},
                                      {'height': 32,
                                       'url': 'https://preview.redd.it/award_images/t5_22cerq/5izbv4fn0md41_Wholesome.png?width=32&height=32&auto=webp&s=d11484a208d68a318bf9d4fcf371171a1cb6a7ef',
                                       'width': 32},
                                      {'height': 48,
                                       'url': 'https://preview.redd.it/award_images/t5_22cerq/5izbv4fn0md41_Wholesome.png?width=48&height=48&auto=webp&s=febdf28b6f39f7da7eb1365325b85e0bb49a9f63',
                                       'width': 48},
                                      {'height': 64,
                                       'url': 'https://preview.redd.it/award_images/t5_22cerq/5izbv4fn0md41_Wholesome.png?width=64&height=64&auto=webp&s=b4406a2d88bf86fa3dc8a45aacf7e0c7bdccc4fb',
                                       'width': 64},
                                      {'height': 128,
                                       'url': 'https://preview.redd.it/award_images/t5_22cerq/5izbv4fn0md41_Wholesome.png?width=128&height=128&auto=webp&s=19555b13e3e196b62eeb9160d1ac1d1b372dcb0b',
                                       'width': 128}],
                    'resized_static_icons': [{'height': 16,
                                              'url': 'https://preview.redd.it/award_images/t5_22cerq/5izbv4fn0md41_Wholesome.png?width=16&height=16&auto=webp&s=92932f465d58e4c16b12b6eac4ca07d27e3d11c0',
                                              'width': 16},
                                             {'height': 32,
                                              'url': 'https://preview.redd.it/award_images/t5_22cerq/5izbv4fn0md41_Wholesome.png?width=32&height=32&auto=webp&s=d11484a208d68a318bf9d4fcf371171a1cb6a7ef',
                                              'width': 32},
                                             {'height': 48,
                                              'url': 'https://preview.redd.it/award_images/t5_22cerq/5izbv4fn0md41_Wholesome.png?width=48&height=48&auto=webp&s=febdf28b6f39f7da7eb1365325b85e0bb49a9f63',
                                              'width': 48},
                                             {'height': 64,
                                              'url': 'https://preview.redd.it/award_images/t5_22cerq/5izbv4fn0md41_Wholesome.png?width=64&height=64&auto=webp&s=b4406a2d88bf86fa3dc8a45aacf7e0c7bdccc4fb',
                                              'width': 64},
                                             {'height': 128,
                                              'url': 'https://preview.redd.it/award_images/t5_22cerq/5izbv4fn0md41_Wholesome.png?width=128&height=128&auto=webp&s=19555b13e3e196b62eeb9160d1ac1d1b372dcb0b',
                                              'width': 128}],
                    'start_date': None,
                    'static_icon_height': 2048,
                    'static_icon_url': 'https://i.redd.it/award_images/t5_22cerq/5izbv4fn0md41_Wholesome.png',
                    'static_icon_width': 2048,
                    'subreddit_coin_reward': 0,
                    'subreddit_id': None,
                    'tiers_by_required_awardings': None}],
 'allow_live_comments': False,
 'approved_at_utc': None,
 'approved_by': None,
 'archived': False,
 'author': Redditor(name='eddiemeddie159'),
 'author_flair_background_color': None,
 'author_flair_css_class': None,
 'author_flair_richtext': [],
 'author_flair_template_id': None,
 'author_flair_text': None,
 'author_flair_text_color': None,
 'author_flair_type': 'text',
 'author_fullname': 't2_16pstq',
 'author_is_blocked': False,
 'author_patreon_flair': False,
 'author_premium': False,
 'awarders': [],
 'banned_at_utc': None,
 'banned_by': None,
 'can_gild': False,
 'can_mod_post': False,
 'category': None,
 'clicked': False,
 'comment_limit': 2048,
 'comment_sort': 'confidence',
 'content_categories': None,
 'contest_mode': False,
 'created': 1637790496.0,
 'created_utc': 1637790496.0,
 'discussion_type': None,
 'distinguished': None,
 'domain': 'reddit.com',
 'downs': 0,
 'edited': False,
 'gallery_data': {'items': [{'id': 88695557, 'media_id': 'd5dml9qh6m181'},
                            {'id': 88695558, 'media_id': 'aimzngvh6m181'}]},
 'gilded': 0,
 'gildings': {},
 'hidden': False,
 'hide_score': False,
 'id': 'r1g417',
 'is_created_from_ads_ui': False,
 'is_crosspostable': False,
 'is_gallery': True,
 'is_meta': False,
 'is_original_content': True,
 'is_reddit_media_domain': False,
 'is_robot_indexable': True,
 'is_self': False,
 'is_video': False,
 'likes': None,
 'link_flair_background_color': '',
 'link_flair_css_class': None,
 'link_flair_richtext': [],
 'link_flair_text': None,
 'link_flair_text_color': 'dark',
 'link_flair_type': 'text',
 'locked': False,
 'media': None,
 'media_embed': {},
 'media_metadata': {'aimzngvh6m181': {'e': 'Image',
                                      'id': 'aimzngvh6m181',
                                      'm': 'image/jpg',
                                      'p': [{'u': 'https://preview.redd.it/aimzngvh6m181.jpg?width=108&crop=smart&auto=webp&s=e7cb47a0bb31a90c0e17e41acbebebb0155e47dc',
                                             'x': 108,
                                             'y': 108},
                                            {'u': 'https://preview.redd.it/aimzngvh6m181.jpg?width=216&crop=smart&auto=webp&s=27afc51e56b70aa302a8aad84d59cff5758c7f8e',
                                             'x': 216,
                                             'y': 216},
                                            {'u': 'https://preview.redd.it/aimzngvh6m181.jpg?width=320&crop=smart&auto=webp&s=9c3b337d2a715ebdf3be3d6b274a5f8faac784fd',
                                             'x': 320,
                                             'y': 320},
                                            {'u': 'https://preview.redd.it/aimzngvh6m181.jpg?width=640&crop=smart&auto=webp&s=1e100729f7bf3a9ca5da0d916931fe5228ffb3ed',
                                             'x': 640,
                                             'y': 640},
                                            {'u': 'https://preview.redd.it/aimzngvh6m181.jpg?width=960&crop=smart&auto=webp&s=0f69dc053c4f9f2fdcbd7c624c15436687d61e04',
                                             'x': 960,
                                             'y': 960},
                                            {'u': 'https://preview.redd.it/aimzngvh6m181.jpg?width=1080&crop=smart&auto=webp&s=8a77a52067bbad7bbc5234beaa78c7f22a502fca',
                                             'x': 1080,
                                             'y': 1080}],
                                      's': {'u': 'https://preview.redd.it/aimzngvh6m181.jpg?width=1440&format=pjpg&auto=webp&s=5fd3ee2edddb1da2344e96b5fe9435bea9ae7cc7',
                                            'x': 1440,
                                            'y': 1440},
                                      'status': 'valid'},
                    'd5dml9qh6m181': {'e': 'Image',
                                      'id': 'd5dml9qh6m181',
                                      'm': 'image/jpg',
                                      'p': [{'u': 'https://preview.redd.it/d5dml9qh6m181.jpg?width=108&crop=smart&auto=webp&s=74f7b663c7bd0db2e728ebfa4233bd7916221873',
                                             'x': 108,
                                             'y': 108},
                                            {'u': 'https://preview.redd.it/d5dml9qh6m181.jpg?width=216&crop=smart&auto=webp&s=eb9e8af141ad45041a174882ae17c5ec261c127b',
                                             'x': 216,
                                             'y': 216},
                                            {'u': 'https://preview.redd.it/d5dml9qh6m181.jpg?width=320&crop=smart&auto=webp&s=8b8a583cfab8a9b23f962b8f6fc31de1748651f3',
                                             'x': 320,
                                             'y': 320},
                                            {'u': 'https://preview.redd.it/d5dml9qh6m181.jpg?width=640&crop=smart&auto=webp&s=6fd3c4d9bf737f42fbfbe1f0b3afbdf994e357d6',
                                             'x': 640,
                                             'y': 640},
                                            {'u': 'https://preview.redd.it/d5dml9qh6m181.jpg?width=960&crop=smart&auto=webp&s=255f40d83ab3a861a49318a2e787be0bb2f0319f',
                                             'x': 960,
                                             'y': 960},
                                            {'u': 'https://preview.redd.it/d5dml9qh6m181.jpg?width=1080&crop=smart&auto=webp&s=e82bb437682ca9627fe723dd85870a454ea85ad0',
                                             'x': 1080,
                                             'y': 1080}],
                                      's': {'u': 'https://preview.redd.it/d5dml9qh6m181.jpg?width=1440&format=pjpg&auto=webp&s=2042f03f1e4b7169a979a02a080391f814314f3a',
                                            'x': 1440,
                                            'y': 1440},
                                      'status': 'valid'}},
 'media_only': False,
 'mod_note': None,
 'mod_reason_by': None,
 'mod_reason_title': None,
 'mod_reports': [],
 'name': 't3_r1g417',
 'no_follow': False,
 'num_comments': 27,
 'num_crossposts': 2,
 'num_duplicates': 2,
 'num_reports': None,
 'over_18': False,
 'parent_whitelist_status': 'all_ads',
 'permalink': '/r/food/comments/r1g417/homemade_chocolate_babka/',
 'pinned': False,
 'pwls': 6,
 'quarantine': False,
 'removal_reason': None,
 'removed_by': None,
 'removed_by_category': None,
 'report_reasons': None,
 'saved': False,
 'score': 1085,
 'secure_media': None,
 'secure_media_embed': {},
 'selftext': '',
 'selftext_html': None,
 'send_replies': True,
 'spoiler': False,
 'stickied': False,
 'subreddit': Subreddit(display_name='food'),
 'subreddit_id': 't5_2qh55',
 'subreddit_name_prefixed': 'r/food',
 'subreddit_subscribers': 21545971,
 'subreddit_type': 'public',
 'suggested_sort': None,
 'thumbnail': 'https://b.thumbs.redditmedia.com/yNinz9SYuP9vzErFmg-W_VnQ3hpOovuclnFM_6C2Fho.jpg',
 'thumbnail_height': 140,
 'thumbnail_width': 140,
 'title': '[Homemade] Chocolate Babka!',
 'top_awarded_type': None,
 'total_awards_received': 3,
 'treatment_tags': [],
 'ups': 1085,
 'upvote_ratio': 0.98,
 'url': 'https://www.reddit.com/gallery/r1g417',
 'url_overridden_by_dest': 'https://www.reddit.com/gallery/r1g417',
 'user_reports': [],
 'view_count': None,
 'visited': False,
 'whitelist_status': 'all_ads',
 'wls': 6}

As of 11/25/2021, these are the dynamic attributes returned by the Reddit API

SaxyPandaBear commented 2 years ago

The FoodPost class uses the url attribute of the Submission object. In this case, it points to https://www.reddit.com/gallery/r1g417, which is not an image.

I don't want to fuss around with trying to introduce a carousel in the embed - don't think this even works without a persistent bot client. I did notice that there is media metadata returned in the submission for the different images in the gallery.

What I can do is pick one of the images from there and derive the URL myself, i.e.: https://i.redd.it/d5dml9qh6m181.jpg

The metadata map is unordered, so I'll opt to just always take the first image ID.

SaxyPandaBear commented 2 years ago

This does not account for posting an Imgur album.. but cross that bridge when I get there