Skip to content
This repository has been archived by the owner on Jul 19, 2023. It is now read-only.

Commit

Permalink
Bug 30: Profile.from_username() login required. fix fernandod1#30
Browse files Browse the repository at this point in the history
  • Loading branch information
jim60105 committed Jun 25, 2022
1 parent 5e7f8c9 commit 750dda5
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/scraper.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from instaloader.exceptions import LoginRequiredException
import sys
import requests
import time
from types import NoneType
Expand All @@ -17,12 +18,16 @@ def __init__(self, username: str, loader: Loader):

def __get_profile(self) -> Profile:
try:
# Use a new loader to get posts that do not require login.
self.profile = Profile.from_username(instaloader.Instaloader().context, self.username)
self.profile = Profile.from_username(
self.loader.context, self.username)
return self.profile
except requests.exceptions.ConnectionError:
time.sleep(10)
return self.__get_profile()
except instaloader.LoginRequiredException as e:
print(e, file=sys.stderr)
print(f'Instagram requires login when fetching the profile. This sometimes happens, not sure how often.', file=sys.stderr)
return self.profile

def get_last_post(self) -> Post | NoneType:
return next(self.get_posts(), None)
Expand Down

0 comments on commit 750dda5

Please sign in to comment.