应用python编写简单新浪微博应用(二)
《应用python编写简单新浪微博应用(一)》一文中简单介绍了python语言实现验证授权、微博发布读取的相关代码,本文会进一步说明评论微博、转发微博、获取未读消息数、获取评论列表、关注他人、取消关注他人、显示和他人的关系、获取他人信息等功能的实现。
注意:本文中各个部分的代码仍需承接在(一)中所列出的应用验证相关代码之后。
一、评论微博消息
from weibopy.error import WeibopError; #设定用户令牌密钥. auth.setToken( atKey, atSecret ); #绑定用户验证信息. api = API(auth); #评论指定微博信息. try: #id为评论的微博id,cid为回复的评论id,comment为评论内容. #comment_ori为是否同时评论原微博(所评论微博为转发时) api.comment( id = id, cid = cid, comment = comment, comment_ori = comment_ori ); except WeibopError, e: return e.reason; return "ok";
二、转发微博消息
from weibopy.error import WeibopError; #设定用户令牌密钥. auth.setToken( atKey, atSecret ); #绑定用户验证信息. api = API(auth); #转发指定微博信息. try: #id为被转发的微博id,status为转发时添加的内容. api.repost( id = id, status = status ); except WeibopError, e: return e.reason; return "ok";
三、获取未读消息数
from weibopy.error import WeibopError; #设定用户令牌密钥. auth.setToken( atKey, atSecret ); #绑定用户验证信息. api = API(auth); #获取未读消息数. try: count = api.unread(with_new_status=1); except WeibopError, e: return e.reason; unread={}; #是否有未读的微博. unread['new_status'] = count.new_status; #未读评论数. unread['comments'] = count.comments; #未读私信数. unread['dm'] = count.dm; #未读@信息数 unread['mentions'] = count.mentions; #新粉丝数. unread['followers'] = count.followers; return unread;
四、获取评论列表
from weibopy.error import WeibopError; #设定用户令牌密钥. auth.setToken( atKey, atSecret ); #绑定用户验证信息. api = API(auth); CommentList = []; #获取指定页评论信息列表. try: timeline = api.comments_timeline( count = count, page = page ); except WeibopError, e: return e.reason; #对微博信息列表进行逐条处理. for line in timeline: comment = {}; #评论id comment["id"] = line.id; #评论用户ID comment["uid"] = line.user.id; #评论用户名称 comment["user"] = line.user.name.encode('utf-8'); #评论文字. comment["text"] = line.text.encode('utf-8'); #评论创建时间. comment["created"] = line.created_at; #评论来源. comment["source"] = line.source.encode('utf-8'); #被评论的微博. status = getattr( line, "status", None ); #被评论的微博ID. comment["wid"] = status.id; #被评论的微博用户ID. comment["wuid"] = status.user.id; reply = getattr( line, "reply_comment", None ); #如果是回复评论. if ( reply ): #评论类型. comment["rtype"] = 1; #被评论的用户ID comment["ruid"] = reply.user['id']; #被评论的用户名称. comment["ruser"] = reply.user['name'].encode('utf-8'); #评论内容. comment["rtext"] = reply.text.encode('utf-8'); #如果不是回复评论(是直接评论微博). else: #评论类型. comment["rtype"] = 0; #被评论的用户ID comment["ruid"] = status.user.id; #被评论的用户名称. comment["ruser"] = status.user.name.encode('utf-8'); #评论内容. comment["rtext"] = status.text.encode('utf-8'); CommentList.append( comment ); return CommentList;
注意:新浪微博给出的程序包的Comments类有个小问题,需要更正后才能正常调用相关功能。
weibopy/models.py 中的如下部分
class Comments(Model):
@classmethod
def parse(cls, api, json):
comments = cls(api)
for k, v in json.items():
if k == 'user':
user = User.parse(api, v)
setattr(comments, 'author', user)
setattr(comments, k, user)
elif k == 'status':
status = Status.parse(api, v)
setattr(comments, 'user', status)
elif k == 'created_at':
setattr(comments, k, parse_datetime(v))
elif k == 'reply_comment':
setattr(comments, k, User.parse(api, v))
else:
setattr(comments, k, v)
return comments
应更正为
class Comments(Model):
@classmethod
def parse(cls, api, json):
comments = cls(api)
for k, v in json.items():
if k == 'user':
user = User.parse(api, v)
setattr(comments, 'author', user)
setattr(comments, k, user)
elif k == 'status':
status = Status.parse(api, v)
setattr(comments, k, status)
elif k == 'created_at':
setattr(comments, k, parse_datetime(v))
elif k == 'reply_comment':
setattr(comments, k, User.parse(api, v))
else:
setattr(comments, k, v)
return comments
即可。
五、关注指定用户
from weibopy.error import WeibopError; #设定用户令牌密钥. auth.setToken( atKey, atSecret ); #绑定用户验证信息. api = API(auth); #关注指定用户. try: api.create_friendship( user_id = user_id ); except WeibopError, e: return e.reason; return "ok";
六、取消关注指定用户
from weibopy.error import WeibopError; #设定用户令牌密钥. auth.setToken( atKey, atSecret ); #绑定用户验证信息. api = API(auth); #取消关注指定用户. try: api.destroy_friendship( user_id = user_id ); except WeibopError, e: return e.reason; return "ok";
七、显示和指定用户的关系
from weibopy.error import WeibopError; #设定用户令牌密钥. auth.setToken( atKey, atSecret ); #绑定用户验证信息. api = API(auth); #显示和指定用户的关系. try: source, target = api.show_friendship(target_id = user_id); if source.following: if source.followed_by: #互相关注. friendship = 3; else: #我关注他. friendship = 2; else: if source.followed_by: #他关注我. friendship = 1; else: #互无关系. friendship = 0; except WeibopError, e: return e.reason; return friendship;
八、获取指定用户信息
from weibopy.error import WeibopError; #设定用户令牌密钥. auth.setToken( atKey, atSecret ); #绑定用户验证信息. api = API(auth); #获取指定用户信息. try: thisUser = {}; result = api.get_user( user_id = user_id, screen_name = screen_name ); #用户ID thisUser['id'] = result.id; #用户名称. thisUser['name'] = result.name.encode('utf-8'); #所在城市. thisUser['location'] = result.location.encode('utf-8'); #自我描述. thisUser['description'] = result.description.encode('utf-8'); #个人主页. thisUser['url'] = result.url; #头像图片地址. thisUser['profile'] = result.profile_image_url; #是否实名认证. thisUser['verified'] = result.verified; #粉丝数. thisUser['followers_count'] = result.followers_count; #关注数. thisUser['friends_count'] = result.friends_count; #微博数. thisUser['statuses_count'] = result.statuses_count; except WeibopError, e: return e.reason; return thisUser ;
本文和上文中均为涉及到的功能,参考两文中的示例代码,结合新浪给出的API文档,再适当阅读sinatpy开发包中的相关代码,相信具有一定python基础的开发者很容易就能实现各种操作。
最后一点小窍门:sinatpy中只绑定了部分常用函数,对于sinatpy中未绑定的函数,可以参考新浪API给出的json地址在代码中进行自行绑定,调用自行绑定的函数时记得需将api做为第一个参数传递。对于Modules.py中未定义的变量类型,简单的可以借用一下tags类型,复杂的就只能自己在该脚本中添加相关类型啦。
引自:http://beauty.hit.edu.cn/myStudy/Product/doc.2011-10-16.4583711134