ログイン
編集不可のページディスカッション情報添付ファイル
"hayakawa/program/geputter"の差分

MMA
1と2のリビジョン間の差分
2011-03-30 08:44:25時点のリビジョン1
サイズ: 4610
編集者: hayakawa
コメント:
2011-03-30 08:45:09時点のリビジョン2
サイズ: 4628
編集者: hayakawa
コメント:
削除された箇所はこのように表示されます。 追加された箇所はこのように表示されます。
行 151: 行 151:
 * 動くかと。  * あとは、実行す動くかと。

geputter

端末上にTLを垂れ流しする+αのプログラム
クライアントやブラウザでスクロールしたり更新ボタンを押すのがダルイので作ってみた。 十秒毎に1ツイートを表示します。
ubuntu10.10で動作確認しました。

   1 #!/usr/bin/python
   2 # -*- coding: utf-8 -*-
   3 import tweepy
   4 import time
   5 from datetime import datetime, timedelta
   6 import pynotify
   7 import sys, urllib
   8 import os.path
   9 import glob
  10 import MeCab
  11 reload(sys)
  12 sys.setdecoding('utf-8')
  13 
  14 ####init####
  15 consumer_key = "kMZiatBR3q3900JANS6TQ"
  16 consumer_secret = "Y4xXXIH34iSAOSLHGMfXEYd4QPvL96gE7KcBVdd3cE"
  17 access_key = ""
  18 access_secret = ""
  19 
  20 # create OAuth handler
  21 auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
  22 # set access token to OAuth handler
  23 auth.set_access_token(access_key, access_secret)
  24 # create API
  25 api = tweepy.API(auth_handler=auth)
  26 
  27 #change curent directory
  28 os.chdir("data")
  29 
  30 #blacklist
  31 f = open("blacklist.txt",'r')
  32 black = f.read()
  33 black = black.split("\n")
  34 f.close()
  35 
  36 #NGword
  37 f = open("ngword.txt","r")
  38 ng = f.read().split("\n")
  39 f.close()
  40 
  41 #get_profile_image
  42 def download(url,userid):
  43     img = urllib.urlopen(str(url))
  44     localfile = open( str(userid) + os.path.basename(str(url)), 'wb')
  45     localfile.write( img.read() )
  46     img.close()
  47     localfile.close()
  48 
  49 
  50 def log_output(tweet):
  51     f = open('timeline.txt','a+')
  52     #f.write(tl.author.screen_name + ' ' + now + '\n' + tl.text + '\n\n')
  53     f.write(tweet + '\n')
  54     f.close()
  55 
  56 def notify(name, text, image):
  57     pynotify.init( name )
  58     n = pynotify.Notification( name, text, os.path.abspath(image) )
  59     n.show()
  60 
  61 def check(image,userid):
  62     for file in glob.glob("*"):
  63         if file == str(userid) + image:
  64             return 1
  65     return 0
  66 
  67 def timeline(tl):
  68     now = str(tl.created_at - timedelta(hours=15))
  69     tweet = tl.author.screen_name + ' ' + now + '\n' + tl.text + '\n'
  70     
  71     image = str(tl.author.profile_image_url).split('/')[-1]
  72     if check(image,tl.author.id) == 0:
  73         download(tl.author.profile_image_url,tl.author.id)
  74 
  75     if blacklist(tl.author.screen_name) and ngword(tl.text):
  76         print tweet
  77         log_output( tweet )
  78         notify( tl.author.screen_name, tl.text , str(tl.author.id) + image)
  79 
  80 
  81 
  82 def blacklist(user):
  83     for i in black:
  84         if i == user:
  85             return 0
  86     return 1
  87 
  88 
  89 def ngword(tweet):
  90     m = MeCab.Tagger('-Owakati')
  91     wakati = m.parse(str(tweet)).split(" ")
  92     for i in wakati:
  93         for j in ng:
  94             if i == j:
  95                 return 0
  96     return 1
  97 
  98 
  99 tl = api.home_timeline(count=20)
 100 tweetid = tl[1].id
 101 
 102 while 1:
 103     try:
 104         tl = api.home_timeline(since_id = tweetid)
 105     except:
 106         # restart for unexpected error
 107         print "error"
 108         # create OAuth handler
 109         auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
 110         # set access token to OAuth handler
 111         auth.set_access_token(access_key, access_secret)
 112         # create API
 113         api = tweepy.API(auth_handler=auth)
 114         
 115         tl = api.home_timeline(since_id = tweetid)
 116         
 117     if len(tl) > 0:
 118         tweetid = tl[0].id
 119     else:
 120         time.sleep(30)
 121         
 122     for i in tl[::-1]: #reverse
 123         timeline(i)
 124         time.sleep(10)

使い方

  • http://gitorious.org/tweepy/examples/trees/master/oauthから、getaccesstoken.pyをダウンロードする。これを実行し、Consumer keyとConsumer secretを入力すると、ブラウザが起動し認証を求められる。その後、pin numberを入力すると、access tokenが得ることができるので、ソースコードに追加する。

  • プログラムの置いてあるディレクトリにdataというディレクトリを作成し、その中に、blacklist.txt , ngword.txtというテキストファイルを作成する。前者は、ユーザーのブラックリストであり、特定のユーザのツイートを非表示にすることができる。後者は、NGワードを指定する事ができる。これは精度が低いです。

例(blacklist.txt)

gepuro
noy41

$ easy_install tweepy
$ tar zxvf mecab-0.97.tar.gz
$ cd mecab-python-0.97
$ python setup.py build
$ sudo python setup.py install
  • あとは、実行すれば動くかと。

hayakawa/program/geputter (最終更新日時 2011-03-30 16:54:07 更新者 hayakawa)