This repository has been archived by the owner on Nov 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
161 lines (132 loc) · 5.5 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import requests
import os
import re
import argparse
import time
import string
from rich.console import Console
console = Console(highlight=False)
def cprint(color, content):
console.print(f"[bold {color}]{content}[/bold {color}]")
# Function to download and save the XML file
def download_xml(clothing_id):
url = f'https://assetdelivery.roblox.com/v1/asset/?id={clothing_id}'
response = requests.get(url)
if response.status_code == 200:
# Create a directory named 'xml_temp' if it doesn't exist
if not os.path.exists('xml_temp'):
os.mkdir('xml_temp')
with open(f'xml_temp/{clothing_id}.xml', 'wb') as file:
file.write(response.content)
cprint('green', f'Successfully downloaded temporary file: {clothing_id}.xml')
else:
cprint('red', f'Failed to download temporary file: {clothing_id}')
# Function to extract the new ID from the XML file
def extract_new_id(xml_file_path):
with open(xml_file_path, 'r') as file:
xml_content = file.read()
# Use regex to extract the new ID from the <url> tag
match = re.search(r'<url>.*\?id=(\d+)</url>', xml_content)
if match:
return match.group(1)
else:
return None
# Function to get the name of the item using ITEM_ID with retries
def get_item_name(item_id):
while True:
url = f'https://economy.roblox.com/v2/assets/{item_id}/details'
response = requests.get(url)
if response.status_code == 200:
data = response.json()
name = data.get('Name', 'Unknown')
return name
else:
cprint('yellow', f'Failed to get item name for ITEM_ID: {item_id}, Retrying...')
time.sleep(2)
# Function to sanitize a string
def sanitize_filename(name):
valid_chars = f"-_.() {string.ascii_letters}{string.digits}"
return ''.join(char for char in name if char in valid_chars)
# Function to avoid overwriting
def add_suffix_if_exists(file_name):
base_name, ext = os.path.splitext(file_name)
index = 1
while os.path.exists(file_name):
file_name = f'{base_name}_{index}{ext}'
index += 1
return file_name
def download_clothing_image(clothing_id, new_id):
# Get the name of the item using new_id, fallback to clothing_id
item_name = get_item_name(new_id)
if item_name == 'Unknown':
item_name = clothing_id
item_name = sanitize_filename(item_name)
file_name = f'clothes/{item_name}.png'
file_name = add_suffix_if_exists(file_name)
url = f'https://assetdelivery.roblox.com/v1/asset/?id={new_id}'
response = requests.get(url)
if response.status_code == 200:
# Create a directory named 'clothes' if it doesn't exist
if not os.path.exists('clothes'):
os.mkdir('clothes')
# Save the image as a PNG file with the sanitized item name
with open(file_name, 'wb') as file:
file.write(response.content)
cprint('green', f'Successfully downloaded {file_name}')
else:
cprint('red', f'Failed to download {file_name}')
if not os.path.exists('xml_temp'):
os.mkdir('xml_temp')
# Set up command-line argument parsing
parser = argparse.ArgumentParser(description='Download.')
parser.add_argument('file', nargs='?', help='optional')
args = parser.parse_args()
if args.file:
# Check if the argument is a clothing ID
if args.file.isdigit():
clothing_id = args.file
download_xml(clothing_id)
xml_file_path = f'xml_temp/{clothing_id}.xml'
new_id = extract_new_id(xml_file_path)
if new_id:
download_clothing_image(clothing_id, new_id)
os.remove(xml_file_path)
else:
cprint('red', f'Failed to extract new ID from {clothing_id}.xml')
else:
with open(args.file, 'r') as file:
lines = file.readlines()
os.system("cls")
for line in lines:
if line.startswith('https://www.roblox.com/catalog/'):
match = re.search(r'/(\d+)/', line)
if match:
clothing_id = match.group(1)
else:
cprint('red', f'Failed to extract clothing ID from URL: {line}')
continue
else:
clothing_id = line.strip() # Assuming each line in the file is a clothing ID
download_xml(clothing_id)
xml_file_path = f'xml_temp/{clothing_id}.xml'
new_id = extract_new_id(xml_file_path)
if new_id:
download_clothing_image(clothing_id, new_id)
os.remove(xml_file_path)
else:
cprint('red', f'Failed to extract new ID from {clothing_id}.xml')
else:
clothing_id = input('Enter the clothing ID or URL: ')
if clothing_id.startswith('https://www.roblox.com/catalog/'):
match = re.search(r'/(\d+)/', clothing_id)
if match:
clothing_id = match.group(1)
else:
cprint('red', 'Failed to extract clothing ID from the provided URL.')
download_xml(clothing_id)
xml_file_path = f'xml_temp/{clothing_id}.xml'
new_id = extract_new_id(xml_file_path)
if new_id:
download_clothing_image(clothing_id, new_id)
os.remove(xml_file_path) # Change this if you want to keep the temporary XML files
cprint('cyan', 'Finished downloading clothing items.')