From dc92bf45633b390f953b2b3edb86468eb6c37d95 Mon Sep 17 00:00:00 2001 From: David Wicker Date: Sat, 5 Dec 2020 11:42:12 +0100 Subject: [PATCH] v2.1.9 --- instaclient/classes/baseprofile.py | 36 ++++++++++++++++++++---------- instaclient/client/notiscraper.py | 10 ++++++--- setup.py | 4 ++-- 3 files changed, 33 insertions(+), 17 deletions(-) diff --git a/instaclient/classes/baseprofile.py b/instaclient/classes/baseprofile.py index 8a4d149..bfe92ff 100644 --- a/instaclient/classes/baseprofile.py +++ b/instaclient/classes/baseprofile.py @@ -1,8 +1,10 @@ -import requests +import requests, logging from instaclient.errors.common import InvalidInstaRequestError, InvalidInstaSchemaError from instaclient.client.urls import GraphUrls from instaclient.classes.instaobject import InstaBaseObject +logger = logging.getLogger(__name__) + class BaseProfile(InstaBaseObject): def __init__(self, id:str, viewer:str, username:str, name:str): id = id.replace('profilePage_', '') @@ -19,20 +21,30 @@ def from_username(username:str): result = requests.get(request) try: data = result.json() + try: + user = data['graphql']['user'] + profile = BaseProfile( + id=user['id'], + viewer=None, + username=user['username'], + name=user['full_name'] + ) + return profile + except: + raise InvalidInstaSchemaError(__name__) except: + logger.error(f'Invalid request. Data: {result.raw}') raise InvalidInstaRequestError(request) - try: - user = data['graphql']['user'] - profile = BaseProfile( - id=user['id'], - viewer=None, - username=user['username'], - name=user['full_name'] - ) - return profile - except: - raise InvalidInstaSchemaError(__name__) + + def username_profile(username:str): + return BaseProfile( + id=None, + viewer=None, + username=username, + name=None + ) + def get_username(self): return self.username diff --git a/instaclient/client/notiscraper.py b/instaclient/client/notiscraper.py index cc47918..7d48639 100644 --- a/instaclient/client/notiscraper.py +++ b/instaclient/client/notiscraper.py @@ -1,4 +1,4 @@ -from instaclient.errors.common import InvalidNotificationTypeError, InvalidInstaSchemaError +from instaclient.errors.common import InvalidInstaRequestError, InvalidNotificationTypeError, InvalidInstaSchemaError import json, requests import types from instaclient.client.urls import GraphUrls @@ -25,7 +25,11 @@ def _scrape_notifications(self, source:int, viewer:int, types:list=None, count:i notifications = [] # Map nodes into Notification Objects - viewer = BaseProfile.from_username(viewer) + try: + viewer = BaseProfile.from_username(viewer) + except InvalidInstaRequestError as error: + self.logger.error(f'InvalidInstaRequestError intercepted. Creating {viewer} profile with username.', exc_info=error) + viewer = BaseProfile.username_profile(viewer) for node in nodes: user = BaseProfile( id=node['user']['id'], @@ -46,7 +50,7 @@ def _scrape_notifications(self, source:int, viewer:int, types:list=None, count:i def __scrape_nodes(self, source:str, types:list, count:int=None): data = json.loads(source) nodes = self.__parse_notifications(data) - self.logger.debug('NODE COUNT:\n{}'.format(len(nodes))) + self.logger.info('NODE COUNT: {}'.format(len(nodes))) selected_nodes = [] for node in nodes: diff --git a/setup.py b/setup.py index 27a0175..8188629 100644 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ setup( name = 'instaclient', # How you named your package folder (MyLib) packages = find_packages(exclude=['tests, drivers']), # Chose the same as "name" - version = '2.1.8', # Start with a small number and increase it with every change you make + version = '2.1.9', # Start with a small number and increase it with every change you make license='MIT', # Chose a license from here: https://help.github.com/articles/licensing-a-repository description = 'Instagram client built with Python 3.8 and the Selenium package.', long_description=README, @@ -19,7 +19,7 @@ author = 'David Wicker', # Type in your name author_email = 'davidwickerhf@gmail.com', # Type in your E-Mail url = 'https://github.com/wickerdevs/py-instaclient', # Provide either the link to your github or to your website - download_url = 'https://github.com/wickerdevs/py-instaclient/archive/v2.1.8.tar.gz', # I explain this later on + download_url = 'https://github.com/wickerdevs/py-instaclient/archive/v2.1.9.tar.gz', # I explain this later on keywords = ['INSTAGRAM', 'BOT', 'INSTAGRAM BOT', 'INSTAGRAM CLIENT'], # Keywords that define your package best install_requires=[ # I get to this in a second 'selenium',