Created
February 24, 2024 09:52
-
-
Save skwashd/a2a33eeceb806317e89e11d8a4ad5274 to your computer and use it in GitHub Desktop.
Script for turning a list of LEGO entityIDs into a BrickLink wishlist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Map lego entity IDs to BrickLink part numbers and colour code. Output them | |
as BrickLink XML. Copy and paste the output into your wish list. | |
This script only uses core Python, with no external dependencies. | |
""" | |
import http.client | |
import random | |
import re | |
import time | |
# Update this with your list of entity IDs | |
data = """ | |
123456 | |
234567 (8) | |
345678 | |
""" | |
BRICKLINK = "www.bricklink.com" | |
ENTITY_ID_REGEX = re.compile(r"(\d{6,7})(\s+\((\d+)\))?") | |
ITEM_REGEX = re.compile(r",\\titemno:\\t\\t\\t\\'(\w+)\\'") | |
REDIRECT_REGEX = re.compile(r"id=(\d+)&idColor=(\d+)&") | |
def get_entity_to_item(entity_id): | |
path = f"/v2/catalog/catalogitem.page?ccName={entity_id}" | |
client = http.client.HTTPSConnection(BRICKLINK) | |
client.request("GET", path) | |
response = client.getresponse() | |
location = [val for header, val in response.getheaders() if header == "Location"][0] | |
response.read() | |
matches = REDIRECT_REGEX.search(location) | |
colour = matches[2] | |
time.sleep(random.randrange(100, 5000) / 1000) | |
new_path = f"/{location.split('/', 3)[3]}" | |
client.request("GET", new_path) | |
response = client.getresponse() | |
item_id = f"ERROR__ENTITY_ID_{entity_id}" | |
try: | |
matches = ITEM_REGEX.search(str(response.read())) | |
item_id = matches.groups()[0] | |
except: | |
pass | |
return (item_id, colour) | |
print("<INVENTORY>") | |
items = [line.strip() for line in data.splitlines()] | |
for item in items: | |
if not item: | |
continue | |
matches = ENTITY_ID_REGEX.match(item).groups() | |
entity_id = matches[0] | |
qty = 1 | |
if matches[2]: | |
qty = matches[2] | |
(item_id, colour) = get_entity_to_item(entity_id) | |
print(f""" <ITEM> | |
<ITEMTYPE>P</ITEMTYPE> | |
<ITEMID>{item_id}</ITEMID> | |
<COLOR>{colour}</COLOR> | |
<MINQTY>{qty}</MINQTY> | |
</ITEM>""") | |
time.sleep(random.randrange(100, 10000) / 1000) | |
print("</INVENTORY>") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment