Handle instagram.com/share/ URLs

- added Sec-Fetch* headers for proper redirect
- fixed formatting
- restore 'guide' test
- integrate in PostExtractor code
This commit is contained in:
Etienne Boucher
2025-03-25 01:53:07 -04:00
committed by Mike Fährmann
parent 2798fb8a80
commit e6f4dcf90f
2 changed files with 26 additions and 2 deletions

View File

@@ -707,11 +707,21 @@ class InstagramPostExtractor(InstagramExtractor):
"""Extractor for an Instagram post""" """Extractor for an Instagram post"""
subcategory = "post" subcategory = "post"
pattern = (r"(?:https?://)?(?:www\.)?instagram\.com" pattern = (r"(?:https?://)?(?:www\.)?instagram\.com"
r"/(?:[^/?#]+/)?(?:p|tv|reel)/([^/?#]+)") r"/(?:share/()|[^/?#]+/)?(?:p|tv|reel)/([^/?#]+)")
example = "https://www.instagram.com/p/abcdefg/" example = "https://www.instagram.com/p/abcdefg/"
def posts(self): 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(): class InstagramRestAPI():

View File

@@ -277,4 +277,18 @@ __tests__ = (
"#class" : instagram.InstagramPostExtractor, "#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",
}
) )