Tag Archives: 同步

Syncing twitter to digu using python / 用python同步twitter到嘀咕

#!/usr/bin/env python
import base64
import simplejson as json
import os
import sys
import urllib
import urllib2

global twitter_username
global digu_username
global digu_password
twitter_username = 'username'
digu_username = 'username'
digu_password = 'password'

def post_to_digu(msg):
	url = 'http://api.minicloud.com.cn/statuses/update.json'
	data = urllib.urlencode(msg)
	req = urllib2.Request(url, data)
	req.add_header("Authorization", "Basic %s" % base64.encodestring('%s:%s' % (digu_username, digu_password))[:-1])
	handle = urllib2.urlopen(req)

def main():
	path = sys.path[0]
	if (sys.platform[:3] == 'win'):
		path += '\\'
	else:
		path += '/'
	id_file = path + 'last_id'
	last_id = 0
	if os.path.exists(id_file):
		f = open(id_file, 'r')
	        last_id = int(f.read())
        	f.close()
	else:
		f = open(id_file, 'w')
		f.write('0')
		f.close()
	url = 'http://twitter.com/statuses/user_timeline/' + twitter_username + '.json?last_id='
	updates = json.load(urllib.urlopen(url+str(last_id)))
	new_last_id = None
	n = 0
	for update in updates:
		text = update['text']
		id = update['id']
		if (text[0] != '@') and (id > last_id):
			if (new_last_id < id):
				new_last_id = id
			text = urllib.quote(text.encode('utf-8'))
			msg = {'content': text}
                        stack.append(msg)
        while (stack)
                post_to_digu(stack.pop())
	if (new_last_id != None):
		f = open(id_file, 'w')
		f.write(str(new_last_id))

if __name__ == "__main__":
    main()

同步 twitter 到 飯否

google 看到 raptium同學的 post Twitter和饭否同步

像我這種懶人自然就拿來用了

不過又不想把 twitter 上的回復也 sync 到飯否

so 加了一個 statement 把 @blah 都過濾了

如果有比我更懶得同學也想這麼做的話只要把

elif snode.nodeName == 'text':

改成

elif (snode.nodeName == 'text') and (snode.childNodes[0].nodeValue[0] != '@'):

就可以了 😀