当前位置: 首页>>代码示例>>Python>>正文


Python API.counts方法代码示例

本文整理汇总了Python中weibopy.api.API.counts方法的典型用法代码示例。如果您正苦于以下问题:Python API.counts方法的具体用法?Python API.counts怎么用?Python API.counts使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在weibopy.api.API的用法示例。


在下文中一共展示了API.counts方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: Test

# 需要导入模块: from weibopy.api import API [as 别名]
# 或者: from weibopy.api.API import counts [as 别名]
class Test(unittest.TestCase):

    consumer_key = ""
    consumer_secret = ""

    def __init__(self):
        """ constructor """

    def getAtt(self, key):
        try:
            return self.obj.__getattribute__(key)
        except Exception as e:
            print(e)
            return ""

    def setAccessToken(self, key, secret):
        self.auth = OAuthHandler(self.consumer_key, self.consumer_secret)
        self.auth.setAccessToken(key, secret)
        self.api = API(self.auth, source=self.consumer_key)

    def basicAuth(self, source, username, password):
        self.authType = "basicauth"
        self.auth = BasicAuthHandler(username, password)
        self.api = API(self.auth, source=source)

    def counts(self):
        counts = self.api.counts(ids="1864372538,1484854960,1877120192")
        for count in counts:
            self.obj = count
            mid = self.getAtt("id")
            comments = self.getAtt("comments")
            rt = self.getAtt("rt")
            print("mentions---" + str(mid) + ":" + str(comments) + ":" + str(rt))
开发者ID:rahulsoni9,项目名称:Research,代码行数:35,代码来源:counts.py

示例2: handle

# 需要导入模块: from weibopy.api import API [as 别名]
# 或者: from weibopy.api.API import counts [as 别名]
 def handle(self, *args, **options):
     print('Start classifying influence area\n')
     YORO_CONSUMER_KEY = settings.SINA_CONSUMER_KEY
     YORO_CONSUMER_SECRET = settings.SINA_CONSUMER_SECRET
     auth = OAuthHandler(YORO_CONSUMER_KEY, YORO_CONSUMER_SECRET)
     auth.setToken('128021658f2bfdae185d89bdffb3cede','1713185d5c8208e8f1ef27a1f484ebc9')
     api = API(auth)
     
     mid_list = []
     rt_count = 0 #Count of status that retweet my status
     ids_list = []        
     tried_count = 0
     while True:
         timeline = api.user_timeline(count=200,user_id=1275017594)
         if len(timeline) == 0:
             tried_count += 1
             #print 'try again in getting timeline'
         else:
             break
         if tried_count > 3:
             raise Exception('weibo api error. No timeline got')
             break        
     for line in timeline:
         text = getAtt(line, 'text')
         retweet = getAtt(line, 'retweet')
         if retweet:
             text += getAtt(retweet, 'text') 
         if is_fashion(text):                   
             mid_list.append(str(getAtt(line, "id")))
             if len(mid_list) == 20:
                 ids_list.append(','.join(mid_list))
                 mid_list = []
     if mid_list: #append the remaining ids
         ids_list.append(','.join(mid_list))
     for ids in ids_list:
         counts = api.counts(ids=ids)
         for obj in counts:
             rt_count += getAtt(obj, 'rt')
             
     sample_size = (len(ids_list)-1)*20 + len(ids_list[-1]) if ids_list else 0                
     influence_count = long(rt_count * 50 * 3990 * 1.0/(sample_size+1)) #indirect influence
     influence_count += 3899834 #direct influence
     print influence_count
     print('Finished classifying influence area\n')
开发者ID:alexdiao,项目名称:3805,代码行数:46,代码来源:test_fashion_influence.py

示例3: compute

# 需要导入模块: from weibopy.api import API [as 别名]
# 或者: from weibopy.api.API import counts [as 别名]
def compute(str_inf_pk):
    try:
        inf = Influence.objects.get(pk=str_inf_pk)
        auth = OAuthHandler(settings.SINA_CONSUMER_KEY, settings.SINA_CONSUMER_SECRET)
        #auth.setToken(inf.sina_key, inf.sina_secret)
        auth.setToken('128021658f2bfdae185d89bdffb3cede', '1713185d5c8208e8f1ef27a1f484ebc9')  #Calculating for celebrity
        api = API(auth)
        
        followed_dict = {}
        if inf.follower_count > 1000: #only for popular users that have too many people retweet his status
            followed, cursors  = api.friends(count=200, cursor=-1)
            while True:
                for f in followed:
                    followed_dict[getAtt(f, 'screen_name')] = 1
                if cursors[1] == 0:
                    break
                followed, cursors  = api.friends(count=200, cursor=cursors[1])

        """TODO:This part seems to be unstable;Sometimes it doens work"""
        #Calculate the most influenced friends through mentions api 
        mentions = api.mentions(count=200)
        friends_dict = {}
        friends_image_dict = {}
        for m in mentions:
            user = getAtt(m, 'user')
            screen_name = getAtt(user, 'screen_name')
            if screen_name in friends_dict:
                friends_dict[screen_name] += 1
            else:
                if screen_name == inf.screen_name:
                    continue
                friends_dict[screen_name] = 1
                friends_image_dict[screen_name] = getAtt(user, 'profile_image_url')
        friends = sorted(friends_dict.iteritems(), key=operator.itemgetter(1), reverse=True)
        if followed_dict:
            friends_list = []
            backup_list = []
            for f in friends:
                if f[0] in followed_dict:
                    friends_list.append(f[0])
                else:
                    backup_list.append(f[0])
                if len(friends_list) == 4:
                    break
            friends_list.extend(backup_list[:4-len(friends_list)])
        else:
            friends_list = [f[0] for f in friends[:4]]
        influence_friends = ','.join(friends_list)
        if influence_friends:
            inf.influence_friends = influence_friends
            friends_images = [friends_image_dict[f] for f in friends_list]
        else:
            friends_images = []
        
        #Calculates the quality of followers; Also get average followers count of sampled followers
        total = 0
        active_count = 0
        counted = 0      
        followers, cursors = api.followers(user_id=inf.sina_account, cursor=-1, count=200)
        while True: #next_cursor is not zero means not the end
            for f in followers:
                counted += 1
                f_follower_count = getAtt(f, 'followers_count')
                #f_friends_count = getAtt(f, 'friends_count')
                f_status_count = getAtt(f, 'statuses_count')
                if ((f_follower_count > 50 or f_status_count > 50) and  f_follower_count + f_status_count > 75
                or f_follower_count + f_status_count > 150):
                    active_count += 1
                total += f_follower_count
            break #200 samples should be enough
            #if cursors[1] == 0:
            #    break
            #followers, cursors = api.followers(user_id=inf.sina_account, cursor=cursors[1], count=200)
        avg_follower_of_followers = total*1.0/counted if counted !=0 else 0
        active_follower_ratio = active_count * 1.0 /counted if counted !=0 else 0
        inf.active_follower_count = int(math.ceil(active_follower_ratio*inf.follower_count))
        
        
        #Calculates the average rt_count of each tweet based on 200 samples
        mid_list = []
        rt_count = 0 #Count of status that retweet my status
        ids_list = []
        timeline = api.user_timeline(user_id=inf.sina_account, count=200)
        for line in timeline:    
            mid_list.append(str(getAtt(line, "id")))
            if len(mid_list) == 20:
                ids_list.append(','.join(mid_list))
                mid_list = []
        if mid_list: #append the remaining ids
            ids_list.append(','.join(mid_list))
        if inf.status_count > 0 and not ids_list:
            raise Exception('weibo api fails') 
        for ids in ids_list:
            counts = api.counts(ids=ids)
            for obj in counts:
                rt_count += getAtt(obj, 'rt')
        sample_size = (len(ids_list)-1)*20 + len(ids_list[-1]) if ids_list else 0
        average_rt_count = rt_count*1.0/sample_size if sample_size != 0 else 0
        inf.average_rt_count = average_rt_count
            
#.........这里部分代码省略.........
开发者ID:alexdiao,项目名称:3805,代码行数:103,代码来源:test_influence.py

示例4: classify_influence

# 需要导入模块: from weibopy.api import API [as 别名]
# 或者: from weibopy.api.API import counts [as 别名]

#.........这里部分代码省略.........
    ids_list = []
    tweet_list = [] #Store the text of tweet and retweet
    rt_count_list = []
    tried_count = 0
    while True:
        timeline = api.user_timeline(user_id=inf.sina_account, count=200)
        if len(timeline) == 0 and inf.status_count >0:
            tried_count += 1
            print 'try again in getting timeline'
        else:
            break
        if tried_count > 3:
            raise Exception('weibo api error. No timeline got')
            break
        
    for line in timeline:
        text = getAtt(line, 'text')
        retweet = getAtt(line, 'retweeted_status')
        retweet_text = getAtt(retweet, 'text')
        if retweet_text:
            text += retweet_text
        tweet_list.append(text)   
        mid_list.append(str(getAtt(line, "id")))
        if len(mid_list) == 20:
            ids_list.append(','.join(mid_list))
            mid_list = []
    if mid_list: #append the remaining ids
        ids_list.append(','.join(mid_list))
    if inf.status_count > 0 and not ids_list:
        raise Exception('weibo api fails')
    tweet_list_correct = []
    correct_index = 20 
    for ids in ids_list:
        counts = api.counts(ids=ids)
        if len(counts) == 0:
            print 'error in counts!'
            correct_index += 20
            continue
        for obj in counts:
            rt_count_list.append(getAtt(obj, 'rt'))
        tweet_list_correct.extend(tweet_list[correct_index-20:correct_index])
        correct_index += 20    
    if len(tweet_list_correct) == 0 or len(tweet_list_correct) != len(rt_count_list):
        raise Exception('weibo api fails')
    print 'length of tweet list and rt_count list', len(tweet_list_correct), len(rt_count_list)
    #Remedy for those user who has posted less than 200 status
    amplify_ratio = 1.0 if len(tweet_list_correct) == 200 else 200.0/len(tweet_list_correct)
    for i in range(len(tweet_list_correct)):
        print i
        #This number 100 should be replaced by avg_follower_count
        #Use math.sqrt to boost those tweet that has not been retweeted, 
        #and smooth the effect of famous people tweeting about things not related to them 
        added_count = (rt_count_list[i]*100 + math.sqrt(inf.follower_count)) * amplify_ratio
        assigned_area = {}
        try: #In Unix environment
            from sinal import signal, SIGALRM, alarm #@UnresolvedImport
            def handler(signum, frame):
                #print 'Signal handler called with signal', signum
                raise Exception("This code block runs for too long time!")
            signal(SIGALRM, handler)
            alarm(3)
            mmseg_text = mmseg.Algorithm(tweet_list_correct[i].encode('utf-8'))
            alarm(0) #cancel the alarm after finised
        except ImportError: # In windows, SIGALRM, alarm is not available in signal module
            mmseg_text = mmseg.Algorithm(tweet_list_correct[i].encode('utf-8'))
        except: #mmseg halts for too long, process next tweet
开发者ID:alexdiao,项目名称:3805,代码行数:70,代码来源:classify_influence.py


注:本文中的weibopy.api.API.counts方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。