#!/bin/bash
#
# 			diu_graph.sh
#
# Del.icio.us graph generator
# http://freaknet.org/alpt/src/misc/diu_graph.sh
#
# --
#
# This script retrieves your Del.icio.us bookmarks and generates a graph of
# your tags.
#
# Two tags are considered connected if they belong to the same URL.
# Two URLs are connected if they share at least one tag. 
#
# Note that if you have more than 200 tags, the graph maybe VERY BIG.
# Use http://zvtm.sourceforge.net/zgrviewer.html to view it.
#
# This script depends on Graphviz: http://www.graphviz.org/
#
# AlpT (@freaknet.org)
#

###### CONFIGURE HERE ######
DIU_USER=""
DIU_PASSWD=""
###### CONFIGURE HERE ######


if [ -z "$DIU_USER" ] || [ -z "$DIU_PASSWD" ]
then
	echo Please configure your del.icio.us username and password.
	echo Change their values in $0.
	exit 1
fi

echo "Retrieving the bookmarks in myDelicious.xml"
curl --user "$DIU_USER":"$DIU_PASSWD" -o myDelicious.xml -O 'https://api.del.icio.us/v1/posts/all'

echo "Generating tags.dot"
cat myDelicious.xml  | awk '{ match($0, /tag="([^"]+)"/, arr); print arr[1];}' \
| grep -v "^$" | sort -u > tags

echo "graph G {" > tags.dot
echo "node [shape=plaintext, fontsize=24]
//edge [len=3]
//nodesep=3" >> tags.dot

cat tags | sed -e 's/ / -- /g'  -e 's/\./_/g' -e \
's/\(^\| \)\([0-9]\)/ _\2/g'  -e 's/:/_/g' -e \
's/\([A-Za-z0-9]\)-\([A-Za-z0-9]\)/\1_\2/g' >> tags.dot
echo "}" >> tags.dot

echo "Executing graphviz on tags.dot, the result will be saved on tags.svg"
echo "This process may take very LONG time"
fdp tags.dot -Tsvg > tags.svg
