Skip to content

Commit

Permalink
Instagram profile support
Browse files Browse the repository at this point in the history
  • Loading branch information
amadejkastelic committed Sep 13, 2023
1 parent d887862 commit fbd7bb2
Showing 1 changed file with 38 additions and 6 deletions.
44 changes: 38 additions & 6 deletions downloader/instagram.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import enum
import io
import os
import typing
Expand All @@ -10,6 +11,20 @@
from models import post


class LinkType(enum.Enum):
MEDIA = 1
PROFILE = 2
STORY = 3

@classmethod
def from_url(cls, url: str) -> typing.Self:
if '/p/' in url or '/reel/' in url:
return cls.MEDIA
if '/stories/' in url or '/story/' in url:
return cls.STORY
return cls.PROFILE


class InstagramClientSingleton(object):
INSTANCE: typing.Optional[instaloader.Instaloader] = None

Expand Down Expand Up @@ -37,16 +52,21 @@ def __init__(self, url: str):
parsed_url = urlparse(url)
self.id = parsed_url.path.strip('/').split('/')[-1]
self.index = int(parse_qs(parsed_url.query).get('img_index', ['1'])[0]) - 1
self._is_story = '/stories/' in url
self._link_type = LinkType.from_url(url=url)

async def get_post(self) -> post.Post:
if self._is_story:
return self._get_story()
match self._link_type:
case LinkType.STORY:
return self._get_story()
case LinkType.MEDIA:
return self._get_post()
case LinkType.PROFILE:
return self._get_profile()

return self._get_post()
raise NotImplementedError(f'Not yet implemented for {self.url}')

def _get_post(self) -> post.Post:
p = instaloader.Post.from_shortcode(self.client.context, self.id)
p = instaloader.Post.from_shortcode(context=self.client.context, shortcode=self.id)

match p.typename:
case 'GraphImage':
Expand All @@ -72,7 +92,7 @@ def _get_post(self) -> post.Post:
)

def _get_story(self) -> post.Post:
story = instaloader.StoryItem.from_mediaid(self.client.context, int(self.id))
story = instaloader.StoryItem.from_mediaid(context=self.client.context, mediaid=int(self.id))
if story.is_video:
url = story.video_url
else:
Expand All @@ -86,3 +106,15 @@ def _get_story(self) -> post.Post:
buffer=io.BytesIO(resp.content),
created=story.date_local,
)

def _get_profile(self) -> post.Post:
profile = instaloader.Profile.from_username(context=self.client.context, username=self.id)

with requests.get(url=profile.profile_pic_url) as resp:
return post.Post(
url=self.url,
author=profile.username,
description=profile.biography,
buffer=io.BytesIO(resp.content),
likes=profile.followers,
)

0 comments on commit fbd7bb2

Please sign in to comment.