利用饭否API以及Twitter的API制作该脚本
首先前期准备工作
使用python2.6版本
Install Python 2.6 in Ubuntu
At first blush, it would seem that those wanting the latest stable python, 2.6, would have to compile it themselves (or so python.org would indicate). Fortunately for us, that process, which is at best annoying, and at worst long and complicated, is spared us by our friends over at Launchpad, and their "Personal Package Archives".
In order to install anything from the PPA, you need to add them to your repositories: "deb http://ppa.launchpad.net/doko/ubuntu/ intrepid main"
For those who’ve forgotten how, or just need a refresher, hit this page for full instructions.
Once you’ve done that, just run
sudo apt-get updatesudo apt-get install python2.6
编辑/etc/apt/source.list 把那个ppa的源加进去就好 注意,该安装只是纯粹的安装,不会删除原有的python2.5版本
运行的时候也需要运行python2.6才可,所以在下面的脚本中是 #!/usr/bin/python2.6
安装simplejson-2.0.9.tar.gz
安装 twitter api for python
http://code.google.com/p/python-twitter/
在安装simplejson的时候,如果是linux版,注意修改ez_setup.py,修改
DEFAULT_VERSION = "0.6c7"
中的0.6c7为0.6c9 否则 在python2.6 setup.py build
python2.6 setup.py install 中会提示无法下载
http://pypi.python.org/packages/2.6/s/setuptools/setuptools-0.6c7-py2.6.egg
实际上是因为该tool已经更新至
http://pypi.python.org/packages/2.6/s/setuptools/setuptools-0.6c9-py2.6.egg
修改之后simplejson安装顺利完成
然后安装twitter api for python
如果想测试是否安装成功
可以在terminal中输入python2.6
>> import simplejson
>> import twitter
如果没提示错误,则说明安装成功
在
>> import twitter
的时候会有一提示
twitter.py:12: DeprecationWarning: the md5 module is deprecated; use hashlib instead
在查 http://docs.python.org/library/md5.html
Deprecated since version 2.5: Use the hashlib module instead.
该错误无视即可
至于为何要使用python2.6
是因为我脚本中用了
content.append(‘{0} says {1}’.format(q,a))
这个format只有在python2.6之后才有效果
目的是为了把在饭否上回复我的人的name和内容合并一块再发消息给我
中间编码unicode anscii转码的问题,耽误了很久时间
可能是linux的sys.defaultencoding 是 anscii
而windows也许会智能根据网页内容调整为utf-8??
我只能这么猜测了,目前win linux下都运行正常
关于unicode anscii的python编码问题
可以参考
http://bbs.ujn.edu.cn/showthread.php?threadid=251448
# 在twitter上建立一个马甲帐号,用来发饭否上抓到有人发给我饭否帐号的回复的时候,把回复人的id以及回复内容,转发到twitter,这样我的主力帐号就可以看到马甲帐号发的内容并且有提示,这样就可以及时回复饭否上的回复
至于如何及时回复… 可以利用Bot来中转即可,即发一个有明显特征的前缀比如fanfou @饭否某人id 内容 把该内容发给twitter的马甲帐号即可,用direct message or @马甲 之类的方式应该是都可以的。该脚本待下次有空再写。因为饭否上貌似回复俺的人不多=,=!! 脚本依然是用crontab上运行,每隔2分钟一次,希望不会被饭否封掉ip,这么频繁的打开那个xml页面 =,=!! 不过应该是有过滤不用很怕,至少twitter 2分钟运行一次都没什么问题
#!/usr/bin/python2.6
# -*- coding: utf-8 -*-
'''
Created on 2009-4-2
@author: jimey
'''
from xml.dom.minidom import parseString
import urllib2
import urllib
import sys
import twitter
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
def main():
f = open('/home/jimey/fanfou/fanfoulast.txt','r')
maxid = str(f.read())
params = urllib.urlencode({'since_id':maxid})
url = "http://api.fanfou.com/statuses/replies.xml?%s"
username = 'jimey'
password = 'password'
passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, url, username, password)
authhandler = urllib2.HTTPBasicAuthHandler(passman)
opener = urllib2.build_opener(authhandler)
urllib2.install_opener(opener)
f = urllib2.urlopen(url % params)
xmltext = f.read()
dom = parseString(xmltext)
statuses = dom.childNodes[0]
id = []
messages = []
name = []
content = []
for node in statuses.childNodes:
if node.nodeName == 'status':
status = node
for snode in status.childNodes:
if snode.nodeName == 'id':
id.append(snode.childNodes[0].nodeValue)
elif snode.nodeName == 'text':
messages.append(snode.childNodes[0].nodeValue)
elif snode.nodeName == 'user':
for unode in snode.childNodes:
if unode.nodeName == 'name':
name.append(unode.childNodes[0].nodeValue)
for q, a in zip(name,messages):
content.append('{0} says {1}'.format(q,a))
while len(content) > 0:
api = twitter.Api(username='jimeybot', password='password')
status = api.PostUpdate("%s" %content.pop())
f = open('fanfoulast.txt','w')
print id
maxid = id[0]
f.write(str(maxid))
f.close()
if __name__ == "__main__":
main()
windows下支援的代码
'''
Created on 2009-4-2
@author: jimey
'''
from xml.dom.minidom import parseString
import urllib2
import urllib
import sys
import twitter
def main():
f = open('fanfoulast.txt','r')
maxid = str(f.read())
params = urllib.urlencode({'since_id':maxid})
url = "http://api.fanfou.com/statuses/replies.xml?%s"
username = 'jimey'
password = 'password'
passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, url, username, password)
authhandler = urllib2.HTTPBasicAuthHandler(passman)
opener = urllib2.build_opener(authhandler)
urllib2.install_opener(opener)
f = urllib2.urlopen(url % params)
xmltext = f.read()
dom = parseString(xmltext)
statuses = dom.childNodes[0]
id = []
messages = []
name = []
content = []
s = 0
for node in statuses.childNodes:
if node.nodeName == 'status':
status = node
for snode in status.childNodes:
if snode.nodeName == 'id':
id.append(snode.childNodes[0].nodeValue)
elif snode.nodeName == 'text':
messages.append(snode.childNodes[0].nodeValue)
elif snode.nodeName == 'user':
for unode in snode.childNodes:
if unode.nodeName == 'name':
name.append(unode.childNodes[0].nodeValue)
for q, a in zip(name,messages):
content.append('{0} says {1}'.format(q,a))
while len(content) > 0:
api = twitter.Api(username='jimeybot', password='password')
status = api.PostUpdate("%s" %content.pop())
f = open('fanfoulast.txt','w')
print id
maxid = id[0]
f.write(str(maxid))
f.close()
s += 1
print "post to bot %d posts" %s
if __name__ == "__main__":
main()