result = api.lookup(query=queryprefix, querytype="keyword", limit=1000, offset=offset)…
- User
result = api.lookup(query=query_prefix, query_type="keyword", limit=1000, offset=offset) uniqueEmails = {item['email'] for item in result if 'email' in item} input(len(uniqueEmails))
codeemail_dict = {email.split('@')[0]: email.split('@')[1] for email in uniqueEmails} for email_prefix in email_dict.keys(): if len(email_prefix) == emailLength: if email_dict[email_prefix] == domain: full_email = f"{email_prefix}@{email_dict[email_prefix]}" num_matched += 1 print(full_email) with open(f"{username}.txt", 'a') as file: file.write(full_email + "\n\n") if num_matched != 0: os.startfile(f"{username}.txt") print(f"{num_matched} emails saved to {username}.txt") else: print(f"{num_matched} emails found")Can you put all of this into a function where you pass the "result" set to the function?
- Assistant
Certainly! Below is a refactored version of the provided code, encapsulated within a function called
process_emails. This function takes , , , and as parameters and processes the email data as specified: - User
without code comments, or explanation
- Assistantpython
import os def process_emails(result, emailLength, domain, username): uniqueEmails = {item['email'] for item in result if 'email' in item} print(f"Number of unique emails found: {len(uniqueEmails)}") email_dict = {email.split('@')[0]: email.split('@')[1] for email in uniqueEmails} num_matched = 0 for email_prefix in email_dict.keys(): if len(email_prefix) == emailLength and email_dict[email_prefix] == domain: full_email = f"{email_prefix}@{email_dict[email_prefix]}" num_matched += 1 print(full_email) with open(f"{username}.txt", 'a') as file: file.write(full_email + "\n\n") if num_matched != 0: os.startfile(f"{username}.txt") print(f"{num_matched} emails saved to {username}.txt") else: print(f"{num_matched} emails found") # Example usage # result = api.lookup(query=query_prefix, query_type="keyword", limit=1000, offset=offset) # process_emails(result, emailLength, domain, username) - User
with open(f"{username}.txt", 'a') as file: file.write(full_email + "\n\n")
Change this so if the full_email line exists in the text file, it wont save to it?
- Assistantpython
- User
with open(f"{username}.txt", 'a') as file: file.write(full_email + "\n\n")
as simple as possible, send only the relevant code. the file will always exist so yeah
- Assistantpython
with open(f"{username}.txt", 'r') as file: if full_email + "\n\n" not in file.readlines(): with open(f"{username}.txt", 'a') as file: file.write(full_email + "\n\n") - User
if not os.path.isfile(f"{username}.txt"): with open(f"{username}.txt", 'a') as file: file.write(full_email + "\n\n") else: with open(f"{username}.txt", 'r') as file: if full_email + "\n\n" not in file.readlines(): with open(f"{username}.txt", 'a') as file: file.write(full_email + "\n\n")
This still printed duplicate lines.
- Assistant
To avoid printing duplicate lines when checking if an email already exists in the text file, you can store the existing entries in a set. This approach allows you to quickly check for duplicates before writing to the file. Here’s a simplified version of the code:
pythonwith open(f"{username}.txt", 'r') as file: existing_emails = {line.strip() for line in file} if full_email not in existing_emails: with open(f"{username}.txt", 'a') as file: file.write(full_email + "\n\n")This code reads all the existing email lines into a set, which allows for O(1) average time complexity for membership checking, ensuring no duplicates are written to the file.