Beispiel #1
0
class Api(object):
    def __init__(self, consumer_key, consumer_secret, oauth_token, oauth_token_secret):
        self._api_oauth = oauth.OAuth(consumer_key, consumer_secret, oauth_token, oauth_token_secret)
        self._json_parser = JsonParser()

    def _get_api_oauth(self):
        return self._api_oauth

    def _get_json_parser(self):
        return self._json_parser

    api_oauth = property(_get_api_oauth)
    json_parser = property(_get_json_parser)

    def _type_parser(self, content, typ):
        if typ == 'status':
            return self._json_parser.create_status_object(json.loads(content))
        elif typ == 'status_list':
            return self._json_parser.create_status_object_list(json.loads(content))
        elif typ == 'user_list':
            return self._json_parser.create_user_object_list(json.loads(content))
        elif typ == 'user':
            return self._json_parser.create_user_object(json.loads(content))
        elif typ == 'search_info':
            return self._json_parser.create_search_info(json.loads(content))

    def _get_method(self, url, param, typ, authentification=True):
        req = oauth.oauth_request(oauth=self._api_oauth,
                                  url=url,
                                  method='GET',
                                  params=param,
                                  authentification=authentification)
        try:
            #print 'DATA: %s' % req.get_data()
            #print 'METHOD: %s' % req.get_method()
            #print 'HOST: %s' % req.get_host()
            #print 'URL: %s' % req.get_full_url()
            #print 'HEADER: %s' % req.get_header('Authorization')
            #print "Error: %s" % e
            res = urllib2.urlopen(req)
            content = res.read()
            return self._type_parser(content, typ)
        except urllib2.HTTPError, e:
            print e.read()
Beispiel #2
0
 def __init__(self, consumer_key, consumer_secret, oauth_token, oauth_token_secret):
     self._api_oauth = oauth.OAuth(consumer_key, consumer_secret, oauth_token, oauth_token_secret)
     self._json_parser = JsonParser()