Skip to content

Instantly share code, notes, and snippets.

@ali1234
Last active March 29, 2017 23:25
Show Gist options
  • Save ali1234/836ba792390c1a5feca9a3dcb35c14af to your computer and use it in GitHub Desktop.
Save ali1234/836ba792390c1a5feca9a3dcb35c14af to your computer and use it in GitHub Desktop.
Rebrickable API tool to find colour replacements
#!/usr/bin/env python
# Finds colour replacements for an LDraw file.
# Usage:
# colourswap.py <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"
try:
minimum_rarity = int(sys.argv[3])
except:
minimum_rarity = 1
def get_colours(part):
response = urllib2.urlopen('https://rebrickable.com/api/v3/lego/parts/' + part + '/colors/?key=' + api_key)
data = json.load(response)
colours = set()
for item in data['results']:
if item['num_sets'] > minimum_rarity:
colours.add(item['color_name'])
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]
parts.add(part)
for part in parts:
colours.append(get_colours(part))
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