Exemple #1
0
class WeiboSdk():
    def __init__(self, appKey = WHERE_PLAY_KEY, appSecret = WHERE_PLAY_SECRET, redirectUri = WHERE_PLAY_CALLBACK):
        self._client = APIClient(app_key=appKey, app_secret=appSecret, redirect_uri=redirectUri)
        
        
    def users_show(self, token, uid):
        self._client.set_access_token(token, 0.0)
        data = self._client.users__show(uid=uid, source=self._client.client_id,access_token=token)
        return data
    
    def statuses_user_timeline(self,token,uid,feature,page,count):
        self._client.set_access_token(token, 0.0)
        data = self._client.statuses__user_timeline(uid=uid, source=self._client.client_id,access_token=token,feature=feature,trim_user=1,page=page,count=count)
        return data
    
    
    def friendships_friends_bilateral(self,token, uid, page, count):
        self._client.set_access_token(token, 0.0)
        data = self._client.friendships__friends__bilateral(uid=uid, source=self._client.client_id,access_token=token,page=page,count=count)
        return data
    
    
    def tags_tags_batch(self,token, uids):
        self._client.set_access_token(token, 0.0)
        data = self._client.tags__tags_batch(uids=','.join(map(str,uids)), source=self._client.client_id, access_token=token)
        return data
    
    #----------------------------------------------
    
    def statuses_user_timeline_all(self,token,uid,feature=0):
        '''
            feature    false    int    过滤类型ID,0:全部、1:原创、2:图片、3:视频、4:音乐,默认为0。
        '''
        ret = []
        page = 1
        while True:
            data = self.statuses_user_timeline(token,uid,feature,page,100)
            ret += [status['text'] for status in data['statuses']]
            if len(data['statuses']) == 0:
                break
            page += 1
        return ret
    
    def getAllBiFriendsId(self, token, uid, minStatusNumber= None,minFollowerNumber = None):
        ret = []
        page = 1
        while True:
            data = self.friendships_friends_bilateral(token,uid,page,100)
            if minStatusNumber:
                data['users'] = filter(lambda a:a['statuses_count'] > minStatusNumber, data['users'])
            if minFollowerNumber:
                data['users'] = filter(lambda a:a['followers_count'] > minFollowerNumber, data['users'])
            
            ret += [user['id'] for user in data['users']]
            if len(data['users']) == 0:
                break
            page += 1
        return ret
Exemple #2
0
class SinaSdk():
    
    RET_CODE = 'retcode'
    SINA_CALL_COUNT = 'call_sina_api_count'
    EMPTY_CONTENT =1
    SUCCESS_CODE = 0
    FAILD_CODE = -1
    ERR_MSG = 'errmsg'
    MAX_COUNT = 40000
    LIMITED_CODE = "rate"
    RATE_LIMITED = "rate limited"

    def __init__(self, appKey, appSecret, redirectUri):
        self._client = APIClient(app_key=appKey, app_secret=appSecret, redirect_uri=redirectUri)
    
    def getFriendsBilateral(self, token, uid, page):
        self._client.set_access_token(token, 0.0)
        try:
            year = strftime("%Y",localtime()) 
            mon  = strftime("%m",localtime()) 
            day  = strftime("%d",localtime()) 
            hour = strftime("%H",localtime()) 
            key = year + mon+day+hour
            if(self.redisOfflineOperator.getCount(key)==self.MAX_COUNT):
                ret = {}
                ret[self.LIMITED_CODE]=self.RATE_LIMITED
                ret[self.RET_CODE] = self.FAILD_CODE
                ret[self.ERR_MSG] = 'the request sina is error'
                return ret
            elif(self.redisOfflineOperator.getCount(key)==None):
                self.redisOfflineOperator.updateCount(key, 0)
            ret = self._client.friendships__friends__bilateral(uid=uid, count=200, page=page)
            self.redisOfflineOperator.updateCount(key, 1)
        except:
            print traceback.format_exc()
            self.redisOfflineOperator.updateCount(key, 1)
            ret = {}
            ret[self.RET_CODE] = self.FAILD_CODE
            ret[self.ERR_MSG] = 'the request sina is error'
            return ret
        ret[self.RET_CODE] = self.SUCCESS_CODE
        ret[self.ERR_MSG] = ''
        return ret

    def statusesUpdate(self, token, text):
        self._client.set_access_token(token, 0.0)
        try:
            year = strftime("%Y",localtime()) 
            mon  = strftime("%m",localtime()) 
            day  = strftime("%d",localtime()) 
            hour = strftime("%H",localtime()) 
            key = year + mon+day+hour
            if(self.redisOfflineOperator.getCount(key)==self.MAX_COUNT):
                ret = {}
                ret[self.LIMITED_CODE]=self.RATE_LIMITED
                ret[self.RET_CODE] = self.FAILD_CODE
                ret[self.ERR_MSG] = 'the request sina is error'
                return ret
            elif(self.redisOfflineOperator.getCount(key)==None):
                self.redisOfflineOperator.updateCount(key, 0)
            ret = self._client.post.statuses__update(status=text)
            self.redisOfflineOperator.updateCount(key, 1)
        except:
            print traceback.format_exc()
            self.redisOfflineOperator.updateCount(key, 1)
            ret = {}
            ret[self.RET_CODE] = self.FAILD_CODE
            ret[self.ERR_MSG] = 'the request sina is error'
            return ret
        ret[self.RET_CODE] = self.SUCCESS_CODE
        ret[self.ERR_MSG] = ''
        return ret

    def statusesUpload(self, token, text, pic):
        self._client.set_access_token(token, 0.0)
        try:
            year = strftime("%Y",localtime()) 
            mon  = strftime("%m",localtime()) 
            day  = strftime("%d",localtime()) 
            hour = strftime("%H",localtime()) 
            key = year + mon+day+hour
            if(self.redisOfflineOperator.getCount(key)==self.MAX_COUNT):
                ret = {}
                ret[self.LIMITED_CODE]=self.RATE_LIMITED
                ret[self.RET_CODE] = self.FAILD_CODE
                ret[self.ERR_MSG] = 'the request sina is error'
                return ret
            elif(self.redisOfflineOperator.getCount(key)==None):
                self.redisOfflineOperator.updateCount(key, 0)
            ret = self._client.upload.statuses__upload(status=text, pic=pic)
            self.redisOfflineOperator.updateCount(key, 1)
        except:
            print traceback.format_exc()
            self.redisOfflineOperator.updateCount(key, 1)
            ret = {}
            ret[self.RET_CODE] = self.FAILD_CODE
            ret[self.ERR_MSG] = 'the request sina is error'
            return ret
        
        ret[self.RET_CODE] = self.SUCCESS_CODE
        ret[self.ERR_MSG] = ''
        return ret
    
    def searchStatus(self,token,keyword,pIndex,province, city):
        self._client.set_access_token(token, 0.0)
        ret = None
        try:
            year = strftime("%Y",localtime()) 
            mon  = strftime("%m",localtime()) 
            day  = strftime("%d",localtime()) 
            hour = strftime("%H",localtime()) 
            key = year + mon+day+hour
            ret = self._client.get.search__statuses(q=keyword,page=pIndex,province=province, city=city, count=50, source=WHERE_PLAY_KEY)
            self.redisOfflineOperator.updateCount(key, 1)
            ret[self.RET_CODE] = self.SUCCESS_CODE
            ret[self.ERR_MSG] = ''
            return ret
        except:
            print '======== ret ====='
            print ret
            print traceback.format_exc()
            ret = {}
            ret[self.RET_CODE] = self.FAILD_CODE
            ret[self.ERR_MSG] = 'the request sina is error'
            return ret
    
    
    def usersShow(self, token, uid):
        self._client.set_access_token(token, 0.0)

        year = strftime("%Y",localtime()) 
        mon  = strftime("%m",localtime()) 
        day  = strftime("%d",localtime()) 
        hour = strftime("%H",localtime()) 
        ret = self._client.users__show(uid=uid, source=self._client.client_id)

        return ret

    def collectStatus(self,token,userid,pagecount):
        self._client.set_access_token(token, 0.0)
        try:
            year = strftime("%Y",localtime()) 
            mon  = strftime("%m",localtime()) 
            day  = strftime("%d",localtime()) 
            hour = strftime("%H",localtime()) 
            key = year + mon+day+hour
            if(self.redisOfflineOperator.getCount(key)==self.MAX_COUNT):
                ret = {}
                ret[self.LIMITED_CODE]=self.RATE_LIMITED
                ret[self.RET_CODE] = self.FAILD_CODE
                ret[self.ERR_MSG] = 'the request sina is error'
                return ret
            elif(self.redisOfflineOperator.getCount(key)==None):
                self.redisOfflineOperator.updateCount(key, 0)
            ret = self._client.get.statuses__user_timeline(uid=userid,count=200,page=pagecount)
            self.redisOfflineOperator.updateCount(key, 1)
        except:
            print traceback.format_exc()
            self.redisOfflineOperator.updateCount(key, 1)
            ret = {}
            ret[self.RET_CODE] = self.FAILD_CODE
            ret[self.ERR_MSG] = 'the request sina is error'
            return ret
        
        if(ret == None):
            ret = {}
            ret[self.RET_CODE] = self.EMPTY_CONTENT
            ret[self.ERR_MSG] = 'No content any more'
            return ret
        else:
            ret[self.RET_CODE] = self.SUCCESS_CODE
            ret[self.ERR_MSG] = ''
            return ret
    def searchPoiList(self, token, page, name, city, category = None):
        self._client.set_access_token(token, 0.0)
        try:
            year = strftime("%Y",localtime()) 
            mon  = strftime("%m",localtime()) 
            day  = strftime("%d",localtime()) 
            hour = strftime("%H",localtime()) 
            key = year + mon+day+hour
            if(self.redisOfflineOperator.getCount(key)==self.MAX_COUNT):
                ret = {}
                ret[self.LIMITED_CODE]=self.RATE_LIMITED
                ret[self.RET_CODE] = self.FAILD_CODE
                ret[self.ERR_MSG] = 'the request sina is error'
                return ret
            elif(self.redisOfflineOperator.getCount(key)==None):
                self.redisOfflineOperator.updateCount(key, 0)
            if category is None:
                ret = self._client.place__pois__search(source=self._client.client_id, keyword=name, count='50', page=page, city=city)
            else:
                ret = self._client.place__pois__search(source=self._client.client_id, keyword=name, count='50', page=page, city=city, category=category)
            self.redisOfflineOperator.updateCount(key, 1)
            print ret
        except:
            print traceback.format_exc()
            self.redisOfflineOperator.updateCount(key, 1)
            ret = {}
            ret[self.RET_CODE] = self.FAILD_CODE
            ret[self.ERR_MSG] = 'the request sina is error'
            return ret
        ret[self.RET_CODE] = self.SUCCESS_CODE
        ret[self.ERR_MSG] = ''
        return ret

    def searchPoiPhotoList(self, token, poiid, page):
        self._client.set_access_token(token, 0.0)
        try:
            year = strftime("%Y",localtime()) 
            mon  = strftime("%m",localtime()) 
            day  = strftime("%d",localtime()) 
            hour = strftime("%H",localtime()) 
            key = year + mon+day+hour
            if(self.redisOfflineOperator.getCount(key)==self.MAX_COUNT):
                ret = {}
                ret[self.LIMITED_CODE]=self.RATE_LIMITED
                ret[self.RET_CODE] = self.FAILD_CODE
                ret[self.ERR_MSG] = 'the request sina is error'
                return ret
            elif(self.redisOfflineOperator.getCount(key)==None):
                self.redisOfflineOperator.updateCount(key, 0)
            ret = self._client.place__pois__photos(source=self._client.client_id, count="50", poiid=poiid, page=page)
            self.redisOfflineOperator.updateCount(key, 1)
        except:
            print traceback.format_exc()
            self.redisOfflineOperator.updateCount(key, 1)
            ret = {}
            ret[self.RET_CODE] = self.FAILD_CODE
            ret[self.ERR_MSG] = 'the request sina is error'
            return ret
        ret[self.RET_CODE] = self.SUCCESS_CODE
        ret[self.ERR_MSG] = ''
        return ret
    
    def searchPoiTipList(self, token, poiid, page):
        self._client.set_access_token(token, 0.0)
        try:
            year = strftime("%Y",localtime()) 
            mon  = strftime("%m",localtime()) 
            day  = strftime("%d",localtime()) 
            hour = strftime("%H",localtime()) 
            key = year + mon+day+hour
            if(self.redisOfflineOperator.getCount(key)==self.MAX_COUNT):
                ret = {}
                ret[self.LIMITED_CODE]=self.RATE_LIMITED
                ret[self.RET_CODE] = self.FAILD_CODE
                ret[self.ERR_MSG] = 'the request sina is error'
                return ret
            elif(self.redisOfflineOperator.getCount(key)==None):
                self.redisOfflineOperator.updateCount(key, 0)
            ret = self._client.place__pois__tips(source=self._client.client_id, count="50", poiid=poiid, page=page)
            self.redisOfflineOperator.updateCount(key, 1)
        except:
            print traceback.format_exc()
            self.redisOfflineOperator.updateCount(key, 1)
            ret = {}
            ret[self.RET_CODE] = self.FAILD_CODE
            ret[self.ERR_MSG] = 'the request sina is error'
            return ret
        ret[self.RET_CODE] = self.SUCCESS_CODE
        ret[self.ERR_MSG] = ''
        return ret
    
    def getRedis(self):
            year = strftime("%Y",localtime()) 
            mon  = strftime("%m",localtime()) 
            day  = strftime("%d",localtime()) 
            hour = strftime("%H",localtime()) 
            key = year + mon+day+hour
            ret = {}
            if self.redisOfflineOperator.getCount(key)==self.MAX_COUNT:
                ret[self.LIMITED_CODE]=self.RATE_LIMITED
                ret[self.RET_CODE] = self.FAILD_CODE
                ret[self.ERR_MSG] = 'the request sina is error'
            elif self.redisOfflineOperator.getCount(key)==None:
                self.redisOfflineOperator.updateCount(key, 0)
            return ret

    def putRedis(self, num):
            year = strftime("%Y",localtime()) 
            mon  = strftime("%m",localtime()) 
            day  = strftime("%d",localtime()) 
            hour = strftime("%H",localtime()) 
            key = year + mon+day+hour
            self.redisOfflineOperator.updateCount(key, num)

    def statusesMentions(self, token, page):
        ret = self.getRedis()
        if len(ret) > 0:
            return ret
        try:
            self._client.set_access_token(token, 0.0)
            ret = self._client.statuses__mentions(source=self._client.client_id, count="200", page=page, filter_by_author = '1', trim_user='******')
            ret[self.RET_CODE] = self.SUCCESS_CODE
            ret[self.ERR_MSG] = ''
        except:
            ret = {}
            ret[self.RET_CODE] = self.FAILD_CODE
            ret[self.ERR_MSG] = 'the request sina is error'
        self.putRedis(1)
        return ret

    def commentsToMe(self, token, page):
        ret = self.getRedis()
        if len(ret) > 0:
            return ret
        try:
            self._client.set_access_token(token, 0.0)
            ret = self._client.comments__to_me(source=self._client.client_id, count="200", page=page, filter_by_author = '1')
            ret[self.RET_CODE] = self.SUCCESS_CODE
            ret[self.ERR_MSG] = ''
        except:
            ret = {}
            ret[self.RET_CODE] = self.FAILD_CODE
            ret[self.ERR_MSG] = 'the request sina is error'
        self.putRedis(1)
        return ret

    def commentsByMe(self, token, page):
        ret = self.getRedis()
        if len(ret) > 0:
            return ret
        try:
            self._client.set_access_token(token, 0.0)
            ret = self._client.comments__by_me(source=self._client.client_id, count="200", page=page)
            ret[self.RET_CODE] = self.SUCCESS_CODE
            ret[self.ERR_MSG] = ''
        except:
            ret = {}
            ret[self.RET_CODE] = self.FAILD_CODE
            ret[self.ERR_MSG] = 'the request sina is error'
        self.putRedis(1)
        return ret

    def getAttentionIds(self, token, uid):
        ret = self.getRedis()
        if len(ret) > 0:
            return ret
        try:
            self._client.set_access_token(token, 0.0)
            ret = self._client.friendships__friends__ids(source = self._client.client_id, uid = uid, count = 5000)
            ret[self.RET_CODE] = self.SUCCESS_CODE
            ret[self.ERR_MSG] = ''
        except:
            ret = {}
            ret[self.RET_CODE] = self.FAILD_CODE
            ret[self.ERR_MSG] = 'the request sina is error'
        self.putRedis(1)
        return ret