|
| 1 | +import requests |
| 2 | +from bs4 import BeautifulSoup |
| 3 | +import os |
| 4 | +from urllib.parse import urljoin |
| 5 | +from collections import defaultdict |
| 6 | + |
| 7 | +BASE_URL = "https://wiki.multitheftauto.com" |
| 8 | +TARGET_URL = f"{BASE_URL}/wiki/Vehicle_IDs" |
| 9 | +OUTPUT_DIR = "vehicle_images" |
| 10 | + |
| 11 | +os.makedirs(OUTPUT_DIR, exist_ok=True) |
| 12 | + |
| 13 | +print("Fetching Vehicles from MTA Wiki...") |
| 14 | + |
| 15 | +response = requests.get(TARGET_URL) |
| 16 | +soup = BeautifulSoup(response.text, "html.parser") |
| 17 | + |
| 18 | +data = defaultdict(lambda: defaultdict(list)) |
| 19 | + |
| 20 | +current_category = None |
| 21 | +current_subcategory = None |
| 22 | + |
| 23 | +for tag in soup.find_all(["h2", "h3", "table"]): |
| 24 | + if tag.name == "h2": |
| 25 | + headline = tag.find("span", class_="mw-headline") |
| 26 | + if headline: |
| 27 | + current_category = headline.text.strip() |
| 28 | + current_subcategory = None |
| 29 | + elif tag.name == "h3": |
| 30 | + headline = tag.find("span", class_="mw-headline") |
| 31 | + if headline: |
| 32 | + current_subcategory = headline.text.strip() |
| 33 | + elif tag.name == "table" and "wikitable" in tag.get("class", []): |
| 34 | + if not current_category: |
| 35 | + continue |
| 36 | + subcat = current_subcategory or "Uncategorized" |
| 37 | + for row in tag.find_all("tr")[1:]: |
| 38 | + cols = row.find_all("td") |
| 39 | + if len(cols) < 3: |
| 40 | + continue |
| 41 | + |
| 42 | + name = cols[0].text.strip() |
| 43 | + try: |
| 44 | + id_ = int(cols[1].text.strip()) |
| 45 | + except ValueError: |
| 46 | + continue |
| 47 | + |
| 48 | + # viewer_link = cols[2].find("a")["href"] if cols[2].find("a") else None |
| 49 | + image_src = cols[2].find("img")["src"] if cols[2].find("img") else None |
| 50 | + |
| 51 | + if image_src: |
| 52 | + image_url = urljoin(BASE_URL, image_src) |
| 53 | + image_path = f"{OUTPUT_DIR}/{id_}.png" |
| 54 | + if not os.path.exists(image_path): |
| 55 | + img_data = requests.get(image_url).content |
| 56 | + with open(image_path, "wb") as f: |
| 57 | + f.write(img_data) |
| 58 | + |
| 59 | + data[current_category][subcat].append({ |
| 60 | + "id": id_, |
| 61 | + "name": name, |
| 62 | + # "image": f"./{OUTPUT_DIR}/{id_}.png", |
| 63 | + # "viewer": viewer_link or "", |
| 64 | + }) |
| 65 | + |
| 66 | +# Export to TypeScript |
| 67 | +lines = ["const vehicles = {"] |
| 68 | +for category, subcats in data.items(): |
| 69 | + lines.append(f' "{category}": {{') |
| 70 | + for subcat, vehicles in subcats.items(): |
| 71 | + lines.append(f' "{subcat}": [') |
| 72 | + for v in vehicles: |
| 73 | + lines.append( |
| 74 | + # f' {{ id: {v["id"]}, name: "{v["name"]}", image: "{v["image"]}", viewer: "{v["viewer"]}" }},' |
| 75 | + f' {{ id: {v["id"]}, name: "{v["name"]}" }},' |
| 76 | + ) |
| 77 | + lines.append(" ],") |
| 78 | + lines.append(" },") |
| 79 | +lines.append("};\n") |
| 80 | + |
| 81 | +with open("vehicles.ts", "w", encoding="utf-8") as f: |
| 82 | + f.write("\n".join(lines)) |
| 83 | + |
| 84 | +print("Done! Vehicle data exported to vehicles.ts and images saved in the vehicle_images directory.") |
0 commit comments