Skip to content

Commit

Permalink
Introduce BaseFormat class and report generation
Browse files Browse the repository at this point in the history
BaseFormat has a decorator to provide common report formatting capabilities for generated report sections. Strings have been extracted to constants.
  • Loading branch information
mohoromitch committed Aug 30, 2020
1 parent c90cde8 commit c029ed9
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 10 deletions.
17 changes: 17 additions & 0 deletions clients/baseclient.py
Original file line number Diff line number Diff line change
@@ -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
41 changes: 39 additions & 2 deletions clients/instaClient.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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:
Expand Down
9 changes: 1 addition & 8 deletions dispatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}!")

Expand Down

0 comments on commit c029ed9

Please sign in to comment.