Make a script that can do the following: (ALL REQUESTS WILL USE PROXIES): accounts.json…
- User
Make a script that can do the following: (ALL REQUESTS WILL USE PROXIES):
accounts.json file which has all the accounts we will use separate login function that is parallelized (multiprocess) which returns the session token or what ever in order to search
now for searching:
we will have 1 file with the data and we want each account to search parts of the data on uhaul api in chunks. if response 200 we store the raw data the apii returns in .json file - if any other error besides it not being existent we sleep 5-10 seconds and try again (max retires 3)
- Assistant
Here's an example script that implements the functionality you described:
import json import requests import multiprocessing import time # Global variables accounts_file = 'accounts.json' data_file = 'data.txt' proxy_file = 'proxies.txt' output_folder = 'output/' # Load accounts from JSON file def load_accounts(): with open(accounts_file) as file: return json.load(file) # Perform login using an account def login(account, proxy): # Assuming the login request and token retrieval implementation here return account['session_token'] # Search U-Haul API with each account def search(account, token, proxy): retries = 0 while retries < 3: try: # Assuming the search request implementation here response = requests.get('https://api.uhaul.com/search', proxies={"http": proxy, "https": proxy}) if response.status_code == 200: # Store raw API response in a JSON file with open(output_folder + f'{account["username"]}_output.json', 'w') as file: file.write(response.text) print(f'{account["username"]} successfully searched.') else: print(f'{account["username"]} encountered an error: {response.status_code}') retries += 1 time.sleep(5 + retries * 2) # Sleep between retries except requests.exceptions.RequestException as e: print(f'{account["username"]} encountered a network error: {e}') retries += 1 time.sleep(5 + retries * 2) # Sleep between retries # Parallelized login function def parallel_login(account, proxy): token = login(account, proxy) search(account, token, proxy) if __name__ == '__main__': # Load accounts accounts = load_accounts() # Read data file with open(data_file) as file: data = file.read() # Read proxies from file with open(proxy_file) as file: proxies = [line.strip() for line in file] # Create output folder if it doesn't exist os.makedirs(output_folder, exist_ok=True) # Parallelize login and search processes = [] for account, proxy in zip(accounts, proxies): p = multiprocessing.Process(target=parallel_login, args=(account, proxy)) p.start() processes.append(p) # Wait for all processes to finish for p in processes: p.join()Please note that this is just a general template, and you will need to implement the specific login and search functionalities based on the U-Haul API requirements and your account authentication process. Also, make sure to handle any exception scenarios appropriately in your actual implementation.