本文整理汇总了Python中weibopy.api.API类的典型用法代码示例。如果您正苦于以下问题:Python API类的具体用法?Python API怎么用?Python API使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了API类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: callback
def callback():
'''用户成功登录授权后,会回调此方法,获取access_token,完成授权'''
verifier = request.args.get('oauth_verifier', None)
# 设置之前保存在session的request_token
if 'oauth_request_token' in session:
request_token = session['oauth_request_token']
del session['oauth_request_token']
else:
return render_template('index.html')
auth_client.set_request_token(request_token.key, request_token.secret)
access_token = auth_client.get_access_token(verifier)
at_key = access_token.key
at_secret = access_token.secret
#设定用户令牌密钥
auth_client.setToken( at_key, at_secret )
#绑定用户验证信息.
api = API(auth_client)
#获取微博信息
weibo = api.verify_credentials()
if weibo is False or weibo is None:
flash(u'杯具啊,你木有开通微博吧(⊙﹏⊙)a', 'error')
return render_template('index.html')
#记录用户登录信息,更新用户微博资料
User.login(weibo=weibo)
Weibo.set(weibo=weibo, at_key=at_key, at_secret=at_secret)
#设置session
session['id'] = weibo.id
session.permanent = True
# 跳转回最初登录前的页面
back_to_url = session.get('login_back_to_url', '/')
return redirect(back_to_url)
示例2: Test
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 get_privacy(self):
privacy = self.api.get_privacy()
self.obj = privacy
ct = self.getAtt("comment")
dm = self.getAtt("dm")
real_name = self.getAtt("real_name")
geo = self.getAtt("geo")
badge = self.getAtt("badge")
print ("privacy---"+ str(ct) + str(dm) + str(real_name) + str(geo) + str(badge))
def update_privacy(self):
update_privacy = self.api.update_privacy(comment=0)
示例3: GET
def GET(self):
access_token=session.get('access_token',None)
if not access_token:
auth = OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET,web.ctx.get('homedomain')+'/callback')
auth_url = auth.get_authorization_url()
session.request_token=auth.request_token
web.seeother(auth_url)
else:
auth =OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.access_token=access_token
api=API(auth)
user=api.verify_credentials()
user_timeline=user.timeline(count=10)
print "current user is ",user.screen_name
hot_list=get_hot_list()
for user_tweet in user_timeline:
try:
hot_list.append(tuple([user_tweet.user.screen_name,
user_tweet.mid,
user_tweet.text,
user_tweet.source,
user_tweet.created_at,
None,
None]))
except AttributeError:
#no retweet_statues
continue
return render.index(user.screen_name,user.id,hot_list)
示例4: addFavorite
def addFavorite(status_id):
auth = OAuthHandler(APP_KEY, APP_SECRET)
# Get currrent user access token from session
access_token = session['oauth_access_token']
auth.setToken(access_token.key, access_token.secret)
api = API(auth)
api.create_favorite(status_id)
示例5: get
def get(self):
verifier = str(self.get_argument('oauth_verifier', None))
auth_client = _oauth()
# 设置之前保存在session的request_token
request_token = self.session['oauth_request_token']
del self.session['oauth_request_token']
self.session.save()
auth_client.set_request_token(request_token.key, request_token.secret)
access_token = auth_client.get_access_token(verifier)
current_user = auth_client.get_username()
api = API(auth_client)
self.session['me'] = api.me()
self.session['username'] = current_user
session_mc_client.set(str(api.me().id), self.session.session_id)
self.set_cookie('sid', self.session.session_id)
self.set_cookie('uid', str(api.me().id))
# 保存access_token,以后访问只需使用access_token即可
self.session['oauth_access_token'] = access_token
self.session['platform'] = 'weibo'
self.session.save()
# 跳转回最初登录前的页面
back_to_url = self.session.get('login_back_to_url', '/')
return self.redirect(back_to_url)
示例6: getFriends
def getFriends(self,name):
self.auth.setToken(self.key,self.secret)
api = API(self.auth)
try:
friends = api.friends_ids(screen_name=name)
except Exception ,e:
print e
示例7: press_sina_weibo
def press_sina_weibo():
'''''
调用新浪微博Open Api实现通过命令行写博文,功能有待完善
author: socrates
date:2012-02-06
新浪微博:@偶是正太
'''
sina_weibo_config = configparser.ConfigParser()
#读取appkey相关配置文件
try:
sina_weibo_config.readfp(open('sina_weibo_config.ini'))
except configparser.Error:
print ('read sina_weibo_config.ini failed.')
#获取需要的信息
consumer_key = sina_weibo_config.get("userinfo","CONSUMER_KEY")
consumer_secret =sina_weibo_config.get("userinfo","CONSUMER_SECRET")
token = sina_weibo_config.get("userinfo","TOKEN")
token_sercet = sina_weibo_config.get("userinfo","TOKEN_SECRET")
#调用新浪微博OpenApi(python版)
auth = OAuthHandler(consumer_key, consumer_secret)
auth.setAccessToken(token, token_sercet)
api = API(auth)
#通过命令行输入要发布的内容
weibo_content = input('Please input content:')
status = api.update_status(status=weibo_content)
print ("Press sina weibo successful, content is: %s" % status.text)
示例8: getWeibo
def getWeibo(self,name):
self.auth.setToken(self.key,self.secret)
api = API(self.auth)
start = time.time()
if self.weiboflag == False:
try:
timeline = api.user_timeline(screen_name=name,count=50)
except Exception ,e:
print e
for node in timeline:
lst = []
lst.append(node.text.encode('utf-8'))
tmp = self.getStatus(api,node.id)
if tmp == []:
lst.extend([0,0])
else:
lst.extend(tmp)
refer = getattr(node,'retweeted_status',None)
if refer:
lst.append(refer.text.encode('utf-8'))
#print refer.mid
#print refer.id
tmp = self.getStatus(api,refer.mid)
#print tmp
if tmp == []:
lst.extend[0,0]
else:
lst.extend(tmp)
else:
lst.extend(['None',0,0])
#print (len(lst))
self.updateWeibo(lst)
示例9: getFollows
def getFollows(self,name):
self.auth.setToken(self.key,self.secret)
api = API(self.auth)
try:
follows = api.followers_ids(screen_name=name)
except Exception ,e:
print e
示例10: get
def get(self):
from weibopy.auth import OAuthHandler
from weibopy.api import API
APP_KEY = "2567661656"
APP_SECRET = "ed0a7bd30de2cc4fbffbba61dc09e60b"
auth = OAuthHandler(APP_KEY, APP_SECRET, callback = 'http://localhost:8423/weibo')
auth_url = auth.get_authorization_url()
verifier = self.get_argument('oauth_verifier', None)
if verifier:
oauth_token = self.get_argument('oauth_token')
REQUEST_SECRET = self.get_secure_cookie('rs')
auth.set_request_token(oauth_token, REQUEST_SECRET)
auth.get_access_token(verifier)
api = API(auth)
res = []
for msg in api.user_timeline(count=10, user_id=1231453741):
txt = msg.__getattribute__('text')
res.append(txt)
self.write(json.dumps(res))
else:
self.set_secure_cookie('rs', auth.request_token.secret)
self.set_secure_cookie('ot', auth.request_token.key)
self.redirect(auth_url, permanent=True)
'''
Access token key: 6c21ec2ce87bf7b6c2955feee8663bc1
Access token secret: 900473fd7334c64fb7ee18f2ae6b9cf0
oauth_token_secret=900473fd7334c64fb7ee18f2ae6b9cf0&oauth_token=6c21ec2ce87bf7b6c2955feee8663bc1
api = API(auth)
status = api.update_status(status="Hello Fouland")
'''
pass
示例11: get
def get(self):
verifier = str(self.get_argument('oauth_verifier', None))
auth_client = _oauth()
# 设置之前保存在session的request_token
request_token = self.session['oauth_request_token']
del self.session['oauth_request_token']
self.session.save()
auth_client.set_request_token(request_token.key, request_token.secret)
access_token = auth_client.get_access_token(verifier)
current_user = auth_client.get_username()
api = API(auth_client)
# save the user info to database
info = api.me()
user, created = User.objects.get_or_create(weibo_id=str(info.id))
if created:
user.avatar = info.profile_image_url
user.name = info.screen_name
user.save()
self.session['me'] = info
self.session['username'] = current_user
# 保存access_token,以后访问只需使用access_token即可
self.session['oauth_access_token'] = access_token
self.session.save()
# 跳转回最初登录前的页面
back_to_url = self.session.get('login_back_to_url', '/')
return self.redirect(back_to_url)
示例12: mainPage
def mainPage(request):
session = get_current_session()
access_token_key = session['access_token_key']
access_token_secret = session['access_token_secret']
oauth_verifier = request.GET.get('oauth_verifier', '')
get_absolute_path(request)
if not access_token_key:
#params['test'] = reverse('sinaweibo.views.login', args=[], kwargs={})#, current_app=context.current_app)
login_url = reverse('sinaweibo.views.login', args=[], kwargs={})
#return shortcuts.render_to_response('test.html', params)
return http.HttpResponseRedirect(login_url)
else:
auth = OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.setToken(access_token_key, access_token_secret)
api = API(auth)
#myself = api.get_user(id=1894001933)
#screen_name = myself. __getattribute__('screen_name')
myweibo = []
myweibo_obj = api.user_timeline(count=20, page=1)
for weibo in myweibo_obj:
myweibo.append({'text': weibo.text,
'created_at': weibo.created_at,
'retweeted_status': hasattr(weibo, 'retweeted_status') and weibo.retweeted_status or None,
'source': weibo.source})
wrapper__at(myweibo)
params = {}
params['user'] = api.verify_credentials()
params['result'] = myweibo
template = get_template_uri(appContext, 'weibo.html')
return shortcuts.render_to_response(template, params)
示例13: get
def get(self):
status_id = LastID.get_by_key_name(s_name)
if not status_id:
return
tl_file = urllib.urlopen(t_timeline_url % t_name)
timeline = json.load(tl_file)
if isinstance(timeline,list):
last_id = int(status_id.twitter_last_id)
tweets_to_be_post = []
for tl in reversed(timeline):
if int(tl['id_str']) > last_id:
tweets_to_be_post.append({'id_str':tl['id_str'],'text':tl['text']})
if len(tweets_to_be_post) > 0:
auth = BasicAuthHandler(s_name,s_pass)
api = API(auth,source=s_app)
for tweet_obj in tweets_to_be_post:
cur_id = tweet_obj['id_str']
cur_tweet = tweet_obj['text']
if cur_tweet.find('#nosina') != -1 or cur_tweet.startswith('@'):
continue
tweet = replace_tweet(cur_tweet)
try:
api.update_status(tweet)
status_id.twitter_last_id = cur_id
status_id.put()
except WeibopError,e:
self.response.out.write(e)
self.response.out.write('<br>')
示例14: get_username
def get_username(self):
if self.username is None:
api = API(self)
user = api.verify_credentials()
if user:
self.username = user.screen_name
else:
raise WeibopError("Unable to get username, invalid oauth token!")
return self.username
示例15: Test
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 friends_timeline(self):
timeline = self.api.friends_timeline(count=2, page=1)
for line in timeline:
self.obj = line
mid = self.getAtt("id")
text = self.getAtt("text")
print("friends_timeline---"+ str(mid) +":"+ text)
def comments_timeline(self):
timeline = self.api.comments_timeline(count=2, page=1)
for line in timeline:
self.obj = line
mid = self.getAtt("id")
text = self.getAtt("text")
print("comments_timeline---"+ str(mid) +":"+ text)
def user_timeline(self):
timeline = self.api.user_timeline(count=5, page=1)
for line in timeline:
self.obj = line
mid = self.getAtt("id")
text = self.getAtt("text")
created_at = self.getAtt("created_at")
print("user_timeline---"+ str(mid) +":"+ str(created_at)+":"+ text)
timeline = self.api.user_timeline(count=20, page=2)
def public_timeline(self):
timeline = self.api.public_timeline(count=2, page=1)
for line in timeline:
self.obj = line
mid = self.getAtt("id")
text = self.getAtt("text")
print("public_timeline---"+ str(mid) +":"+ text)