import urllib import urllib2 import simplejson def get_from_url(url): opener = urllib2.build_opener() opener.addheaders = [('User-agent', 'zyons.com/1.0')] base_url = 'http://tagthe.net/api?view=json&url=%s' % url resp = opener.open( base_url ) json = resp.read() return simplejson.loads( json ) def get_from_text(text): data = urllib.urlencode({'text':text}) opener = urllib2.build_opener() opener.addheaders = [('User-agent', 'zyons.com/1.0')] base_url = 'http://tagthe.net/api?view=json' # % data resp = opener.open( base_url, data ) json = resp.read() return simplejson.loads( json ) def get_from_text_or_url( text, url ): try: if text: json = get_from_text(text) else: if url: json = get_from_url(url) else: json={} except urllib2.HTTPError: if url: json = get_from_url(url) else: json={} return json def tags_from_text( text, url): json = get_from_text_or_url(text,url) tags=[] try: for person in json['memes'][0]['dimensions']['person']: tags.append( person ) except KeyError: pass try: for topic in json['memes'][0]['dimensions']['topic']: tags.append( topic ) except KeyError: pass try: for loc in json['memes'][0]['dimensions']['location']: tags.append( loc ) except KeyError: pass tag_hash={} for t in tags: if t.strip() == "": continue if t.strip() == "-": continue tag_hash[t] = t tags = tag_hash.keys() return tags