Skip to content

Commit

Permalink
When downloading the story file, the request.get request was used dir…
Browse files Browse the repository at this point in the history
…ectly, which did not include the data specified by the user at the beginning of the work: headers, proxy & other data. Because of this, users from countries where Instagram is blocked could not use the method correctly.

There was some confusion in the story_pk typing.
Critical error due to video_url: HttpUrl and thumbnail_url: HttpUrl being passed directly to the urllib.parse.urlparse method, which does not support it.
  • Loading branch information
krasov-rf committed Dec 26, 2023
1 parent c74dafe commit 0bc4b76
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
3 changes: 2 additions & 1 deletion instagrapi/extractors.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ def extract_hashtag_v1(data):
def extract_story_v1(data):
"""Extract story from Private API"""
story = deepcopy(data)
story["pk"] = str(story.get("pk"))
if "video_versions" in story:
# Select Best Quality by Resolutiuon
story["video_url"] = sorted(
Expand Down Expand Up @@ -437,7 +438,7 @@ def extract_story_gql(data):
if story_cta_url:
story["links"] = [StoryLink(**{"webUri": story_cta_url})]
story["user"] = extract_user_short(story.get("owner"))
story["pk"] = int(story["id"])
story["pk"] = str(story["id"])
story["id"] = f"{story['id']}_{story['owner']['id']}"
story["code"] = InstagramIdCodec.encode(story["pk"])
story["taken_at"] = story["taken_at_timestamp"]
Expand Down
9 changes: 6 additions & 3 deletions instagrapi/mixins/public.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def public_request(
continue

def _send_public_request(
self, url, data=None, params=None, headers=None, return_json=False
self, url, data=None, params=None, headers=None, return_json=False, stream=None, timeout=None
):
self.public_requests_count += 1
if headers:
Expand All @@ -135,13 +135,16 @@ def _send_public_request(
try:
if data is not None: # POST
response = self.public.data(
url, data=data, params=params, proxies=self.public.proxies
url, data=data, params=params, proxies=self.public.proxies, timeout=timeout
)
else: # GET
response = self.public.get(
url, params=params, proxies=self.public.proxies
url, params=params, proxies=self.public.proxies, stream=stream, timeout=timeout
)

if stream:
return response

expected_length = int(response.headers.get("Content-Length") or 0)
actual_length = response.raw.tell()
if actual_length < expected_length:
Expand Down
18 changes: 7 additions & 11 deletions instagrapi/mixins/story.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@ def story_pk_from_url(self, url: str) -> str:
parts = [p for p in path.split("/") if p and p.isdigit()]
return str(parts[0])

# def story_info_gql(self, story_pk: str):
# # GQL havent video_url :-(
# return self.media_info_gql(self, str(story_pk))

def story_info_v1(self, story_pk: str) -> Story:
"""
Get Story by pk or id
Expand All @@ -62,8 +58,7 @@ def story_info_v1(self, story_pk: str) -> Story:
"""
story_id = self.media_id(story_pk)
story_pk, user_id = story_id.split("_")
# result = self.private_request(f"media/{story_pk}/info/")
# story = extract_story_v1(result["items"][0])

stories = self.user_stories_v1(user_id)
for story in stories:
self._stories_cache[story.pk] = story
Expand Down Expand Up @@ -253,23 +248,22 @@ def story_seen(self, story_pks: List[int], skipped_story_pks: List[int] = []):
)

def story_download(
self, story_pk: int, filename: str = "", folder: Path = ""
self, story_pk: str, filename: str = "", folder: Path = ""
) -> Path:
"""
Download story media by media_type
Parameters
----------
story_pk: int
story_pk: str
Returns
-------
Path
Path for the file downloaded
"""
story_pk = int(story_pk)
story = self.story_info(story_pk)
url = story.thumbnail_url if story.media_type == 1 else story.video_url
url = str(story.thumbnail_url if story.media_type == 1 else story.video_url)
return self.story_download_by_url(url, filename, folder)

def story_download_by_url(
Expand Down Expand Up @@ -300,8 +294,10 @@ def story_download_by_url(
)
filename = "%s.%s" % (filename, fname.rsplit(".", 1)[1]) if filename else fname
path = Path(folder) / filename
response = requests.get(url, stream=True, timeout=self.request_timeout)

response = self._send_public_request(url, stream=True, timeout=self.request_timeout)
response.raise_for_status()

with open(path, "wb") as f:
response.raw.decode_content = True
shutil.copyfileobj(response.raw, f)
Expand Down

0 comments on commit 0bc4b76

Please sign in to comment.