cleanup, fixed handling connection error exception

pull/12/head
Master 6 years ago
parent 930ffd19db
commit b59dc55631

@ -1,18 +1,12 @@
import requests
import json
import os import os
import json
import requests
# TODO: fix tumblr # TODO: fix tumblr
def write_to_file(url, fname): def print_banner():
with open(fname, "a") as f: print(" .\"\"\"-.")
f.write(url+"\n") print(" / \\")
def main():
# Not sure why, but the banner messes up if i put into one print function
print("\033[37;1m .\"\"\"-.")
print("\033[37;1m / \\")
print("\033[37;1m ____ _ _ _ | _..--'-.") print("\033[37;1m ____ _ _ _ | _..--'-.")
print("\033[37;1m/ ___|| |__ ___ _ __| | ___ ___| |__ >.`__.-\"\"\;\"`") print("\033[37;1m/ ___|| |__ ___ _ __| | ___ ___| |__ >.`__.-\"\"\;\"`")
print("\033[37;1m\___ \| '_ \ / _ \ '__| |/ _ \ / __| |/ / / /( ^\\") print("\033[37;1m\___ \| '_ \ / _ \ '__| |/ _ \ / __| |/ / / /( ^\\")
@ -21,67 +15,80 @@ def main():
print("\033[37;1m .'`-._ `.\ | J /") print("\033[37;1m .'`-._ `.\ | J /")
print("\033[37;1m / `--.| \__/\033[0m") print("\033[37;1m / `--.| \__/\033[0m")
username = input("\033[92;1m[\033[37;1m?\033[92;1m]\033[92;1m Input Username: \033[0m") def search_accounts(username, social_networks_params):
print() existing_accounts = list()
fname = username+".txt" headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:55.0) Gecko/20100101 Firefox/55.0'
}
if os.path.isfile(fname): for social_network in social_networks_params:
os.remove(fname) url = social_networks_params.get(social_network).get("url").format(username)
print("\033[1;92m[\033[0m\033[1;77m*\033[0m\033[1;92m] Removing previous file:\033[1;37m {}\033[0m".format(fname)) error_type = social_networks_params.get(social_network).get("errorType")
try:
response = requests.get(url, headers=headers)
except Exception as e:
print(f"error connecting to {url}: {e}")
continue
if error_type == "message":
error = social_networks_params.get(social_network).get("errorMsg")
if error in response.text:
print(f"\033[37;1m[\033[91;1m-\033[37;1m]\033[92;1m {social_network}:\033[93;1m Not Found!")
continue
print("\033[1;92m[\033[0m\033[1;77m*\033[0m\033[1;92m] Checking username\033[0m\033[1;37m {}\033[0m\033[1;92m on: \033[0m".format(username)) elif error_type == "status_code":
raw = open("data.json", "r", encoding="utf-8") if response.status_code == 404:
data = json.load(raw) print(f"\033[37;1m[\033[91;1m-\033[37;1m]\033[92;1m {social_network}:\033[93;1m Not Found!")
continue
# User agent is needed because some sites does not elif error_type == "response_url":
# return the correct information because it thinks that error = social_networks_params.get(social_network).get("errorUrl")
# we are bot if error in response.url:
headers = { print(f"\033[37;1m[\033[91;1m-\033[37;1m]\033[92;1m {social_network}:\033[93;1m Not Found!")
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:55.0) Gecko/20100101 Firefox/55.0' continue
}
for social_network in data: print(f"\033[37;1m[\033[92;1m+\033[37;1m]\033[92;1m {social_network}:\033[0m", url)
url = data.get(social_network).get("url").format(username) existing_accounts.append(url)
error_type = data.get(social_network).get("errorType")
cant_have_period = data.get(social_network).get("noPeriod") return existing_accounts
if cant_have_period == "True": def save_account_urls(account_urls, filename):
print("\033[37;1m[\033[91;1m-\033[37;1m]\033[92;1m {}:\033[93;1m Not Found!".format(social_network)) with open(filename, "a") as file:
continue file.writelines(account_urls)
r = requests.get(url, headers=headers) def remove_old_file(filename):
if os.path.isfile(filename):
os.remove(filename)
print(f"\033[1;92m[\033[0m\033[1;77m*\033[0m\033[1;92m] Removing previous file:\033[1;37m {filename}\033[0m")
if error_type == "message": def get_social_networks_params():
error = data.get(social_network).get("errorMsg") with open("data.json", "r") as file:
# Checks if the error message is in the HTML social_networks_params = json.load(file)
if not error in r.text: return social_networks_params
print("\033[37;1m[\033[92;1m+\033[37;1m]\033[92;1m {}:\033[0m".format(social_network), url)
write_to_file(url, fname)
else: def main():
print("\033[37;1m[\033[91;1m-\033[37;1m]\033[92;1m {}:\033[93;1m Not Found!".format(social_network)) print_banner()
username = input("\033[92;1m[\033[37;1m?\033[92;1m]\033[92;1m Input Username: \033[0m")
print()
elif error_type == "status_code": filename = f"{username}.txt"
# Checks if the status code of the repsonse is 404 remove_old_file(filename)
if not r.status_code == 404:
print("\033[37;1m[\033[92;1m+\033[37;1m]\033[92;1m {}:\033[0m".format(social_network), url)
write_to_file(url, fname)
else: social_networks_params = get_social_networks_params()
print("\033[37;1m[\033[91;1m-\033[37;1m]\033[92;1m {}:\033[93;1m Not Found!".format(social_network)) if social_networks_params is None:
print("Error loading social networks parameters. Exiting.")
return
elif error_type == "response_url": print(f"\033[1;92m[\033[0m\033[1;77m*\033[0m\033[1;92m] Checking username\033[0m\033[1;37m {username}\033[0m\033[1;92m on: \033[0m")
error = data.get(social_network).get("errorUrl")
# Checks if the redirect url is the same as the one defined in data.json existing_accounts = search_accounts(username, social_networks_params)
if not error in r.url: if len(existing_accounts) == 0:
print("\033[37;1m[\033[92;1m+\033[37;1m]\033[92;1m {}:\033[0m".format(social_network), url) print("No accounts found. Exiting without saving.")
write_to_file(url, fname) return
else:
print("\033[37;1m[\033[91;1m-\033[37;1m]\033[92;1m {}:\033[93;1m Not Found!".format(social_network))
print("\033[1;92m[\033[0m\033[1;77m*\033[0m\033[1;92m] Saved: \033[37;1m{}\033[0m".format(username+".txt")) save_account_urls(existing_accounts, filename)
print(f"\033[1;92m[\033[0m\033[1;77m*\033[0m\033[1;92m] Saved: \033[37;1m{filename}\033[0m")
main() if '__main__' in __name__:
main()
Loading…
Cancel
Save