|
| 1 | +import requests |
| 2 | +from bs4 import BeautifulSoup |
| 3 | +import json |
| 4 | +import os |
| 5 | + |
| 6 | +# Color Codes for Printing |
| 7 | +GREEN = "\033[32m" |
| 8 | +YELLOW = "\033[33m" |
| 9 | +BLUE = "\033[34m" |
| 10 | +RED = "\033[31m" |
| 11 | +RESET = "\033[0m" |
| 12 | + |
| 13 | +url = "https://www.useragentstring.com/pages/useragentstring.php?name=All" |
| 14 | + |
| 15 | +try: |
| 16 | + response = requests.get(url) |
| 17 | + response.raise_for_status() # Raise an exception if there's an HTTP error |
| 18 | + soup = BeautifulSoup(response.content, 'html.parser') |
| 19 | + |
| 20 | + user_agents = [] # List to store user agent dictionaries |
| 21 | + group = None # Initialize group value |
| 22 | + sequence = 0 # Initialize sequence value |
| 23 | + seen_combinations = set() # Set to store seen combinations of user-agent and title |
| 24 | + mobile_detected = False # Flag to indicate if mobile user agent has been detected |
| 25 | + |
| 26 | + for tag in soup.find_all(True): |
| 27 | + if tag.name == 'h3': |
| 28 | + group_text = tag.get_text(strip=True) # Get the text from <h3> |
| 29 | + if group_text == "MOBILE BROWSERS": |
| 30 | + host = "Mobile" |
| 31 | + else: |
| 32 | + host = "General" |
| 33 | + group = group_text # Update group value |
| 34 | + |
| 35 | + elif tag.name == 'h4': |
| 36 | + title = tag.get_text(strip=True) # Get the text from <h4> as the title |
| 37 | + |
| 38 | + ul_tag = tag.find_next('ul') # Find the <ul> under <h4> |
| 39 | + a_tags = ul_tag.find_all('a') # Find all <a> tags within the <ul> |
| 40 | + |
| 41 | + for a_tag in a_tags: |
| 42 | + href = a_tag['href'] # Get the 'href' attribute value |
| 43 | + user_agent = a_tag.text # Get the text within the <a> tag |
| 44 | + |
| 45 | + user_agent = user_agent.replace("-->>", "") # Remove "-->>" if present |
| 46 | + |
| 47 | + if "Opera/9.80 (J2ME/MIDP; Opera Mini/4.2.14912Mod.By.www.9jamusic.cz.cc/22.387; U; en)" in user_agent: |
| 48 | + continue # Skip this iteration if the condition is met |
| 49 | + |
| 50 | + host = "Mobile" if mobile_detected else "General" # Set the "Host" value |
| 51 | + |
| 52 | + # Check if the combination of user-agent and title has been seen before |
| 53 | + if (user_agent, title) in seen_combinations: |
| 54 | + continue # Skip this iteration if the combination has been seen |
| 55 | + |
| 56 | + sequence += 1 # Increment the sequence value |
| 57 | + |
| 58 | + ua_dict = { |
| 59 | + "title": title if title != "Opera/9.80 (J2ME/MIDP; Opera Mini/4.2.14912Mod.By.www.9jamusic.cz.cc/22.387; U; en)" else "", |
| 60 | + "group": group if group != "Opera/9.80 (J2ME/MIDP; Opera Mini/4.2.14912Mod.By.www.9jamusic.cz.cc/22.387; U; en)" else "", |
| 61 | + "id": f"ua-{sequence}", |
| 62 | + "user-agent": user_agent if user_agent != "Opera/9.80 (J2ME/MIDP; Opera Mini/4.2.14912Mod.By.www.9jamusic.cz.cc/22.387; U; en)" else "", |
| 63 | + "Host": host |
| 64 | + } |
| 65 | + |
| 66 | + # Check if "Xenu Link Sleuth/1.3.7" is in the user agent |
| 67 | + if "Xenu Link Sleuth/1.3.7" in user_agent: |
| 68 | + mobile_detected = True |
| 69 | + |
| 70 | + user_agents.append(ua_dict) |
| 71 | + seen_combinations.add((user_agent, title)) # Add the combination to the set |
| 72 | + |
| 73 | + # Prompt the user whether to print the data on the screen |
| 74 | + while True: |
| 75 | + print_data = input("Do you want to print the data on the screen? (yes/no): ").lower() |
| 76 | + if print_data in ["yes", "no"]: |
| 77 | + break |
| 78 | + else: |
| 79 | + print(f"{RED}[ERROR]{RESET} Please enter 'yes' or 'no.'") |
| 80 | + |
| 81 | + if print_data == "yes": |
| 82 | + # Print the user agent dictionaries in a pretty format |
| 83 | + print(json.dumps(user_agents, indent=4)) |
| 84 | + else: |
| 85 | + print("Data was not printed on the screen.") |
| 86 | + |
| 87 | + # Count the total number of user-agents before updating |
| 88 | + total_user_agents_before = len(user_agents) |
| 89 | + |
| 90 | + # Prompt the user whether to update the JSON file |
| 91 | + while True: |
| 92 | + update_json = input("Do you want to update the JSON file? (yes/no): ").lower() |
| 93 | + if update_json in ["yes", "no"]: |
| 94 | + break |
| 95 | + else: |
| 96 | + print(f"{RED}[ERROR]{RESET} Please enter 'yes' or 'no.'") |
| 97 | + |
| 98 | + # Calculate statistics for user agents per "Mobile" and "General" host values |
| 99 | + mobile_user_agents = sum(1 for ua in user_agents if ua["Host"] == "Mobile") |
| 100 | + general_user_agents = sum(1 for ua in user_agents if ua["Host"] == "General") |
| 101 | + |
| 102 | + # Count the total number of user-agents after updating |
| 103 | + total_user_agents_after = len(user_agents) |
| 104 | + |
| 105 | + print(GREEN + "[+]" + RESET + " General User Agents: " + BLUE + str(general_user_agents) + RESET) |
| 106 | + print(GREEN + "[+]" + RESET + " Mobile User Agents: " + BLUE + str(mobile_user_agents) + RESET) |
| 107 | + print(GREEN + "[+]" + RESET + " Total User Agents: " + BLUE + str(total_user_agents_after) + RESET) |
| 108 | + |
| 109 | + if update_json == "yes": |
| 110 | + json_file_path = "user_agents.json" # Updated to use default working directory |
| 111 | + |
| 112 | + try: |
| 113 | + # Save the user agent dictionaries to the JSON file |
| 114 | + with open(json_file_path, "w") as json_file: |
| 115 | + json.dump(user_agents, json_file, indent=4) |
| 116 | + print(GREEN + "[+]" + RESET +" JSON file updated successfully.") |
| 117 | + |
| 118 | + # Check if new user agents were identified |
| 119 | + if total_user_agents_after > total_user_agents_before: |
| 120 | + new_user_agents = user_agents[total_user_agents_before:] |
| 121 | + print(GREEN + '[+]' + RESET + " New User Agents Identified: " + BLUE + str(len(new_user_agents)) + RESET) |
| 122 | + for ua in new_user_agents: |
| 123 | + print(json.dumps(ua, indent=4)) |
| 124 | + |
| 125 | + input("Press Enter to acknowledge...") |
| 126 | + else: |
| 127 | + print(f"{YELLOW}[!]{RESET} No new user-agents found.") |
| 128 | + |
| 129 | + except (FileNotFoundError, PermissionError, json.JSONDecodeError) as e: |
| 130 | + print(f"{RED}[ERROR]{RESET} Error while updating JSON file:", e) |
| 131 | + else: |
| 132 | + print(RED + "[x]" + RESET + " JSON file was not updated.") |
| 133 | + |
| 134 | +except requests.exceptions.RequestException as e: |
| 135 | + print(f"{RED}[ERROR]{RESET} Unable to retrieve data from the URL.") |
| 136 | +except (AttributeError, ValueError, BeautifulSoup.exceptions.BeautifulSoupError) as e: |
| 137 | + print(f"{RED}[ERROR]{RESET} Error while parsing HTML:", e) |
| 138 | +except ValueError as e: |
| 139 | + print(f"{RED}[ERROR]{RESET} Invalid input:", e) |
| 140 | +except KeyboardInterrupt: |
| 141 | + print("\nProgram interrupted by user.") |
0 commit comments