Merge pull request #793 from gpodder/remove-advertise
[mygpo.git] / mygpo / data / delicious.py
blobee46bebb7bf3cd0126ed18dc4081b9b4f5db1212
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(
17 split.scheme, split.netloc, "/", split.query, split.fragment
19 url = split.geturl()
21 m = hashlib.md5()
22 m.update(url.encode("ascii"))
24 url_md5 = m.hexdigest()
25 req = "http://feeds.delicious.com/v2/json/urlinfo/%s" % url_md5
27 resp = urllib.request.urlopen(req).read()
28 try:
29 resp_obj = json.loads(resp)
30 except ValueError:
31 return {}
33 tags = {}
34 for o in resp_obj:
35 if (not "top_tags" in o) or (not o["top_tags"]):
36 return {}
37 for tag, count in o["top_tags"].items():
38 tags[tag] = count
40 return tags