#!/usr/bin/python
# matt.joyce@gmail.com
# April 2007
#


import pickle
import urllib
import pydot
import gradi
import amazon

## How many of the original list are similar to themselves

# file prefix
filename='dhh'

#filename='ln'
output_filename='/var/www/'+filename+'_graph4'

#load the pairs and the original asins
pairs=pickle.load(open(filename+"_pairs.pik"))
asins=pickle.load(open(filename+"_asins.pik"))


weight={}

for asin in asins:
    weight[asin]=0

for pair in pairs:
    if pair[1] in asins:
        weight[pair[1]]+=1
        weight[pair[0]]+=1

print max(weight.values())
# create a graph
g=pydot.Dot(type='digraph', prog='neato',splines='true', overlap='false', size='20,20')


loColor=gradi.HTMLColorToRGB('FFCC00')
hiColor=gradi.HTMLColorToRGB('FF0000')
colorgradient=1.0/max(weight.values())

lweight={}

# add a node for each original title
for asin in asins:
    color=gradi.RGBToHTMLColor(gradi.RGBinterpolate(loColor,hiColor,colorgradient*weight[asin]))
    node=pydot.Node(asin, shape='circle',style='filled', fillcolor=color,  fontsize=8+weight[asin])
    node.set_URL('http://www.amazon.com/gp/product/'+asin)
    amazonresult=amazon.AmazonAPI(asin)
    tooltip=amazon.getelement(amazonresult,'Title')
    node.set_tooltip(tooltip)
    g.add_node(node)



# add the edges if both asin are in the original list (the first will always be)    
for pair in pairs:
    if pair[1] in asins:
	g.add_edge(pydot.Edge(pair[0],pair[1]))

g.write(output_filename+'.svg',format='svg')

