#!/usr/bin/python # Created by Matteo Rattotti on 13/03/06. # Copyright (c) 2006 Matteo Rattotti. All rights reserved. # Contact matteo.rattotti@gmail.com for any problem # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. ''' Interface for getting information from http://del.icio.us api ''' __version__ = "0.1" __author__ = "Matteo Rattotti" __contact__ = "http://www.rknet.it" __date__ = 'Tue Nov 8 12:22:21 2005' __timestamp__ = '1131448941' import xml.sax import urllib2 import sys #Api url allPostsAPI = 'https://api.del.icio.us/v1/posts/all?' recentPostsAPI = 'https://api.del.icio.us/v1/posts/recent?count=' tagAPI = 'https://api.del.icio.us/v1/tags/get' bundleAPI = 'https://api.del.icio.us/v1/tags/bundles/all' class dataGrabber: """ Interface class for post and tag parser """ def __init__(self, username, password, proxy=None): self.password = password self.username = username self.proxy = proxy """ Return a tuple like this ([tags], {Posts}) """ def returnData(self): return {'tags':self.getTag(), 'posts':self.getPost(), 'bundles':self.getBundle(), 'recent':self.getRecentPost()} def returnBundle(self): return self.getBundle() def returnRecent(self): return self.getRecentPost() def getPost(self): postData = getApi(allPostsAPI, self.username, self.password, self.proxy) p = xml.sax.make_parser() p.setContentHandler(postHandler()) p.feed(postData.read()) p.close() store = p.getContentHandler() return store.returnData() def getRecentPost(self, howMuch=15): recentPostData = getApi(recentPostsAPI + str(howMuch), self.username, self.password, self.proxy) p = xml.sax.make_parser() p.setContentHandler(postHandler()) p.feed(recentPostData.read()) p.close() store = p.getContentHandler() return store.returnData() def getTag(self): tagData = getApi(tagAPI, self.username, self.password, self.proxy) p = xml.sax.make_parser() p.setContentHandler(tagHandler()) p.feed(tagData.read()) p.close() store = p.getContentHandler() return store.returnData() def getBundle(self): bundleData = getApi(bundleAPI, self.username, self.password, self.proxy) p = xml.sax.make_parser() p.setContentHandler(bundleHandler()) p.feed(bundleData.read()) p.close() store = p.getContentHandler() return store.returnData() class tagHandler(xml.sax.ContentHandler): def __init__(self): self.tagList = [] def startElement(self, tag, attributes): if tag == "tag": value = attributes.get('tag') if value.lower() not in self.tagList: self.tagList.append(value.lower()) def returnData(self): return self.tagList class postHandler(xml.sax.ContentHandler): def __init__(self): self.postList = [] def startElement(self, tag, attributes): if tag == "post": url = attributes.get('href') desc = attributes.get('description') tags = attributes.get('tag') tags = tags.lower() self.postList.append({'url':url, 'desc':desc, 'tag':tags}) def returnData(self): return self.postList class bundleHandler(xml.sax.ContentHandler): def __init__(self): self.bundles = {} def startElement(self, tag, attributes): if tag == "bundle": name = attributes.get('name') tags = attributes.get('tags') tags = tags.lower() self.bundles[name] = tags.split(" ") def returnData(self): return self.bundles def getApi(apiUrl, username, password, proxy=None): """ This function return the xml for any delicious api """ if proxy is not None: proxy_handler = urllib2.ProxyHandler({"http:": proxy}) #"http://proxyuser:proxypassword@myproxy:8050"}) #userAgent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.6) Gecko/20040113' userAgent = 'Delibar/0.8 Tiger version, http://www.rknet.it/program/delibar/ released on 20060330' authinfo = urllib2.HTTPBasicAuthHandler() authinfo.add_password('del.icio.us API', 'api.del.icio.us', username,password) # Aggiungere il proxy_handler qui if proxy is not None: opener = urllib2.build_opener(authinfo, proxy_handler) else: opener = urllib2.build_opener(authinfo) urllib2.install_opener(opener) """data = urllib2.urlopen('http://del.icio.us/api/tags/get') print data """ try: req = urllib2.Request(apiUrl) req.add_header('User-Agent', userAgent) data = urllib2.urlopen(req) return data except Exception, inst: print "##################################" print inst print sys.exc_info()[0] print "##################################" raise if __name__ == '__main__': d = dataGrabber('whamoo', 'cav168') a = d.returnData() #try: #print a['posts'] print a['tags'] #except urllib2.HTTPError, e: # print e.code # if e.code == 401: # print "Username required"