wassr と Twitter への投稿クライアント(清楚なクライアント)
そういえばソース公開してなかったので……
自分用なので色々実験したりしててきちゃないコードです><
何か入力するとクロスポストして、
何も入力せずに Return で replies が表示されます。
replies にいる ID は tab 補完が出来ます。
Outputz に文字数を投稿します。
#!/usr/bin/env python # -*- coding: utf-8 -*- import sys, simplejson, urllib2, urllib, twitter as tw, readline from pit import Pit #source = 'profile_answer' source = '清楚なクライアント' class Wassr: def __init__(self, user, passwd): self.user = user self.passwd = passwd def post(self, text): text = text.encode('utf-8') # text = text.encode('utf-7') self.getOpener().open('http://api.wassr.jp/statuses/update.json', urllib.urlencode({'status':text,'source':source})) def getTimeline(self): url = 'http://api.wassr.jp/statuses/friends_timeline.json' r = self.getOpener().open(url) data = simplejson.loads(r.read()) return data def getReplies(self): url = 'http://api.wassr.jp/statuses/replies.json' r = self.getOpener().open(url) data = simplejson.loads(r.read()) return data def getOpener(self): passman = urllib2.HTTPPasswordMgrWithDefaultRealm() passman.add_password(None, 'api.wassr.jp', self.user, self.passwd) authhandler = urllib2.HTTPBasicAuthHandler(passman) opener = urllib2.build_opener(authhandler) opener.addheaders = [('User-agent', 'WassrClient(http://d.hatena.ne.jp/jYoshiori/)')] return opener class Outputz: def __init__(self,key,uri): self.key = key self.uri = uri def post(self,count): param = urllib.urlencode({'key':self.key,'uri':self.uri, 'size':count}) self.getOpener().open('http://outputz.com/api/post', param) def getOpener(self): opener = urllib2.build_opener() opener.addheaders = [('User-agent', 'OutputsMiniblogClient(%s)' % self.uri)] return opener if __name__ == "__main__": readline.parse_and_bind("tab: complete") wassr_config = Pit.get('wassr.jp',{'require' : {'user':'Your wassr name','password':'Your wassr password'}}) wassr = Wassr(wassr_config['user'],wassr_config['password']) twitter_config = Pit.get('twitter.com',{'require' : {'user':'Your twitter name','password':'Your twitter password'}}) twitter = tw.Api(twitter_config['user'], twitter_config['password']) friends = set() outputz_key = Pit.get('outputz.com',{'require' : {'key':'Your outputz key'}})['key'] twitter_outputz = Outputz(outputz_key, 'http://twitter.com/%s' % twitter_config['user']) wassr_outputz = Outputz(outputz_key, 'http://wassr.jp/user/%s' % wassr_config['user']) def complete(text, status): results = [x for x in friends if x.startswith(text)] + [None] return results[status] readline.set_completer(complete) while True: input = raw_input('> ') input = unicode(input,'utf-8') if input: wassr.post(input) twitter.PostUpdate(input) twitter_outputz.post(len(input)) wassr_outputz.post(len(input)) print 'update : ' + input else: print '\twassr replies\t' for data in reversed(wassr.getReplies()): print '%-12s : %s' % (data['user_login_id'] ,data['text']) friends.add(data['user_login_id']) print '\n\ttwitter replies\t' for data in reversed(twitter.GetReplies()): print '%-12s : %s' % (data.GetUser().GetScreenName() ,data.GetText()) friends.add(data.GetUser().GetScreenName())