Skip to content

Instantly share code, notes, and snippets.

@SuzanaK
Last active January 12, 2022 03:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SuzanaK/5375732 to your computer and use it in GitHub Desktop.
Save SuzanaK/5375732 to your computer and use it in GitHub Desktop.
Using the ColourLovers API, these scripts will download all pattern image files (200 x 200 pixel, no repetition), palettes or colors that were created by a specific user. Usage: python cl_image_download.py USERNAME Until now, this script was only tested on a Linux system. After downloading, the image files will be in a directory called "images_U…
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import urllib, urllib2, sys, os, time, json, codecs, requests, logging
import unicodecsv as csv
logging.basicConfig(level=logging.INFO)
CL_COLORS = 0
CL_PALETTES = 1
CL_PATTERNS = 2
# how long to wait between one image and the next
SLEEP_TIME = 0.5 # seconds
def download_image(directory, title, image_url, badge=False):
title = "".join(i for i in title if i not in "\/:*?<>|")
if os.path.isdir(directory):
filename = directory + os.sep + title
else:
filename = title
if badge:
filename += '_badge.png'
else:
filename += '.png'
# allow_redirects=True is important!
r = requests.get(image_url, allow_redirects=True)
if r.status_code == 200:
try:
with open(filename, 'wb',) as fh:
fh.write(r.content)
except:
logging.warn("Could not save image!")
sys.exit(0)
logging.info("Downloaded badge image for: %s" %title)
else:
logging.warn("Could not download image! %s" %image_url)
sys.exit(0)
logging.info("Downloaded image file %s"%filename)
def download_one(directory, data):
title = "".join(i for i in data['title'] if i not in "\/:*?<>|")
logging.info("downloaded data for item named %s" %title)
if os.path.isdir(directory):
filename = directory + os.sep + title + '.txt'
else:
filename = title + '.txt'
if os.path.isfile(filename):
logging.warn('File exists already! Returning...')
return False
fh = codecs.open(filename, mode="w", encoding="utf-8")
json.dump(data, fh, sort_keys=True, indent=4, separators=(',', ': '))
fh.close()
return True
def download_all(username, directory, mode, API_URL):
offset = 0
counter = 0
while True:
if username == 'ALL':
data = {'format':'json','resultOffset':str(offset),'numResults':'100'}
else:
data = {'lover':username,'format':'json','resultOffset':str(offset),'numResults':'100'}
data_encoded = urllib.urlencode(data)
request = urllib2.Request(API_URL, data_encoded,
headers={'User-Agent':'Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:41.0) Gecko/20100101 Firefox/41.0'})
fh = urllib2.urlopen(request)
result = json.load(fh)
logging.info("Received a list with the next %d items"%len(result))
if len(result) == 0 or counter >= 2000:
break
counter += len(result)
alldata = []
for i in result:
data = {}
data['title'] = i['title']
data['date'] = i['dateCreated']
data['id'] = i['id']
data['image_url'] = i['imageUrl']
data['badge_url'] = i['badgeUrl']
data['api_url'] = i['apiUrl']
data['user_name'] = i['userName']
data['loves'] = i['numVotes']
data['views'] = i['numViews']
data['comments'] = i['numComments']
if mode == CL_COLORS:
data['hex'] = i['hex']
data['rgb'] = i['rgb']
data['hsv'] = i['hsv']
elif mode == CL_PALETTES:
data['colors'] = i['colors']
#data['color_widths'] = i['colorWidths']
data['description'] = i['description']
elif mode == CL_PATTERNS:
data['template_author'] = i['template']['author']['userName']
data['template_title'] = i['template']['title']
data['colors'] = i['colors']
else:
logging.error('Unkown mode! Returning.')
return
res = download_one(directory, data)
if not res:
break
download_image(directory, data['title'], data['image_url'])
download_image(directory, data['title'], data['badge_url'], badge=True)
alldata.append(data)
#in case that ColourLovers will block your IP when there are too many requests in a short time
#time.sleep(SLEEP_TIME)
if len(result) < 100:
break
offset += len(result)
if mode == CL_COLORS:
alldata.sort(key=lambda c: c['hsv']['hue']) #sort colors by hue from 0 to 359
filename = username + os.path.sep + 'all_colors_' + username
if mode == CL_PALETTES:
filename = username + os.path.sep + 'all_palettes_' + username
if mode == CL_PATTERNS:
filename = username + os.path.sep + 'all_patterns_' + username
fh = open(filename+'.json', 'w')
json.dump(alldata, fh)
fh.close()
fh = open(filename+'.csv', 'w')
writer = csv.DictWriter(fh, delimiter=';', fieldnames=sorted(alldata[0].keys()))
writer.writeheader()
for data in alldata:
writer.writerow(data)
fh.close()
def usage():
return """This script will download all colors, patterns or palettes at ColourLovers.com that were created by a specific user.
After downloading, the image files will be in a directory called colors_USERNAME. Usage: %s USERNAME"""%sys.argv[0]
if __name__ == "__main__":
if len(sys.argv) == 2:
username = sys.argv[1]
username = "".join(i for i in username if i not in "\/:*?<>|")
directory = username
if not os.path.isdir(directory): os.mkdir(directory)
logging.info("Created new directory: %s" %directory)
mode = CL_COLORS
API_URL = 'http://www.colourlovers.com/api/colors/'
directory = username + os.path.sep + 'colors_' + username
if not os.path.isdir(directory): os.mkdir(directory)
logging.info("Created new directory: %s" %directory)
download_all(username, directory, mode, API_URL)
sys.exit(0)
mode = CL_PALETTES
API_URL = 'http://www.colourlovers.com/api/palettes/'
directory = username + os.path.sep + 'palettes_' + username
if not os.path.isdir(directory): os.mkdir(directory)
logging.info("Created new directory: %s" %directory)
download_all(username, directory, mode, API_URL)
mode = CL_PATTERNS
API_URL = 'http://www.colourlovers.com/api/patterns/'
directory = username + os.path.sep + 'patterns_' + username
if not os.path.isdir(directory): os.mkdir(directory)
logging.info("Created new directory: %s" %directory)
download_all(username, directory, mode, API_URL)
else:
print(usage())
sys.exit(0)
# soup.find_all(href=re.compile("elsie"))
# n-meta-4033136-k4aQNQgW-item (id)
# http://www.colourlovers.com/lover/DarkBlueMe4Ever/patterns/most-loved/all-time/meta?page=1
# http://www.colourlovers.com/lover/Suzana_K/palettes/most-loved/all-time/meta?page=1
# http://www.colourlovers.com/lover/Suzana_K/colors/most-loved/all-time/meta?page=1
# http://www.colourlovers.com/lover/L%20u%20n%20a/patterns/new/all-time/meta?page=1
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import urllib, urllib2, sys, os, time, json, codecs, requests, logging
API_URL = 'http://www.colourlovers.com/api/colors/'
# how long to wait between one image and the next
SLEEP_TIME = 0.5 # seconds
logging.basicConfig(level=logging.INFO)
# The API call for downloading color images seems to be broken, so the call to this function is commented out
def download_image(directory, title, image_url):
title = "".join(i for i in title if i not in "\/:*?<>|")
if os.path.isdir(directory):
filename = directory + os.sep + title + '.png'
else:
filename = title + '.png'
r = requests.get(image_url, allow_redirects=True)
if r.status_code == 200:
try:
with open(filename, 'wb',) as fh:
fh.write(r.content)
except:
logging.warn("Could not save image!")
sys.exit(0)
logging.info("Downloaded badge image for: %s" %title)
else:
logging.warn("Could not download image! %s" %image_url)
sys.exit(0)
logging.info("Downloaded image file %s"%filename)
def download_color(directory, data):
title = "".join(i for i in data['title'] if i not in "\/:*?<>|")
logging.info("downloaded color information for color named %s" %title)
if os.path.isdir(directory):
filename = directory + os.sep + title + '.txt'
else:
filename = title + '.txt'
if os.path.isfile(filename):
logging.warn('File exists already! Returning...')
return False
fh = codecs.open(filename, mode="w", encoding="utf-8")
json.dump(data, fh, sort_keys=True, indent=4, separators=(',', ': '))
fh.close()
return True
def download_all_colors(username):
directory = 'colours_' + username
if not os.path.isdir(directory): os.mkdir(directory)
logging.info("Created new directory: %s" %directory)
offset = 0
counter = 0
while True:
if username == 'ALL':
data = {'format':'json','resultOffset':str(offset),'numResults':'100'}
else:
data = {'lover':username,'format':'json','resultOffset':str(offset),'numResults':'100'}
data_encoded = urllib.urlencode(data)
request = urllib2.Request(API_URL, data_encoded,
headers={'User-Agent':'Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:41.0) Gecko/20100101 Firefox/41.0'})
fh = urllib2.urlopen(request)
result = json.load(fh)
logging.info("Received a list with the next %d colors"%len(result))
if len(result) == 0 or counter >= 2000:
break
counter += len(result)
allcolors = []
for i in result:
data = {}
data['title'] = i['title']
data['hex'] = i['hex']
data['rgb'] = i['rgb']
data['hsv'] = i['hsv']
data['date'] = i['dateCreated']
data['id'] = i['id']
data['image_url'] = i['imageUrl']
res = download_color(directory, data)
if not res:
break
download_image(directory, data['title'], data['image_url'])
allcolors.append(data)
#in case that ColourLovers will block your IP when there are too many requests in a short time
#time.sleep(SLEEP_TIME)
if len(result) < 100:
break
offset += len(result)
allcolors.sort(key=lambda c: c['hsv']['hue']) #sort colors by hue from 0 to 359
filename = directory + os.sep + 'ALLCOLORS_%s'%username + '.json'
fh = open(filename, 'w')
json.dump(allcolors, fh)
fh.close()
def usage():
return "This script will download all color images and json data at ColourLovers.com that were created by a specific user. After downloading, the image files will be in a directory called colors_USERNAME. Usage: %s USERNAME"%sys.argv[0]
if __name__ == "__main__":
if len(sys.argv) == 2:
username = sys.argv[1]
username = "".join(i for i in username if i not in "\/:*?<>|")
download_all_colors(username)
else:
print(usage())
sys.exit(1)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# TODO csv overview of patterns, pal, col
# dl ALL patterns, pal, col
#function setTimeOutJQ(i, iMax, user, tag){if (i<iMax){jQuery.ajax({type: "GET", url:"http://www.colourlovers.com/lover/" + user + "/" + tag + "/new/all-time/meta", data:{page:i}, success:onAjaxSuccess, dataType:'html'}); setTimeout(function(){setTimeOutJQ(i+1, iMax, user, tag)}, 1000); } else{console.log('finished');}}
#function onAjaxSuccess (data){spanNode = document.createElement("span"); spanNode.innerHTML = data; arr = spanNode.select("[id$=-item]"); for (var i=0; i< arr.length; i++){$('ajax-comments').value += arr[i].id.match('[0-9]{5,8}')[0] + '\n';}}
# call API to dl inform. about each pattern, incl. template
# include likes (loves)
# dl templates?
# create svg for each template
# calculate ration views to loves
# which template maker is my fav? max of template authors
import urllib, urllib2, sys, os, time, json, codecs, logging
from cl_color_download import download_all_colors
from cl_palette_download import download_all_palettes
from cl_pattern_download import download_all_patterns
API_URL = 'http://www.colourlovers.com/api/colors/'
# how long to wait between one image and the next
SLEEP_TIME = 0.5 # seconds
def download_all(username):
download_all_colors(username)
download_all_palettes(username)
download_all_patterns(username)
def usage():
return """This script will download all colors, patterns and palettes at ColourLovers.com that were created by a specific user.
After downloading, the image files will be in a directory called colors_USERNAME.
Usage: %s USERNAME"""%sys.argv[0]
if __name__ == "__main__":
if len(sys.argv) == 2:
username = sys.argv[1]
username = "".join(i for i in username if i not in "\/:*?<>|")
download_all(username)
else:
print(usage())
sys.exit(1)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import urllib, urllib2, sys, os, time, json, codecs, requests, logging
API_URL = 'http://www.colourlovers.com/api/palettes/'
# how long to wait between one image and the next
SLEEP_TIME = 0.5 # seconds
logging.basicConfig(level=logging.INFO)
def download_image(directory, id, title, image_url):
title = "".join(i for i in title if i not in "\/:*?<>|")
if os.path.isdir(directory):
filename = directory + os.sep + title + '.png'
else:
filename = title + '.png'
r = requests.get(image_url, allow_redirects=True)
if r.status_code == 200:
try:
with open(filename, 'wb',) as fh:
fh.write(r.content)
except:
logging.warn("Could not save image!")
sys.exit(0)
logging.info("Downloaded badge image for: %s" %title)
else:
logging.warn("Could not download image! %s" %image_url)
sys.exit(0)
# urllib.urlretrieve(image_link, filename) this doesn't work for unkown reasons
logging.info("Downloaded image file %s"%filename)
def download_palette_colors(directory, id, title, colors):
#logging.info("Colors of %s:" %title)
#logging.info(", ".join(colors))
title = "".join(i for i in title if i not in "\/:*?<>|")
if os.path.isdir(directory):
filename = directory + os.sep + title + '.txt'
else:
filename = title + '.txt'
if os.path.isfile(filename):
logging.warn('File exists already! Returning...')
return False
fh = codecs.open(filename, mode="w", encoding="utf-8")
fh.write(title + "\n\n")
fh.write(", ".join(colors))
fh.close()
return True
def download_all_palettes(username):
directory = 'palettes_' + username
if not os.path.isdir(directory): os.mkdir(directory)
offset = 0
#hues= ['red', 'orange', 'green', 'aqua', 'blue', 'violet','fuchsia']
#sortBy [Where X can be: dateCreated, score, name, numVotes, or numViews]
# [Where X can be: ASC or DESC. Default ASC]
#orderCol
#for hue in hues:
while True:
if username == 'ALL':
data = {'format':'json','resultOffset':str(offset),'numResults':'100'}
else:
data = {'lover':username,'format':'json','resultOffset':str(offset),'numResults':'100'}
data_encoded = urllib.urlencode(data)
request = urllib2.Request(API_URL, data_encoded, headers={'User-Agent':'CL Backup Script'})
fh = urllib2.urlopen(request)
result = json.load(fh)
logging.info( "Received a list with the next %d patterns"%len(result))
if len(result) == 0:
break
for i in result:
image_url = i['badgeUrl']
pattern_id = i['id']
title = i['title']
colors = i['colors']
date = i['dateCreated']
res = download_palette_colors(directory, pattern_id, title, colors)
if not res:
break
download_image(directory, pattern_id, title, image_url)
# in case that ColourLovers will block your IP when there are too many requests in a short time
time.sleep(SLEEP_TIME)
if len(result) < 100:
break
offset += len(result)
logging.info('last data: %s' %data)
def usage():
return "This script will download all pattern images at colourlovers.com that were created by a specific user. After downloading, the image files will be in a directory called images_USERNAME. Usage: %s USERNAME or ALL to download the most popular palettes of all colourlovers."%sys.argv[0]
if __name__ == "__main__":
if len(sys.argv) == 2:
username = sys.argv[1]
username = "".join(i for i in username if i not in "\/:*?<>|")
download_all_palettes(username)
else:
print(usage())
sys.exit(1)
example = [{"id":113451,"title":"Anaconda","userName":"kunteper","numViews":582,"numVotes":2,"numComments":2,"numHearts":0,"rank":0,"dateCreated":"2007-08-05 14:14:15","colors":["2B2D42","7A7D7F","B1BBCF","6E0B21","9B4D73"],"description":"like an anaconda","url":"http:\/\/www.colourlovers.com\/palette\/113451\/Anaconda","imageUrl":"http:\/\/www.colourlovers.com\/paletteImg\/2B2D42\/7A7D7F\/B1BBCF\/6E0B21\/9B4D73\/Anaconda.png","badgeUrl":"http:\/\/www.colourlovers.com\/images\/badges\/p\/113\/113451_Anaconda.png","apiUrl":"http:\/\/www.colourlovers.com\/api\/palette\/113451"}]
# http://www.colourlovers.com/images/badges/p/113/113451_Anaconda.png
#http://www.colourlovers.com/paletteImg/2B2D42/7A7D7F/B1BBCF/6E0B21/9B4D73/Anaconda.png
@SuzanaK
Copy link
Author

SuzanaK commented Jun 18, 2013

Usage: python cl_image_download.py USERNAME

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment