Skip to content

Instantly share code, notes, and snippets.

@ali1234 ali1234/
Last active Mar 29, 2017

What would you like to do?
Rebrickable API tool to find colour replacements
#!/usr/bin/env python
# Finds colour replacements for an LDraw file.
# Usage:
# <LDraw file> <colour> [<rarity>]
# colour: an LDraw colour number, eg yellow = 14
# rarity: pieces must be in at least this many sets
import urllib2
import json
import sys
api_key = "rebrickable API key goes here"
minimum_rarity = int(sys.argv[3])
minimum_rarity = 1
def get_colours(part):
response = urllib2.urlopen('' + part + '/colors/?key=' + api_key)
data = json.load(response)
colours = set()
for item in data['results']:
if item['num_sets'] > minimum_rarity:
return colours
parts = set()
colours = []
if __name__ == '__main__':
with open(sys.argv[1]) as data:
for line in data:
x = line.strip().split()
if x[0] == '1':
colour = x[1]
if colour == sys.argv[2]:
part = x[-1][:-4]
for part in parts:
print 'Parts:'
print ', '.join(parts)
print 'Common Colours:'
print ', '.join(set.intersection(*colours))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.