#!/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+'_graph5'

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


weight={}

# create a weight for every similar asin
for pair in pairs:
    weight[pair[1]]=0

# create a weight for every original asin, we know they will be included
for asin in asins:
    weight[asin]=0

# increase the weight every time a similar item is encountered
for pair in pairs:
    weight[pair[1]]+=1

print len(pairs)
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())


# add a node for each original title
i=0
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)
    print i
    i+=1


similarity_threshold=max(weight.values())/3.0

# tease out the similar asins we want, avoiding ones from the original list.
# create these new node using a double circle shape so we can tell them apart.
# add them to a seperate list, to use later
s_asins=[]
for asin in weight.keys():
    if (asin not in asins) & (weight[asin]>similarity_threshold):
        color=gradi.RGBToHTMLColor(gradi.RGBinterpolate(loColor,hiColor,colorgradient*weight[asin]))
        node=pydot.Node(asin, shape='doublecircle',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')
	print tooltip
	node.set_tooltip(tooltip)
	g.add_node(node)
	s_asins.append(asin)
	


# add the edges
for pair in pairs:
    # from the original list and the new similar titles list
    if (pair[1] in asins) | (pair[1] in s_asins):
        g.add_edge(pydot.Edge(pair[0],pair[1]))

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


