public
Description: Python PostRank API library by Ash Christopher
Homepage: http://www.postrank.com/developers
Clone URL: git://github.com/aiderss/python-postrank.git
root (author)
Sat Oct 18 20:34:29 -0700 2008
python-postrank / postrank.py
100644 92 lines (71 sloc) 2.944 kb
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
from urllib import urlencode
 
import httplib
 
DEFAULT_VER = "v1"
POSTRANK_URL = "api.postrank.com"
 
RAW = 0
PROCESSED = 1
 
class PostRank(object):
    def __init__(self, version=DEFAULT_VER):
        """
Create a new PostRank object.
'version' version of the api to use. Maps to the api url.
eg. http://api.postrank.com/<version>/postrank
"""
        self.version = version
 
 
    def __call__(self, resource, **kwargs):
        """
Call to the PostRank api.
 
Use the options outlined at http://postrank.com/api
 
'resource' the api resource you want to use (feed_id, feed, postrank, etc).
'**kwargs' api fields as per the api document.
eg.
postrank = PostRank()
result = postrank('feed_id', appkey='<your api key>', format='json', url='http://www.postrank.com')
 
Note:
When using the 'postrank' resource
- url[] is entered as an array of urls
- feed_id[] is entered as an array of feed_id
"""
        method="GET"
 
        # unfortunatly, no standard uri change other than this to denote requirement of POST
        if (self.version == 'v1' and resource.endswith('postrank')):
            method='POST'
            postrank_urls = []
            postrankfeed_ids = []
            # get post args
            if kwargs.has_key('url'):
                for url in kwargs.pop('url'):
                    postrank_urls.append(url)
            if kwargs.has_key('feed_id'):
                for feed_id in kwargs.pop('feed_id'):
                    postrankfeed_ids.append(feed_id)
        
        if kwargs:
            # build api args
            args = urlencode(kwargs.items())
 
        c = httplib.HTTPConnection(POSTRANK_URL)
        uri = "/%s/%s?%s" %(self.version, resource,args)
 
        try:
            if (method == "POST"):
                postargs = "%s&%s" %(urlencode({'url[]': postrank_urls}, True), urlencode({'feed_id[]': postrankfeed_ids}, True))
                c.request(method, "%s" %(uri), postargs)
            else:
                c.request(method, "%s" %(uri))
            result = c.getresponse()
        
            retVal = []
 
            if (result.status != 200): # HTTP 200
                retVal.insert(RAW, None)
                retVal.insert(PROCESSED, None)
                return retVal
 
            retVal.insert(RAW, result.read())
            # process the raw input in the format specified
            if (kwargs['format'] == "json"):
                try:
                    import simplejson
                    retVal.insert(PROCESSED, simplejson.loads(retVal[RAW]))
                except ImportError:
                    raise PostRankException("missing library: simplejson is not installed")
 
            return retVal
        finally:
            c.close()
 
class PostRankException(Exception):
    pass