Fix byte/str mix in get_git_head()
[mygpo.git] / mygpo / data / delicious.py
blob750e683ae022f67c5755e2732051c581f5d5bb61
2 # This file is part of gpodder.net.
4 # my.gpodder.org is free software: you can redistribute it and/or modify it
5 # under the terms of the GNU Affero General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or (at your
7 # option) any later version.
9 # my.gpodder.org is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
12 # License for more details.
14 # You should have received a copy of the GNU Affero General Public License
15 # along with my.gpodder.org. If not, see <http://www.gnu.org/licenses/>.
19 import json
20 import hashlib
21 import urllib.request, urllib.parse, urllib.error
22 import urllib.parse
25 def get_tags(url):
26 """
27 queries the public API of delicious.com and retrieves a dictionary of all
28 tags that have been used for the url, with the number of users that have
29 used each tag
30 """
32 split = urllib.parse.urlsplit(url)
33 if split.path == '':
34 split = urllib.parse.SplitResult(split.scheme, split.netloc, '/', split.query, split.fragment)
35 url = split.geturl()
37 m = hashlib.md5()
38 m.update(url.encode('ascii'))
40 url_md5 = m.hexdigest()
41 req = 'http://feeds.delicious.com/v2/json/urlinfo/%s' % url_md5
43 resp = urllib.request.urlopen(req).read()
44 try:
45 resp_obj = json.loads(resp)
46 except ValueError:
47 return {}
49 tags = {}
50 for o in resp_obj:
51 if (not 'top_tags' in o) or (not o['top_tags']):
52 return {}
53 for tag, count in o['top_tags'].items():
54 tags[tag] = count
57 return tags