Esempio n. 1
0
 def GET(self):   
     web.header('Content-Type','text/html; charset=utf-8', unique=True)               
     i = web.input()
     try:#callback,update token,secret
         if i.has_key('oauth_verifier'):
             credentials = {}  
             f1 = open(settings.get_home_dir() + 'token.txt','r')
             str1 = f1.read()
             f1.close()
             l1 = str1.split(';')
             if '0' == l1[2].replace(r'ss=',''):
                 credentials['oauth_token'] = l1[0].replace(r'ot=','')
                 credentials['oauth_token_secret'] = l1[1].replace(r'ots=','')
                 access_token = twitUtil.get_access_token(credentials,i.oauth_verifier)
                 #print access_token
                 ot = access_token['oauth_token']#update tokens
                 ots = access_token['oauth_token_secret']#update secret
                 sc_name = access_token['screen_name']
                 us_id = access_token['user_id']
                 ss = '1'
                 str2 = 'ot=' + ot + ';' + 'ots=' + ots + ';' + 'ss=' + ss
                 str2 += ';' + 'sc_name=' + sc_name + ';' + 'us_id=' + us_id
                 f2 = open(settings.get_home_dir() + 'token.txt','w')
                 f2.write(str2)
                 f2.close()
     except Exception,e:
         #print '---e is ',e
         pass
Esempio n. 2
0
    def apicall(self,type1):
        web.header('Content-Type','text/html; charset=utf-8', unique=True)

        access_token = {}
            
        f1 = open(settings.get_home_dir() + 'token.txt','r')
        str1 = f1.read()
        f1.close()
        l1 = str1.split(';')
        if '1' == l1[2].replace(r'ss=',''):
            access_token['oauth_token'] = l1[0].replace(r'ot=','')
            access_token['oauth_token_secret'] = l1[1].replace(r'ots=','')
            
        if web.url().startswith('/api/'):
            url1 = web.url().replace(r'/api/','',1)
        i1 = web.input().copy()


        #for new twitter ,home_timeline
        #home timeline
        pop_earned = not -1 == url1.find('statuses/home_timeline') or\
            not -1 == url1.find('statuses/user_timeline') or\
            not -1 == url1.find('statuses/show')
        if pop_earned:
            if i1.has_key('earned'):
                i1.pop('earned')

        rv = twitUtil.MakeApiCall(access_token,url1,type1,dict(i1))
        if url1.endswith('.json'):
            rv = self.sub_url(rv)
        return rv
Esempio n. 3
0
    def ApiCall1(self, call, type="GET", parameters={}):
        if parameters.has_key('x_auth_mode'):#twitter iphone xauth
            f1 = open(settings.get_home_dir() + 'token.txt','r')
            str1 = f1.read()
            f1.close()
            l1 = str1.split(';')
            print 'l1 is ', l1
            rv = u'oauth_token=' + l1[0].replace(r'ot=','').decode('utf-8')
            rv += u'&oauth_token_secret=' + l1[1].replace(r'ots=','').decode('utf-8')
            rv += u'&screen_name=' + l1[3].replace(r'sc_name=','').decode('utf-8')
            rv += u'&user_id=' + l1[4].replace(r'us_id=','').decode('utf-8')
            #rv += u'&x_auth_expires=0'
            return rv
        if parameters.has_key('oauth_nonce'):parameters.pop('oauth_nonce')
        if parameters.has_key('oauth_timestamp'):parameters.pop('oauth_timestamp')
        if parameters.has_key('oauth_consumer_key'):parameters.pop('oauth_consumer_key')
        if parameters.has_key('oauth_version'):parameters.pop('oauth_version')
        if parameters.has_key('oauth_token'):parameters.pop('oauth_token')
        if parameters.has_key('oauth_signature'):parameters.pop('oauth_signature')

        if parameters.has_key('application_id'):parameters.pop('application_id')
        if parameters.has_key('pc'):parameters.pop('pc')
        if parameters.has_key('send_error_codes'):parameters.pop('send_error_codes')

        if call.startswith('1/') or call.startswith('i/'):
            call = '1.1' + call[call.find('/'):]
        if not call.startswith('1.1/'):
            call = '1.1/' + call

        call = call.replace('statuses/mentions.json', 'statuses/mentions_timeline.json')
        
        def sub_favorite_action(action, call, parameters):
            import re
            o = re.search('favorites/' + action + '/(\d+)\.json', call)
            if o is not None:
                id = o.group(1)
                parameters['id'] = id
                return '1.1/favorites/' + action + '.json'
            else:
                return call
            
        call = sub_favorite_action('create', call, parameters)
        call = sub_favorite_action('destroy', call, parameters)
        call = call.replace('favorites.json', 'favorites/list.json')
        
        logging.debug('https://api.twitter.com/' + call + ' ' + str(parameters))
        data1 = self._FetchUrl('https://api.twitter.com/' + call , type, parameters)
        return data1 
Esempio n. 4
0
    def POST(self):
        import os.path
        import os
        web.header('Content-Type','text/html; charset=utf-8', unique=True)
        str1 = ''
        try:
            credentials,str1 = twitUtil.get_auth_url()

            ot = credentials['oauth_token']
            ots = credentials['oauth_token_secret']
            ss = '0'
            str2 = 'ot=' + ot + ';' + 'ots=' + ots + ';' + 'ss=' + ss
            f2 = open(settings.get_home_dir() + 'token.txt','w')
            f2.write(str2)
            f2.close()
        except Exception,e:
            str1 = str1 + ' ' + str(e)           
            return '<div>' + str1 + '</div>'