from xml.dom import minidom import urllib2, base64 user = 'yourusername' passwd = 'yourpassword' mindmap = open('deliciousmap.mm', 'w') mindmap.write('\n') mindmap.write('\n') mindmap.write('\n'%(user, user)) ########################################################################### ########################################################################### def auth_open(url): request = urllib2.Request(url) base64string = base64.encodestring('%s:%s'%(user, passwd))[: -1] request.add_header("Authorization", "Basic %s"%base64string) return urllib2.urlopen(request) ########################################################################### def compareTags(category_column,category_row): "Compares two tags, and gives 'I', '<', '>', 'X', 'D' if they are Identical, the first in the second, the second in the first, with nonempty intersection, Disjointed" column_in_row = 1 #this will remain true iff every instance that is present in column is also present in row row_in_column = 1 #this will remain true iff every instance that is present in row is also present in column intersection = 0 #this will become true as soon as there is a non trivial intersection for post in posts: if post.getAttribute("tag").find('+'+category_column+'+') != -1 : #it is in column if post.getAttribute("tag").find('+'+category_row+'+') != -1 : intersection = 1 #it is in row else: column_in_row = 0 #is in column but not in row else: if post.getAttribute("tag").find('+'+category_row+'+') != -1 : row_in_column = 0 #it is in row but not in column else: pass #last case added for completness. The post has nothing to do with those two tags. Do nothing. if intersection == 0 and column_in_row == 1 and row_in_column == 1 : return 'B' #B for BUG! With an empty Tag all the empty tags go 'B' if intersection == 0: return 'D' elif column_in_row == 1 and row_in_column == 1 : return 'I' elif column_in_row == 1 : return '>' elif row_in_column == 1 : return '<' return 'X' ########################################################################### def GetInverseRelation(relation): "Given a relation gives the inverse: 'I'->'I' 'X'->'X' '>'->'<' '<'->'>' 'D'->'D' 'B'->'B'" if relation == '<': return '>' if relation == '>': return '<' return relation def GetSubTags(category): "Given a Tag finds all the one under and returns it as a list" sub=[] pairRow = all_dict[category].items() for couple in pairRow: if (couple[1]=='>'): sub.append(couple[0]) return sub def GetEquivalentTags(category): "Given a Tag finds all the one under and returns it as a list" sub=[] pairRow = all_dict[category].items() for couple in pairRow: if (couple[1]=='I'): sub.append(couple[0]) return sub ########################################################################### def InsertMetaTag(category): "Insert the Tag in the mindmap and internal tags if necessary for one level" subcategories=GetSubTags(category) mindmap.write('\n'%(user, category, category,len(postsbytag[category]),len(subcategories))) posts_to_add = [] if len(subcategories)>0: if len(subcategories) > 1: mindmap.write('\n') for subcategory in subcategories: for altsubcat in subcategories: if all_dict[subcategory][altsubcat]=='<': #if the subcategory is contained in an alternative one we don't visualise it break else: EquivalentCategories = GetEquivalentTags(subcategory ) if len(EquivalentCategories)>1: if EquivalentCategories[0] == subcategory : #when tags have same content we only print the first instance, and put the all inside an empty node mindmap.write('\n') for EqivalentCategory in EquivalentCategories: InsertMetaTag(EqivalentCategory) mindmap.write('\n') else: continue else: InsertMetaTag(subcategory) if len(subcategories) > 1: mindmap.write('\n') for post in postsbytag[category] : for subcategory in subcategories: #this is to avoid that post that are present in subcategories appear again if post.getAttribute("tag").find('+'+subcategory+'+')!= -1: break else: posts_to_add.append(post) else: posts_to_add=postsbytag[category] if len(posts_to_add)==1 or (len(posts_to_add)>=1 and len(subcategories)==0 ): for post in posts_to_add: mindmap.write('\n'%(post.getAttribute("href").encode('ascii', 'xmlcharrefreplace').replace('&', '&'), post.getAttribute("description").encode('ascii','xmlcharrefreplace').replace('"', '"').replace('&', '&'))) elif len(posts_to_add) > 1: mindmap.write('\n') for post in posts_to_add: mindmap.write('\n'%(post.getAttribute("href").encode('ascii', 'xmlcharrefreplace').replace('&', '&'), post.getAttribute("description").encode('ascii','xmlcharrefreplace').replace('"', '"').replace('&', '&'))) mindmap.write('\n') mindmap.write('\n') ########################################################################### recent_post_data = auth_open('http://del.icio.us/api/posts/recent?count=1000').read() posts = minidom.parseString(recent_post_data).documentElement.getElementsByTagName("post") tag_data = auth_open('http://del.icio.us/api/tags/get?').read() xdoc = minidom.parseString(tag_data).documentElement newposts=[] for post in posts: newpost=post Attribute=newpost.getAttribute("tag") Attribute=Attribute.replace(" ","+ +") newpost.setAttribute("tag","+"+Attribute+"+") newposts.append(newpost) posts=newposts postsbytag = {} for post in posts: for tag in xdoc.getElementsByTagName("tag"): category = tag.getAttribute("tag") if post.getAttribute("tag").find("+"+category+"+") != -1: if category in postsbytag: postsbytag[category]=postsbytag[category]+[post] else: postsbytag[category]=[post] for tag in xdoc.getElementsByTagName("tag"): category = tag.getAttribute("tag") print category, len(postsbytag[category]) xdoc3 = xdoc2 = xdoc all_dict = {} for tag_row in xdoc2.getElementsByTagName("tag"): category_row = tag_row.getAttribute("tag") one_dict = {} for tag_column in xdoc3.getElementsByTagName("tag"): category_column = tag_column.getAttribute("tag") if category_column == category_row: one_dict[category_column] = 'I' elif category_column in all_dict: one_dict[category_column] = GetInverseRelation(all_dict[category_column][category_row]) else: one_dict[category_column] = compareTags(category_column,category_row) all_dict[category_row]=one_dict print category_row, one_dict.values() for tag in xdoc.getElementsByTagName("tag"): category = tag.getAttribute("tag") valueRow=all_dict[category].values() if valueRow.count('<') : continue EquivalentCategories = GetEquivalentTags(category) if len(EquivalentCategories)>1: if EquivalentCategories[0] == category: #when tags have same content we only print the first instance, and put the all inside an empty node mindmap.write('\n') for EqivalentCategory in EquivalentCategories: InsertMetaTag(EqivalentCategory) mindmap.write('\n') else: continue else: InsertMetaTag(category) mindmap.write('\n\n')