1.[代码][Python]代码
Python语言: 用python实现发送即时消息到twitter
#!/usr/bin/env python
#coding=utf-8
# 通过urllib2模块实现发送即时消息到Twitter.com
import re,urllib2,urllib
user = {'session[username_or_email]':'username_or_email','session[password]':'******'}
data = {
'status':"""
Send by Python!
""",
'tab':'home',
'source':'web',
}
def u(s, encoding):
if isinstance(s, unicode):
return s
else:
try:
return unicode(s, encoding)
except:
return s
def send(user=user,data=data):
c = urllib2.HTTPCookieProcessor()
builder = urllib2.build_opener(c)
url = 'https://twitter.com/sessions'
request = urllib2.Request(
url=url,
data = urllib.urlencode(user)
)
d = builder.open(request)
r = re.compile('')
x = d.read()
if len(re.compile(r"name=\"session\[username_or_email\]\"").findall(x))>0:
print "Login Error!"
return False
auth = {'authenticity_token':r.findall(x)[0]}
send = '%s&%s'%(
urllib.urlencode(auth),
urllib.urlencode(data)
)
request = urllib2.Request(
url='http://twitter.com/status/update',
data = send ,
)
builder.open(request)
return True
if __name__=="__main__":
import sys
if len(sys.argv)>1 and sys.argv[1]!="":
data["status"] = u(" ".join(sys.argv[1:]),"gb2312").encode("utf-8")
if send():
print 'ok'