Remove unused license preamble
[mygpo.git] / mygpo / data / delicious.py
blob60c4e5608ffa9a6ab733b5fb1d438ad57582dc8e
1 import json
2 import hashlib
3 import urllib.request, urllib.parse, urllib.error
4 import urllib.parse
7 def get_tags(url):
8 """
9 queries the public API of delicious.com and retrieves a dictionary of all
10 tags that have been used for the url, with the number of users that have
11 used each tag
12 """
14 split = urllib.parse.urlsplit(url)
15 if split.path == '':
16 split = urllib.parse.SplitResult(split.scheme, split.netloc, '/', split.query, split.fragment)
17 url = split.geturl()
19 m = hashlib.md5()
20 m.update(url.encode('ascii'))
22 url_md5 = m.hexdigest()
23 req = 'http://feeds.delicious.com/v2/json/urlinfo/%s' % url_md5
25 resp = urllib.request.urlopen(req).read()
26 try:
27 resp_obj = json.loads(resp)
28 except ValueError:
29 return {}
31 tags = {}
32 for o in resp_obj:
33 if (not 'top_tags' in o) or (not o['top_tags']):
34 return {}
35 for tag, count in o['top_tags'].items():
36 tags[tag] = count
39 return tags