Je Api好写,但不一定好用

Je的服务总是这么不稳定,实在没心情捣鼓下去



#coding:utf-8

__author__="[email protected]"
__version__="0.1"

import base64
import md5
import os
import simplejson
import sys
import tempfile
import time
import urllib
import urllib2
import urlparse
import jtalk

    

#########################################################################
## Api Core
#########################################################################    


class JTalkError(Exception):
    '''Base error define'''
    
class Api():
    
    DEFAULT_CACHE_TIMEOUT = 60 # cache for 1 minute
    
    _API_REALM = 'JavaEye API'
    
    
    def __init__(self,
                 username=None,
                 password=None,
                 input_encoding='utf-8',
                 request_headers=None):
        self._cache = _FileCache()
        self._urllib = urllib2
        self._cache_timeout = Api.DEFAULT_CACHE_TIMEOUT
        self._InitializeRequestHeaders(request_headers)
        self._InitializeUserAgent()
        self._InitializeJSONParameters()
        self._input_encoding = input_encoding
        self.SetCredentials(username, password)
        
    def listTalk(self,last_id=None,page=None):
        '''query talk list'''
        parameters = {}
        if last_id:
            parameters['last_id'] = last_id
        if page:
            parameters['page'] = page
        url = "http://api.iteye.com/api/twitters/list"
        json = self._FetchUrl(url,  parameters=parameters)
        return simplejson.loads(json)
    
    def repliesTalk(self,last_id=None,page=None):
        '''query my talk replies '''
        parameters = {}
        if last_id:
            parameters['last_id'] = last_id
        if page:
            parameters['page'] = page        
        url = "http://api.iteye.com/api/twitters/replies"
        json = self._FetchUrl(url,  parameters=parameters)
        return simplejson.loads(json)
    
    def allTalk(self,last_id=None,page=None):
        '''query all talk '''
        parameters = {}
        if last_id:
            parameters['last_id'] = last_id
        if page:
            parameters['page'] = page        
        url = "http://api.iteye.com/api/twitters/replies"
        json = self._FetchUrl(url,  parameters=parameters)
        return simplejson.loads(json)
    
    def createTalk(self,body,reply_to_id=None,via=None):
        '''query all talk '''
        data = {}
        if not body:
            raise JTalkError("talk body can not be null")
        else:
            data['body']=body
        if reply_to_id:
            data['reply_to_id'] = lreply_to_id
        if via:
            data['via'] = via        
        url = "http://api.iteye.com/api/twitters/create"
        json = self._FetchUrl(url,  post_data=data)
        return simplejson.loads(json)
    
        
    def SetCredentials(self, username, password):
        self._username = username
        self._password = password
        
    def SetUserAgent(self, user_agent):
        self._request_headers['User-Agent'] = user_agent
        
    def _InitializeUserAgent(self):
        user_agent = 'JTalk/%s' % jtalk.__version__
        self.SetUserAgent(user_agent)
    
    def _InitializeRequestHeaders(self, request_headers):
        if request_headers:
            self._request_headers = request_headers
        else:
            self._request_headers = {}    
        
    def _InitializeJSONParameters(self):
        self._json_param = {}      
        

    def _Encode(self, s):
        if self._input_encoding:
            return unicode(s, self._input_encoding).encode('utf-8')
        else:
            return unicode(s).encode('utf-8')

    def _EncodeParameters(self, parameters):
        if parameters is None:
            return None
        else:
            return urllib.urlencode(dict([(k, self._Encode(v)) for k, v in parameters.items() if v is not None]))

        
    def _EncodePostData(self, post_data):
        if post_data is None:
            return None
        else:
            return urllib.urlencode(dict([(k, self._Encode(v)) for k, v in post_data.items()]))
        
    def _FetchUrl(self,
                url,
                post_data=None,
                parameters=None,
                no_cache=None):

        url = self._BuildUrl(url, extra_params=parameters)
        opener = self._GetOpener(url, username=self._username, password=self._password)
        encoded_post_data = self._EncodePostData(post_data)
        if encoded_post_data or no_cache or not self._cache or not self._cache_timeout:
            url_data = opener.open(url, encoded_post_data).read()
        else:
            if self._username:
                key = self._username + ':' + url
            else:
                key = url

            last_cached = self._cache.GetCachedTime(key)

            if not last_cached or time.time() >= last_cached + self._cache_timeout:
                url_data = opener.open(url, encoded_post_data).read()
                self._cache.Set(key, url_data)
            else:
                url_data = self._cache.Get(key)
        return url_data    
    
    def _BuildUrl(self, url, path_elements=None, extra_params=None):
        (scheme, netloc, path, params, query, fragment) = urlparse.urlparse(url)
        if path_elements:
            p = [i for i in path_elements if i]
            if not path.endswith('/'):
                path += '/'
                path += '/'.join(p)
            if extra_params and len(extra_params) > 0:
                extra_query = self._EncodeParameters(extra_params)
                if query:
                    query += '&' + extra_query
                else:
                    query = extra_query
        return urlparse.urlunparse((scheme, netloc, path, params, query, fragment))
    
    def _AddAuthorizationHeader(self, username, password):
        if username and password:
            basic_auth = base64.encodestring('%s:%s' % (username, password))[:-1]
            self._request_headers['Authorization'] = 'Basic %s' % basic_auth
    
    def _RemoveAuthorizationHeader(self):
        if self._request_headers and 'Authorization' in self._request_headers:
            del self._request_headers['Authorization']

    def _GetOpener(self, url, username=None, password=None):
        if username and password:
            self._AddAuthorizationHeader(username, password)
            handler = self._urllib.HTTPBasicAuthHandler()
            (scheme, netloc, path, params, query, fragment) = urlparse.urlparse(url)
            handler.add_password(Api._API_REALM, netloc, username, password)
            opener = self._urllib.build_opener(handler)
        else:
            opener = self._urllib.build_opener()
            opener.addheaders = self._request_headers.items()
        return opener
        
 
#########################################################################
## 缓存处理
#########################################################################

    
class _FileCacheError(Exception):
    pass

class _FileCache(object):

    DEPTH = 3

    def __init__(self,root_directory=None):
        self._InitializeRootDirectory(root_directory)

    def Get(self,key):
        path = self._GetPath(key)
        if os.path.exists(path):
            return open(path).read()
        else:
            return None

    def Set(self,key,data):
        path = self._GetPath(key)
        directory = os.path.dirname(path)
        if not os.path.exists(directory):
            os.makedirs(directory)
        if not os.path.isdir(directory):
            raise _FileCacheError('%s exists but is not a directory' % directory)
        temp_fd, temp_path = tempfile.mkstemp()
        temp_fp = os.fdopen(temp_fd, 'w')
        temp_fp.write(data)
        temp_fp.close()
        if not path.startswith(self._root_directory):
            raise _FileCacheError('%s does not appear to live under %s' %
                                (path, self._root_directory))
        if os.path.exists(path):
            os.remove(path)
        os.rename(temp_path, path)

    def Remove(self,key):
        path = self._GetPath(key)
        if not path.startswith(self._root_directory):
            raise _FileCacheError('%s does not appear to live under %s' %
                            (path, self._root_directory ))
        if os.path.exists(path):
            os.remove(path)

    def GetCachedTime(self,key):
        path = self._GetPath(key)
        if os.path.exists(path):
            return os.path.getmtime(path)
        else:
            return None

    def _GetUsername(self):
        '''Attempt to find the username in a cross-platform fashion.'''
        return os.getenv('USER') or \
               os.getenv('LOGNAME') or \
               os.getenv('USERNAME') or \
               os.getlogin() or \
               'nobody'

    def _GetTmpCachePath(self):
        username = self._GetUsername()
        cache_directory = 'python.cache_' + username
        return os.path.join(tempfile.gettempdir(), cache_directory)

    def _InitializeRootDirectory(self, root_directory):
        if not root_directory:
            root_directory = self._GetTmpCachePath()
            root_directory = os.path.abspath(root_directory)
        if not os.path.exists(root_directory):
            os.mkdir(root_directory)
        if not os.path.isdir(root_directory):
            raise _FileCacheError('%s exists but is not a directory' %
                            root_directory)
        self._root_directory = root_directory

    def _GetPath(self,key):
        hashed_key = md5.new(key).hexdigest()
        return os.path.join(self._root_directory,
                        self._GetPrefix(hashed_key),
                        hashed_key)

    def _GetPrefix(self,hashed_key):
        return os.path.sep.join(hashed_key[0:_FileCache.DEPTH])

        


#####################################################################
## 测试
#####################################################################    

if __name__ == "__main__":
    jt = jtalk.Api(username="",password="")
    print jt.listTalk()
    
    
    
    
    




你可能感兴趣的:(json,cache,OS,twitter,FP)