Twitter 和饭否同步

Twitter饭否上看到 Zhaonc, aka Nelson 同学一直在折腾同步,我闲得无聊,也开始折腾这事。他的研究结果是,饭否发的消息最后能自动跑到 Twitter 上,并写得此文 Twitter与饭否同步。于是我非要逆向行驶,目标是搞个 Twitter 发的消息自动跑去饭否的方法,方法现在找到了,于是我开始写本文……

啰嗦完毕,说正事。那些国外的第三方工具啊,大都是不认识饭否的,认识饭否的又竟然不认识中文。Ping.fm 倒是支持 Custom URL ,我尝试写个脚本让他支持饭否,可是又发现速度奇慢,还老丢东西。最后一怒之下自己写了个脚本,用最笨的办法吧,却是最靠谱的。放到 Dreamhost 上 cron 了10分钟一次,效果奇佳。

from xml.dom.minidom import parseString
import urllib
import urllib2
import sys

def post_to_fanfou(msg):
    url = 'http://api.fanfou.com/statuses/update.xml'
    data = urllib.urlencode({'status':msg})
    req = urllib2.Request(url,data)
    username = 'raptium'
    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)
    response = urllib2.urlopen(req)

def main():
    f = open('last.txt','r')
    maxid = int(f.read())
    f.close()
    if maxid < 1166717326:
        print 'error'
        sys.exit(-1)

    params = urllib.urlencode({'since_id':maxid})
    f = urllib.urlopen("http://twitter.com/statuses/user_timeline/raptium.xml?%s" % params)
    xmltext = f.read()
    dom = parseString(xmltext)
    statuses = dom.childNodes[0]
    messages = []
    s = 0
    for node in statuses.childNodes:
        if node.nodeName == 'status':
            status = node
            for snode in status.childNodes:
                if snode.nodeName == 'id':
                    n = int(snode.childNodes[0].nodeValue)
                    if n > maxid:
                        maxid = n
                elif snode.nodeName == 'text':
                    messages.append(snode.childNodes[0].nodeValue.encode('utf-8'))
    while len(messages) > 0:
        post_to_fanfou(messages.pop())
        s = s + 1
    f = open('last.txt','w')
    f.write(str(maxid))
    f.close()
    print 'Post %d status to Fanfou.' % s

if __name__ == "__main__":
    main()

13 thoughts on “Twitter 和饭否同步

  1. Pingback: 同步 twitter 到 飯否 | phill84.org

  2. Pingback: 同步twitter到饭否 - jimey - jimey’s Life jimey’s World

  3. Pingback: Twitter开启消息保护模式后认证同步饭否 - jimey - jimey’s Life jimey’s World

  4. cail

    尝试了一下,发现这个错误
    ValueError: invalid literal for int() with base 10: ”
    已经改了用户名密码,创建了last.txt文件
    有什么建议?
    多谢

    Reply
  5. raptium

    @cail
    我不知道出错的行啊 似乎只有两个地方用到了 int()
    如果是第一个的话 你的 last.txt 是不是空的? 如果是空的 请把twitter上最后一条消息的 id 填进去

    Reply
  6. M.T.

    能不能帮我写个反的,把饭否的全部消息同步到twitter呢?
    我尝试对换链接同步,但是没成功,因为饭否那个消息的ID非数字。

    Reply
  7. raptium

    @M.T.
    饭否的消息ID还真不是数字 不过我看了一下饭否的api 也是支持 since_id 这个参数的
    你可以把代码中相关的int() 去掉 应该还是可以用的吧

    Reply
  8. Pingback: python版的饭否到twitter的同步代码。 | As Time Goes By

  9. ni

    非挨踢人士想知道上边这一大串东西要往哪里放才可以同步twitter和饭否同步……汗,但是还是希望知道哈……如果能发我邮箱就更好鸟,谢谢!

    Reply
  10. raptium

    @ni
    这段程序最好要放到一台24小时开机并且在线的机器上…… 因为是 python 写的 所以还需要装 python 其实我的做法通用性并不好 要方便还是使用 hellotxt 之类的第三方程序来解决同步问题吧~~

    Reply
  11. TsienS

    嘀嗒的服务可以的,直接从twitter网页更新,同步到hellotxt,hellotxt可以同步其他的微博,不过嘀嗒正在申请OpenCloud,应该不久能实现twitter网页同步到其他微博。

    Reply

Leave a Reply to raptium Cancel reply

Your email address will not be published. Required fields are marked *