Skip to content

Commit

Permalink
Fix instagram media url lookup (#17)
Browse files Browse the repository at this point in the history
* Fix instagram media url lookup

* Remove logger

* ignore c901
  • Loading branch information
amadejkastelic authored Oct 13, 2024
1 parent c505b29 commit e6e23be
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions bot/integrations/instagram/aiograpi/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,12 @@ async def get_post(self, url: str) -> domain.Post:
await self.login()
except aiograpi_exceptions.LoginRequired:
await self.login(relogin=True)
except aiograpi_exceptions.ReloginAttemptExceeded:
await self.login()

return await self._get_post(url)

async def _get_post(self, url: str) -> domain.Post:
async def _get_post(self, url: str) -> domain.Post: # noqa: C901
_, pk, _ = await self.get_integration_data(url)

if 'stories' in url:
Expand All @@ -67,12 +69,24 @@ async def _get_post(self, url: str) -> domain.Post:
created=media_info.taken_at,
)

media_url = None
if media_info.video_url:
post.buffer = await self._download(url=str(media_info.video_url), cookies=self.client.cookie_dict)
media_url = str(media_info.video_url)
elif len(media_info.video_versions) > 0:
media_url = media_info.video_versions[0].get('url')
elif len(media_info.resources) > 0:
for resource in media_info.resources:
if resource.video_url:
media_url = resource.video_url
break
for resource in media_info.resources:
if len(resource.video_versions) > 0:
media_url = resource.video_versions[0].get('url')
elif len(media_info.image_versions2.get('candidates', [])) > 0:
post.buffer = await self._download(
url=media_info.image_versions2['candidates'][0]['url'], cookies=self.client.cookie_dict
)
media_url = media_info.image_versions2['candidates'][0]['url']

if media_url:
post.buffer = await self._download(url=media_url, cookies=self.client.cookie_dict)

return post

Expand Down

0 comments on commit e6e23be

Please sign in to comment.