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


import pickle
import urllib
import xml.dom.minidom
import pydot
import gradi
import amazon

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

# file prefix
filename='dhh'
output_filename='/var/www/'+filename+'_graph3'

#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='7,14')


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

# 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])
    g.add_node(node)
    
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')
