#!/usr/bin/python
# matt.joyce@gmail.com
# April 2007
#
#   1. Load the pickled ASINs
#   2. Load the pickled ASIN pairs
#   3. Create a graph object
#   4. Create a node object for ASIN and add to the graph
#   5. Create an edge object for each linked original ASIN
#   6. Save the graph


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


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

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

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

# create a graph
g=pydot.Dot(type='digraph', prog='neato',splines='true', overlap='false')

# add a node for each original title
for asin in asins:
    node=pydot.Node(asin)
    g.add_node(node)
    
# add the edges, but only if the link is to/from the original list.
for pair in pairs:
    if pair[1] in asins:
	g.add_edge(pydot.Edge(pair[0],pair[1]))


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