李昱:百度产品登录协议介绍
时间:2010-09-21 来源:alexliyu
我只是分享一下技术思路而已,其他都与我无关哦。。。。你懂得。 一、python实现代码
def Get_baidu_msgs(self,model,pushitem,**kwargs):
"""
this method is used to get login to website,and put the content to the micro blog
"""
return self.Get_msgs(model,pushitem,'send_baidu_msgs',**kwargs)
def Get_msgs(self,model,pushitem,method,**kwargs):
"""
this method is used to get login to website,and put the content to the micro blog
"""
try:
content='#%s# %s 详细内容请查看:%s'% (htmllib.encoding(model.title,'utf-8'),
htmllib.encoding(htmllib.Filter_html(model.excerpt)[:70],'utf-8'),
htmllib.encoding(model.fullurl,'utf-8'))
#content=htmllib.encoding(content,'utf-8')
memcachekey=method
result=getattr(self,method)(pushitem.username,pushitem.password,content,memcachekey)
if result:
return self.Get_True(model,pushitem)
else:
return self.Get_False(model,pushitem)
except Exception,data:
return self.Get_False(model,pushitem,data)
def send_baidu_msgs(self,username,password,msg,memcachekey):
"""
send baidu msgs. use baidu username, password.
the msgs parameter is a message list, not a single string.
"""
cookie=''
if memcache.get(memcachekey):
cookie=memcache.get(memcachekey)
else:
try:
form_fields = {
"UserLoginForm[username]": username,
"UserLoginForm[password]":password,
"UserLoginForm[rememberMe]":1
}
form_data = urllib.urlencode(form_fields)
result = urlfetch.fetch(url="http://t.baidu.com/userlogin",
payload=form_data,
method=urlfetch.POST,
follow_redirects=False,
headers={'Referer':'http://t.baidu.com/',
'Content-Type': 'application/x-www-form-urlencoded',
'user-agent':'Mozilla/5.0 (Linux; U; Linux i686; en-US) AppleWebKit/525.13 (KHTML, like Gecko)_chrome/0.4.2.80 Safari/525.13'
}
)
except Exception:
return False
if result.status_code == 302:
pass
else:
return False
cookie = Cookie.SimpleCookie(result.headers.get('set-cookie', ''))
callback_url = result.headers.get('location','')
cookie=self.make_cookie_header(cookie)
#result,cookie = self.do_redirect(callback_url, cookie)
memcache.set(memcachekey,cookie,36000)
self.Add_baidu_firend(cookie)
msg=unescape(msg)
form_fields = {
"m_content":msg,
"pic_id":0,
"pic_filename":'',
"pic_id_water":0,
"pic_filename_water":'',
}
form_data = urllib.urlencode(form_fields)
try:
result = urlfetch.fetch(url="http://t.baidu.com/message/post",
payload=form_data,
method=urlfetch.POST,
follow_redirects = False,
headers={'Referer':'http://t.baidu.com/','Cookie' : cookie})
except Exception,data:
logging.info(data)
return False
if result.status_code == 200:
return True
else:
return False
def Add_baidu_firend(self,cookie):
rid=str(random.randint(100,99999))
form_fields = {
"uid":rid,
"t":'t',
}
form_data = urllib.urlencode(form_fields)
result = urlfetch.fetch(url="http://t.baidu.com/follow/setfollow",
payload=form_data,
method=urlfetch.POST,
follow_redirects = False,
headers={'Referer':'http://t.baidu.com/','Cookie' : cookie})
二、代码的简单解释 1、请第一次看本文的朋友先请看 http://www.51203344.tk/blog/2010/09/20/559007.html 或者http://blog.vsharing.com/liyu2012/A1206050.html 以便于理解 2、 Get_baidu_msgs不解释,请直接对照
相关阅读 更多 +










