#!/usr/bin/python # Created by Matteo Rattotti on 13/03/06. # Copyright (c) 2006 - 2007 Shiny Frog. All rights reserved. # Contact matteo.rattotti@shinyfrog.net 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://ma.gnolia.com 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 # User Agent userAgent = 'Delibar/0.9 Tiger version, http://www.shinyfrog.net/en/software/delibar/' #Api url allPostsAPI = 'https://ma.gnolia.com/api/mirrord/v1/posts/all?' recentPostsAPI = 'https://ma.gnolia.com/api/mirrord/v1/posts/recent?count=' tagAPI = 'https://ma.gnolia.com/api/mirrord/v1/tags/get' bundleAPI = 'https://ma.gnolia.com/api/mirrord/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): # Ma.gnolia doesn't have bundle! return [] class tagHandler(xml.sax.ContentHandler): def __init__(self): self.tagList = [] def startElement(self, tag, attributes): if tag == "tag": valueTag = attributes.get('tag') valueCount = attributes.get('count') if {'tag':valueTag.lower(), 'count':valueCount} not in self.tagList: self.tagList.append({'tag':valueTag.lower(), 'count':int(valueCount)}) 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 Ma.gnolia api """ if proxy is not None: proxy_handler = urllib2.ProxyHandler({"http:": proxy}) #"http://proxyuser:proxypassword@myproxy:8050"}) authinfo = urllib2.HTTPBasicAuthHandler() authinfo.add_password('Ma.gnolia API', 'ma.gnolia.com', 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('shinytest', 'shinytest') #print d.getPost() #print d.getTag() print d.returnRecent()