From c029ed93e6b11d5f93c47d396da7d9c3cc014235 Mon Sep 17 00:00:00 2001 From: Mitchell Mohorovich Date: Sat, 29 Aug 2020 22:23:37 -0400 Subject: [PATCH] Introduce BaseFormat class and report generation BaseFormat has a decorator to provide common report formatting capabilities for generated report sections. Strings have been extracted to constants. --- clients/baseclient.py | 17 +++++++++++++++++ clients/instaClient.py | 41 +++++++++++++++++++++++++++++++++++++++-- dispatch.py | 9 +-------- 3 files changed, 57 insertions(+), 10 deletions(-) create mode 100644 clients/baseclient.py diff --git a/clients/baseclient.py b/clients/baseclient.py new file mode 100644 index 0000000..d438dee --- /dev/null +++ b/clients/baseclient.py @@ -0,0 +1,17 @@ +from abc import ABCMeta, abstractmethod + +LINES_BEFORE_SECTION = "\n\n" +LINES_AFTER_SECTION = "\n\n" + + +def formatted_section(func): + def wrapper(self): + return LINES_BEFORE_SECTION + func(self) + LINES_AFTER_SECTION + return wrapper + + +class BaseClient(metaclass=ABCMeta): + + @abstractmethod + def generate_report(self): + pass diff --git a/clients/instaClient.py b/clients/instaClient.py index 296cafc..0635f57 100644 --- a/clients/instaClient.py +++ b/clients/instaClient.py @@ -1,8 +1,16 @@ +from pprint import pformat from InstagramAPI import InstagramAPI -from clients.client import Client +from clients.baseclient import BaseClient, formatted_section +FOLLOWERS_SECTION_TITLE = "Your followers:\n" +FOLLOWING_SECTION_TITLE = "You're following:\n" +FOLLOWERS_NOT_FOLLOWING_BACK = "Who's following you " +"that you're not following back:\n" +FOLLOWING_NOT_FOLLOWING_BACK = "Who you're following " +"that is not following back:" -class InstaClient: + +class InstaClient(BaseClient): def __init__(self, username, password): """Initialize the client Note, this attempts to log into the underlying client API @@ -14,6 +22,35 @@ def __init__(self, username, password): self.client.login() + def generate_report(self) -> str: + rs = [] + rs.append(self.__report_followers_section()) + rs.append(self.__report_following_section()) + rs.append(self.__report_followers_not_following_back()) + rs.append(self.__report_following_not_following_back()) + + return ''.join(rs) + + @formatted_section + def __report_followers_section(self) -> str: + return FOLLOWERS_SECTION_TITLE + pformat( + self.get_followers_usernames()) + + @formatted_section + def __report_following_section(self) -> str: + return FOLLOWING_SECTION_TITLE + pformat( + self.get_following_usernames()) + + @formatted_section + def __report_followers_not_following_back(self) -> str: + return FOLLOWERS_NOT_FOLLOWING_BACK + pformat( + self.get_followers_not_following_back()) + + @formatted_section + def __report_following_not_following_back(self) -> str: + return FOLLOWING_NOT_FOLLOWING_BACK + pformat( + self.get_following_not_following_back()) + def get_followers_usernames(self): """return the account's followers, lazy loaded""" if not self.followers: diff --git a/dispatch.py b/dispatch.py index 693f559..ff8f96c 100644 --- a/dispatch.py +++ b/dispatch.py @@ -11,14 +11,7 @@ def run_ig(app, username, password): try: print("Attempting logging in as %s..." % username) instaClient = InstaClient(username, password) - print("Your followers:") - pprint(instaClient.get_followers_usernames()) - print("You're following:") - pprint(instaClient.get_following_usernames()) - print("Who's following you that you're not following back:") - pprint(instaClient.get_followers_not_following_back()) - print("Who you're following that is not following back:") - pprint(instaClient.get_following_not_following_back()) + print(instaClient.generate_report()) except Exception: return (f"Couldn't log into {app}!")