本文整理汇总了Python中weibopy.api.API.mentions方法的典型用法代码示例。如果您正苦于以下问题:Python API.mentions方法的具体用法?Python API.mentions怎么用?Python API.mentions使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类weibopy.api.API
的用法示例。
在下文中一共展示了API.mentions方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Test
# 需要导入模块: from weibopy.api import API [as 别名]
# 或者: from weibopy.api.API import mentions [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)
def basicAuth(self, source, username, password):
self.auth = BasicAuthHandler(username, password)
self.api = API(self.auth,source=source)
def mentions(self):
comments = self.api.mentions()
for comment in comments:
self.obj = comment
mid = self.getAtt("id")
text = self.getAtt("text")
print("mentions---"+ str(mid) +":"+ text)
示例2: GET
# 需要导入模块: from weibopy.api import API [as 别名]
# 或者: from weibopy.api.API import mentions [as 别名]
def GET(self):
auth = OAuthHandler(oauth.APP_KEY, oauth.APP_SECRET)
auth.setToken(session.atKey[web.ctx.ip], session.atSec[web.ctx.ip])
api = API(auth)
unread = api.unread()
timeline = api.mentions()
html = '<p>%(id)s. %(user)s created %(created)s: %(text)s -- %(ref)s</p>'
h_list = []
for status in timeline:
weibo = {}
weibo['id'] = status.id
weibo['created'] = status.created_at
weibo['user'] = status.text
weibo['text'] = status.text
weibo['source'] = status.source
weibo['ref'] = ''
refer = getattr(status, 'retweeted_status', None)
if refer:
weibo['ref'] = u'{[%s]%s}'% (refer.user.name, refer.text)
h_list.append(html % weibo)
html = ''.join(h_list)
return html
示例3: compute
# 需要导入模块: from weibopy.api import API [as 别名]
# 或者: from weibopy.api.API import mentions [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
#.........这里部分代码省略.........