|
| 1 | +import requests |
| 2 | +import json |
| 3 | +import os |
| 4 | +from urllib.parse import urlparse |
| 5 | + |
| 6 | + |
| 7 | +# Load the domains to exclude from the .env file |
| 8 | +def load_excluded_domains(): |
| 9 | + print("Loading excluded domains from .env file...") |
| 10 | + excluded_domains = [] |
| 11 | + with open('../.env', 'r') as env_file: # Updated path to '../.env' |
| 12 | + for line in env_file: |
| 13 | + if line.startswith("EXCLUDED_DOMAINS"): |
| 14 | + excluded_domains = line.strip().split('=')[1].split(',') |
| 15 | + print(f"Excluded domains loaded: {excluded_domains}") |
| 16 | + break |
| 17 | + return excluded_domains |
| 18 | + |
| 19 | + |
| 20 | +# Find the final URL after following redirects |
| 21 | +def get_final_url(surface_url): |
| 22 | + print(f"Following redirects for surface URL: {surface_url}") |
| 23 | + try: |
| 24 | + response = requests.get(surface_url, allow_redirects=True, timeout=10) |
| 25 | + final_url = response.url |
| 26 | + print(f"Found final URL: {final_url}") |
| 27 | + return final_url |
| 28 | + except requests.RequestException as e: |
| 29 | + print(f"Failed to retrieve final URL for {surface_url}. Error: {e}") |
| 30 | + return None |
| 31 | + |
| 32 | + |
| 33 | +# Extract domain from a URL |
| 34 | +def get_domain(url): |
| 35 | + parsed_url = urlparse(url) |
| 36 | + return parsed_url.netloc |
| 37 | + |
| 38 | + |
| 39 | +# Main function to process the JSON file |
| 40 | +def process_compromised_accounts(): |
| 41 | + print("Starting to process compromised Discord accounts...") |
| 42 | + |
| 43 | + # Load the excluded domains from the .env file |
| 44 | + excluded_domains = load_excluded_domains() |
| 45 | + |
| 46 | + # Load the compromised Discord accounts data from the JSON file |
| 47 | + print("Loading compromised Discord accounts from JSON file...") |
| 48 | + with open('../../data/Compromised-Discord-Accounts.json', 'r') as file: |
| 49 | + data = json.load(file) |
| 50 | + |
| 51 | + # Process each account entry |
| 52 | + updated_count = 0 |
| 53 | + skipped_count = 0 |
| 54 | + for account_key, account_info in data.items(): |
| 55 | + surface_url = account_info.get('SURFACE_URL') |
| 56 | + |
| 57 | + if not surface_url: |
| 58 | + print(f"No surface URL found for account {account_key}, skipping...") |
| 59 | + continue |
| 60 | + |
| 61 | + # Skip if the surface URL domain is in the excluded domains list |
| 62 | + surface_domain = get_domain(surface_url) |
| 63 | + if surface_domain in excluded_domains: |
| 64 | + print(f"Surface URL domain {surface_domain} is excluded. Skipping account {account_key}...") |
| 65 | + skipped_count += 1 |
| 66 | + continue |
| 67 | + |
| 68 | + print(f"Processing account {account_key} with surface URL: {surface_url}") |
| 69 | + |
| 70 | + # Try to get the final URL after redirection |
| 71 | + final_url = get_final_url(surface_url) |
| 72 | + |
| 73 | + if final_url: |
| 74 | + final_domain = get_domain(final_url) |
| 75 | + # Update the account with the final URL and domain |
| 76 | + print(f"Updating account {account_key} with final URL: {final_url} and domain: {final_domain}") |
| 77 | + account_info['FINAL_URL'] = final_url |
| 78 | + account_info['FINAL_URL_DOMAIN'] = final_domain |
| 79 | + updated_count += 1 |
| 80 | + else: |
| 81 | + print(f"No final URL found for account {account_key}, skipping update...") |
| 82 | + |
| 83 | + # Save the updated data back to the JSON file |
| 84 | + print(f"Saving updated data back to JSON file...") |
| 85 | + with open('../../data/Compromised-Discord-Accounts.json', 'w') as file: |
| 86 | + json.dump(data, file, indent=4) |
| 87 | + |
| 88 | + print(f"Processing complete. {updated_count} accounts updated, {skipped_count} accounts skipped.") |
| 89 | + |
| 90 | + |
| 91 | +# Run the script |
| 92 | +if __name__ == "__main__": |
| 93 | + process_compromised_accounts() |
0 commit comments