touch-couchdb-views accepts DB URL from commandline
[mygpo.git] / mygpo / data / delicious.py
blob8d470bb210a339cbb1a2c358eea3f9d4da5dc3ee
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 hashlib
20 import urllib
21 import urlparse
23 from mygpo.core.json import json
26 def get_tags(url):
27 """
28 queries the public API of delicious.com and retrieves a dictionary of all
29 tags that have been used for the url, with the number of users that have
30 used each tag
31 """
33 split = urlparse.urlsplit(url)
34 if split.path == '':
35 split = urlparse.SplitResult(split.scheme, split.netloc, '/', split.query, split.fragment)
36 url = split.geturl()
38 m = hashlib.md5()
39 m.update(url.encode('ascii'))
41 url_md5 = m.hexdigest()
42 req = 'http://feeds.delicious.com/v2/json/urlinfo/%s' % url_md5
44 resp = urllib.urlopen(req).read()
45 try:
46 resp_obj = json.loads(resp)
47 except ValueError:
48 return {}
50 tags = {}
51 for o in resp_obj:
52 if (not 'top_tags' in o) or (not o['top_tags']):
53 return {}
54 for tag, count in o['top_tags'].iteritems():
55 tags[tag] = count
58 return tags