Skip to content

Commit

Permalink
Merge pull request #1724 from DUZA-dev/stories
Browse files Browse the repository at this point in the history
Bugs in download user stories
  • Loading branch information
subzeroid authored Jan 10, 2024
2 parents 814150b + 0bc4b76 commit ac05237
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 @@ -377,6 +377,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 @@ -442,7 +443,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 @@ -301,8 +295,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 ac05237

Please sign in to comment.