summaryrefslogtreecommitdiff
path: root/lib/utils/APIClients.py
blob: 24ab97bb039a22b8ccc3a028cb10566797d2f808 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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)