From e6f4dcf90f980fdf3bf357353e98575a3576e3ea Mon Sep 17 00:00:00 2001 From: Etienne Boucher Date: Tue, 25 Mar 2025 01:53:07 -0400 Subject: [PATCH] Handle instagram.com/share/ URLs - added Sec-Fetch* headers for proper redirect - fixed formatting - restore 'guide' test - integrate in PostExtractor code --- gallery_dl/extractor/instagram.py | 14 ++++++++++++-- test/results/instagram.py | 14 ++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/gallery_dl/extractor/instagram.py b/gallery_dl/extractor/instagram.py index 942feb3b..0f88cac3 100644 --- a/gallery_dl/extractor/instagram.py +++ b/gallery_dl/extractor/instagram.py @@ -707,11 +707,21 @@ class InstagramPostExtractor(InstagramExtractor): """Extractor for an Instagram post""" subcategory = "post" pattern = (r"(?:https?://)?(?:www\.)?instagram\.com" - r"/(?:[^/?#]+/)?(?:p|tv|reel)/([^/?#]+)") + r"/(?:share/()|[^/?#]+/)?(?:p|tv|reel)/([^/?#]+)") example = "https://www.instagram.com/p/abcdefg/" def posts(self): - return self.api.media(self.item) + share, shortcode = self.groups + if share is not None: + url = text.ensure_http_scheme(self.url) + headers = { + "Sec-Fetch-Dest": "empty", + "Sec-Fetch-Mode": "navigate", + "Sec-Fetch-Site": "same-origin", + } + location = self.request_location(url, headers=headers) + shortcode = location.split("/")[-2] + return self.api.media(shortcode) class InstagramRestAPI(): diff --git a/test/results/instagram.py b/test/results/instagram.py index 3cdb7cd8..4175a7fb 100644 --- a/test/results/instagram.py +++ b/test/results/instagram.py @@ -277,4 +277,18 @@ __tests__ = ( "#class" : instagram.InstagramPostExtractor, }, +{ + "#url" : "https://www.instagram.com/share/p/BACiUUUYQV", + "#category": ("", "instagram", "post"), + "#class" : instagram.InstagramPostExtractor, + "shortcode" : "C6q-XdvsU5v", +}, + +{ + "#url" : "https://www.instagram.com/share/reel/BARSSL4rTu", + "#category": ("", "instagram", "post"), + "#class" : instagram.InstagramPostExtractor, + "shortcode" : "DHbVbT4Jx0c", +} + )