Skip to content

Commit

Permalink
Error handling and new option
Browse files Browse the repository at this point in the history
  • Loading branch information
SmartsvXD committed Sep 22, 2024
1 parent 5ec6434 commit e18e143
Showing 1 changed file with 33 additions and 16 deletions.
49 changes: 33 additions & 16 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@

def loadJSONs():
success = True

followers, followings, whitelist = None, None, None

#Open followers and following jsons.
try:
with open("./followers_1.json") as file:
followers = json.load(file)
followers = list(follower["string_list_data"][0]['value'] for follower in json.load(file))
except FileNotFoundError:
error("You need to have \"followers_1.json\" on the same folder as the executable. For more, read the README.")
success = False
Expand All @@ -30,7 +29,7 @@ def loadJSONs():

try:
with open("./following.json") as file:
followings = json.load(file)
followings = list(following["string_list_data"][0]['value'] for following in json.load(file)['relationships_following'])
except FileNotFoundError:
error("You need to have \"following.json\" on the same folder as the executable. For more, read the README.")
success = False
Expand All @@ -41,14 +40,20 @@ def loadJSONs():
#Open or create the whitelist json
try:
with open("./whitelist.json") as file:
whitelist = list(tag.lower() for tag in json.load(file))
data = json.load(file)
if type(data) != list:
error("The file \"whitelist.json\" is not formatted correctly. It should be a list.")
success = False
else:
whitelist = list(tag for tag in data)
except FileNotFoundError:
with open("./whitelist.json", "w") as file:
file.write("[\n\t\"INSERT_HERE_THE_ACCOUNTS_YOU_DON'T_WANNA_CHECK_ON\"\n]")
info("The whitelist file has been created. Write in \"whitelist.json\" the accounts you don't wanna check on.\n")
info("whitelist.json has been created. Write in \"whitelist.json\" the accounts you don't wanna check on.\n")
with open("./whitelist.json") as file:
whitelist = list(tag.lower() for tag in json.load(file))
except json.JSONDecodeError:
whitelist = list(tag for tag in json.load(file))
except json.JSONDecodeError as e:
print(e)
error("The file \"whitelist.json\" is not formatted correctly.")
success = False

Expand All @@ -60,25 +65,37 @@ def compareLists():
if not success:
return

#Prepare lists to compare
tagFollowers = list(follower["string_list_data"][0]['value'] for follower in followers)
tagFollowings = list(following["string_list_data"][0]['value'] for following in followings['relationships_following'])

#Compare lists
nUnfollorers = 0
for tagFollowing in tagFollowings:
if tagFollowing not in tagFollowers and tagFollowing.lower() not in whitelist:
for following in followings:
if following not in followers and following.lower() not in tuple(user.lower() for user in whitelist):
nUnfollorers += 1
print(f"{(len(str(len(tagFollowings)))-len(str(nUnfollorers))) * ' '}{nUnfollorers}. {Fore.RED}{Style.BRIGHT}{tagFollowing.upper()}{Style.RESET_ALL} {(len(max(tagFollowings))+2-len(tagFollowing)) * ' '} doesn't follow you back.")
print(f"{(len(str(len(followings)))-len(str(nUnfollorers))) * ' '}{nUnfollorers}. {Fore.RED}{Style.BRIGHT}{following.upper()}{Style.RESET_ALL} {(len(max(followings))+2-len(following)) * ' '} doesn't follow you back.")

if nUnfollorers == 0:
printColored("Congotulations! Everybody follows you back.", Fore.GREEN)

def printWhitelist():
whitelist, success = loadJSONs()[2:]

if not success:
return

if "INSERT_HERE_THE_ACCOUNTS_YOU_DON'T_WANNA_CHECK_ON" in whitelist:
whitelist.remove("INSERT_HERE_THE_ACCOUNTS_YOU_DON'T_WANNA_CHECK_ON")

if len(whitelist) == 0:
info("The whitelist is empty")
else:
for i in whitelist:
print(f" - {Fore.GREEN}{Style.BRIGHT}{i}{Style.RESET_ALL}")

def about():
printColored("Code written by Smartsv (SmartsvXD on Github)\n\nItaly, 2024", Fore.GREEN)

actions = {
"Find unfollowers": compareLists,
"Print whitelist": printWhitelist,
"About": about,
f"{Fore.RED}Exit{Fore.RESET}": exit
}
Expand All @@ -89,7 +106,7 @@ def about():

#Print all the actions
for n, i in enumerate(actions.keys()):
print(f"{n+1}. {Style.BRIGHT}{i}{Style.RESET_ALL}")
print(f" {n+1}. {Style.BRIGHT}{i}{Style.RESET_ALL}")
print()

#Print something if the previrous input was not valid
Expand All @@ -103,5 +120,5 @@ def about():
clearCLI()
tuple(actions.values())[selection]()
input(f"{Fore.YELLOW}{Style.DIM}\n(Press ENTER to go back to menu){Style.RESET_ALL}")
except (IndexError, ValueError):
except (IndexError, ValueError) as e:
prevInputInvalid = 1

0 comments on commit e18e143

Please sign in to comment.