Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def locate_txt_files(path=os.getcwd):
- """ A function that locates and returns a list of txt files from a given path """
- txt_array = []
- for item in os.listdir(path()):
- if item.endswith(".txt"):
- txt_array.append(item)
- return txt_array
- def open_and_prepare_urls(txt_array):
- """ A function that takes in a list of txt files, open each file, split("\n") and returns
- a list of cleaned domains without trailing "\n" """
- domains = []
- for item in txt_array:
- with open(item) as f:
- content = f.readlines()
- domains.extend(content)
- domains = [domain.strip() for domain in domains]
- return domains
- def open_and_prepare_urls2(txt_file_array):
- final_domains = []
- for txt_file in txt_file_array:
- with open(txt_file, encoding="utf-8") as f:
- content = f.read()
- content = content.split("au")[0:]
- domain_urls = [item.replace("|", "").replace("\n", "").replace(",", "").strip()+"au" for item in content if "." in item]
- final_domains.extend(domain_urls)
- print(f"Total of {len(domain_urls)} '.au' domains found in {txt_file}")
- print("\n")
- return final_domains
- def make_dict(array):
- """ A function that makes a dictionary ready to be fed to pandas """
- a = []
- b = []
- for item in array:
- a.append(item[0])
- b.append(item[1])
- my_dict = {"Domain":pd.Series(a), "Phone Number":pd.Series(b)}
- return my_dict
- def make_dict2(result_array, filename="results.csv", path=os.getcwd):
- """ A function that updates an exixting csv file (look at make_dataframe function) """
- df1 = pd.read_csv(filename)
- domains = df1["Domain"].tolist()
- phones = df1["Phone Number"].tolist()
- for item in result_array:
- domains.append(item[0])
- phones.append(item[1])
- my_dict = {"Domain":pd.Series(domains), "Phone Number":pd.Series(phones)}
- return my_dict
- def make_dataframe(dic, filename="results.csv", path=os.getcwd):
- """" A function that takes in a dict and makes a pd dataframe """
- try:
- df__ = pd.DataFrame(dic)
- df__ = df__.drop_duplicates()
- return df__
- except Exception as e:
- #print(e)
- pass
- def save_dataframe(data_f):
- """ A function that takes in a pandas dataframe and saves it as a csv file """
- data_f.to_csv("results.csv", index=False)
- print("Done")
- def check_for_04(arg):
- arg = str(arg).replace(" ", "").replace("(", "").replace("-", "").replace(".", "").strip()
- if arg.startswith("04") or arg.startswith("4"):
- return True
- else:
- return False
- def create_04(filename="results.csv", dest_filename="results_04.csv"):
- """ create phones that start with 04 """
- a = []
- b = []
- try:
- df2 = pd.read_csv(dest_filename)
- domains_ = df1["Domain"].tolist()
- phones_ = df1["Phone Number"].tolist()
- except:
- pass
- df1 = pd.read_csv(filename)
- domains = df1["Domain"].tolist()
- phones = df1["Phone Number"].tolist()
- try:
- domains.extend(domains_)
- phones.extend(phones_)
- except:
- pass
- for index, phone in enumerate(phones):
- if check_for_04(phone):
- a.append(phones[index])
- b.append(domains[index])
- del phones[index]
- del domains[index]
- my_dict = {"Domain":pd.Series(b), "Phone Number":pd.Series(a)}
- dframe = make_dataframe(dic=my_dict)
- my_dict2 = {"Domain":pd.Series(domains), "Phone Number":pd.Series(phones)}
- df_2 = make_dataframe(dic=my_dict2)
- save_dataframe(df_2)
- dframe.to_csv(dest_filename, index=False)
- print("Created 04..phone numbers!")
- def main():
- #result = [] #list of tuple containing domain and phone
- if filename in os.listdir(path()):
- dict_ = make_dict2(result_array=results)
- dataframe = make_dataframe(dic=dict_)
- save_dataframe(data_f=dataframe)
- create_04()
- else:
- dic = make_dict(array=results)
- dataframe2 = make_dataframe(dic=dic)
- save_dataframe(data_f=dataframe2)
- create_04()
- print("Done!")
Add Comment
Please, Sign In to add comment