summaryrefslogtreecommitdiff
path: root/lib/APIClients.py
diff options
context:
space:
mode:
authorluxagraf@c63593aa-01b0-44d9-8516-4b9c7e931d7f <luxagraf@c63593aa-01b0-44d9-8516-4b9c7e931d7f>2009-11-23 20:34:40 +0000
committerluxagraf@c63593aa-01b0-44d9-8516-4b9c7e931d7f <luxagraf@c63593aa-01b0-44d9-8516-4b9c7e931d7f>2009-11-23 20:34:40 +0000
commitccde62e1d2c86be183b2e546957b2e77087ecde6 (patch)
tree47cd81c67958620d41d8bbcd40c75d888e4251be /lib/APIClients.py
parent526fbe767130b4dedd75841969808fe750afa22e (diff)
added externals and lib
Diffstat (limited to 'lib/APIClients.py')
-rw-r--r--lib/APIClients.py104
1 files changed, 104 insertions, 0 deletions
diff --git a/lib/APIClients.py b/lib/APIClients.py
new file mode 100644
index 0000000..24ab97b
--- /dev/null
+++ b/lib/APIClients.py
@@ -0,0 +1,104 @@
+# APIClients for grabbing data from popular web services
+# By Scott Gilbertson
+# Copyright is lame, take what you want, except for those portions noted
+
+# Dependencies:
+import sys, urllib
+import xml.etree.cElementTree as xml_parser
+
+
+DEBUG = 0
+
+"""
+base class -- handles GoodReads.com, but works for any rss feed, just send an empty string for anything you don't need
+"""
+class APIClient:
+ def __init__(self, base_path, api_key):
+ self.api_key = api_key
+ self.base_path = base_path
+
+ def __getattr__(self, method):
+ def method(_self=self, _method=method, **params):
+ url = "%s%s?%s&" % (self.base_path, self.api_key, urllib.urlencode(params))
+ if DEBUG: print url
+ data = self.fetch(url)
+ return data
+
+ return method
+
+ def fetch(self, url):
+ u = urllib.FancyURLopener(None)
+ usock = u.open(url)
+ rawdata = usock.read()
+ if DEBUG: print rawdata
+ usock.close()
+ return xml_parser.fromstring(rawdata)
+
+"""
+ Extend APIClient to work with the ma.gnolia.com API
+ (http://wiki.ma.gnolia.com/Ma.gnolia_API)
+ Adds some error handling as well
+"""
+class MagnoliaError(Exception):
+ def __init__(self, code, message):
+ self.code = code
+ self.message = message
+
+ def __str__(self):
+ return 'Magnolia Error %s: %s' % (self.code, self.message)
+
+
+class MagnoliaClient(APIClient):
+ def __getattr__(self, method):
+ def method(_self=self, _method=method, **params):
+ url = "%s%s?%s&api_key=%s" % (self.base_path, _method, urllib.urlencode(params), self.api_key)
+ if DEBUG: print url
+ data = APIClient.fetch(self, url)
+ return data
+ return method
+
+
+"""
+ Extend APIClient to work with the Flickr API
+ (http://www.flickr.com/services/api/)
+ Adds error handling as well
+"""
+
+class FlickrError(Exception):
+ def __init__(self, code, message):
+ self.code = code
+ self.message = message
+
+ def __str__(self):
+ return 'Flickr Error %s: %s' % (self.code, self.message)
+
+class FlickrClient(APIClient):
+ def __getattr__(self, method):
+ def method(_self=self, _method=method, **params):
+ _method = _method.replace("_", ".")
+ url = "%s?method=%s&%s&api_key=%s" % (self.base_path, _method, urllib.urlencode(params), self.api_key)
+ if DEBUG: print url
+ data = APIClient.fetch(self, url)
+ return data
+ return method
+
+class TumblrClient:
+ def __init__(self, base_path):
+ self.base_path = base_path
+
+ def __getattr__(self, method):
+ def method(_self=self, _method=method, **params):
+ url = "%s" % (self.base_path)
+ if DEBUG: print url
+ data = self.fetch(url)
+ return data
+
+ return method
+
+ def fetch(self, url):
+ u = urllib.FancyURLopener(None)
+ usock = u.open(url)
+ rawdata = usock.read()
+ if DEBUG: print rawdata
+ usock.close()
+ return xml_parser.fromstring(rawdata)