= geputter = 端末上にTLを垂れ流しする+αのプログラム <
> クライアントやブラウザでスクロールしたり更新ボタンを押すのがダルイので作ってみた。 十秒毎に1ツイートを表示します。<
> ubuntu10.10で動作確認しました。 {{{#!highlight python #!/usr/bin/python # -*- coding: utf-8 -*- import tweepy import time from datetime import datetime, timedelta import pynotify import sys, urllib import os.path import glob import MeCab reload(sys) sys.setdecoding('utf-8') ####init#### consumer_key = "" consumer_secret = "" access_key = "" access_secret = "" # create OAuth handler auth = tweepy.OAuthHandler(consumer_key, consumer_secret) # set access token to OAuth handler auth.set_access_token(access_key, access_secret) # create API api = tweepy.API(auth_handler=auth) #change curent directory os.chdir("data") #blacklist f = open("blacklist.txt",'r') black = f.read() black = black.split("\n") f.close() #NGword f = open("ngword.txt","r") ng = f.read().split("\n") f.close() #get_profile_image def download(url,userid): img = urllib.urlopen(str(url)) localfile = open( str(userid) + os.path.basename(str(url)), 'wb') localfile.write( img.read() ) img.close() localfile.close() def log_output(tweet): f = open('timeline.txt','a+') f.write(tweet + '\n') f.close() def notify(name, text, image): pynotify.init( name ) n = pynotify.Notification( name, text, os.path.abspath(image) ) n.show() def check(image,userid): for file in glob.glob("*"): if file == str(userid) + image: return 1 return 0 def timeline(tl): now = str(tl.created_at - timedelta(hours=15)) tweet = tl.author.screen_name + ' ' + now + '\n' + tl.text + '\n' image = str(tl.author.profile_image_url).split('/')[-1] if check(image,tl.author.id) == 0: download(tl.author.profile_image_url,tl.author.id) if blacklist(tl.author.screen_name) and ngword(tl.text): print tweet log_output( tweet ) notify( tl.author.screen_name, tl.text , str(tl.author.id) + image) def blacklist(user): for i in black: if i == user: return 0 return 1 def ngword(tweet): m = MeCab.Tagger('-Owakati') wakati = m.parse(str(tweet)).split(" ") for i in wakati: for j in ng: if i == j: return 0 return 1 tl = api.home_timeline(count=20) tweetid = tl[1].id while 1: try: tl = api.home_timeline(since_id = tweetid) except: # restart for unexpected error print "error" # create OAuth handler auth = tweepy.OAuthHandler(consumer_key, consumer_secret) # set access token to OAuth handler auth.set_access_token(access_key, access_secret) # create API api = tweepy.API(auth_handler=auth) tl = api.home_timeline(since_id = tweetid) if len(tl) > 0: tweetid = tl[0].id else: time.sleep(30) for i in tl[::-1]: #reverse timeline(i) time.sleep(10) }}} == 使い方 == * [[http://d.hatena.ne.jp/Number6/20100116/1263631863]]を参考にconsumer key等を取得する。 * プログラムの置いてあるディレクトリにdataというディレクトリを作成し、その中に、blacklist.txt , ngword.txtというテキストファイルを作成する。前者は、ユーザーのブラックリストであり、特定のユーザのツイートを非表示にすることができる。後者は、NGワードを指定する事ができる。これは精度が低いです。 例(blacklist.txt) {{{#!format text gepuro noy41 }}} * ライブラリのインストールを行います。[[http://sourceforge.net/projects/mecab/files/]]からmecab-pythonをダウンロードする。 {{{#! text $ easy_install tweepy $ tar zxvf mecab-0.97.tar.gz $ cd mecab-python-0.97 $ python setup.py build $ sudo python setup.py install }}} * あとは、実行すれば動くかと。 == メモ == * 短縮URLを展開して表示できるといいなって案があったので、そのうち書きます。