# coding: utf-8
#
# version 2008-02-06
#
import sys
import urllib
from cgi import parse_qs
def parse_deck(str):
"""Parse the string into a dictionary"""
str = parse_qs(str)
rows = str['rows'][0]
colours = str['c'][0]
names = str['n'][0].split('^')[0]
stars = str['n'][0].split('^')[7][0]
outofgamut = str['n'][0].split('^')[8]
return {'rows': rows, 'stars': stars, 'colours': colours, 'names': names, 'outofgamut': outofgamut}
def merge_decks(deck1, deck2):
"""Merge 2 decks into one"""
return {'rows': deck1['rows'], 'stars': deck1['stars'], 'colours': deck1['colours']+'~'+deck2['colours'], 'names': deck1['names']+'~'+deck2['names'], 'outofgamut': deck1['outofgamut']+'~'+deck2['outofgamut']}
url = sys.argv[1]
sock = urllib.urlopen(url)
webpage = sock.read()
sock.close()
if 'ReceiveServerData' in webpage:
data = webpage.split('ReceiveServerData("fandeck^&')[1].split('");')[0]
deck = parse_deck(data)
if 'Page: 1 of ' in webpage:
pages = webpage.split('Page: 1 of ')[1].split('
')[0]
sock = urllib.urlopen(url +'&'+urllib.urlencode({'__VIEWSTATE': viewstate})+'&ctl00%24mainbody%24Pager1%24Next.x=7&ctl00%24mainbody%24Pager1%24Next.y=7')
webpage = sock.read()
sock.close()
print 'page '+str(i+1)+'/'+pages
data = webpage.split('ReceiveServerData("fandeck^&')[1].split('");')[0]
deck = merge_decks(deck, parse_deck(data))
deck['company'] = webpage.split('')[1].split('')[0]
deck['collection'] = webpage.split('selected" value="'+parse_qs(url)['lineid'][0]+'">')[1].split('')[0]
deck['colours'] = deck['colours'].split('~')
deck['names'] = deck['names'].split('~')
deck['outofgamut'] = deck['outofgamut'].split('~')
palette = open(deck['company']+' - '+deck['collection']+'.gpl', 'w')
palette.write('GIMP Palette\n')
palette.write('Name: '+deck['company']+' - '+deck['collection']+'\n')
palette.write('Columns: '+deck['rows']+'\n')
palette.write('#\n')
palette.write('#\tPalette downloaded from Colorcharts.org\n')
palette.write('#\t'+url+'\n')
palette.write('#\n')
palette.write('#\t* The original colour is out of sRGB gamut\n')
palette.write('#\n')
for i in range(len(deck['colours'])):
oog = ''
if deck['outofgamut'][i] == '-1':
oog = '*'
R = str(int(deck['colours'][i][0:2],16)).rjust(3)
G = str(int(deck['colours'][i][2:4],16)).rjust(3)
B = str(int(deck['colours'][i][4:6],16)).rjust(3)
palette.write(R+" "+G+" "+B+"\t"+deck['names'][i]+oog+'\n')
palette.close()
else:
print 'wrong url '+url
sys.exit()
|