サイズ: 4557
コメント:
|
サイズ: 4623
コメント:
|
削除された箇所はこのように表示されます。 | 追加された箇所はこのように表示されます。 |
行 22: | 行 22: |
consumer_key = "kMZiatBR3q3900JANS6TQ" consumer_secret = "Y4xXXIH34iSAOSLHGMfXEYd4QPvL96gE7KcBVdd3cE" |
consumer_key = "" consumer_secret = "" |
行 151: | 行 151: |
== メモ == * 短縮URLを展開して表示できるといいなって案があったので、そのうち書きます。 |
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 = ""
16 consumer_secret = ""
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(tweet + '\n')
53 f.close()
54
55 def notify(name, text, image):
56 pynotify.init( name )
57 n = pynotify.Notification( name, text, os.path.abspath(image) )
58 n.show()
59
60 def check(image,userid):
61 for file in glob.glob("*"):
62 if file == str(userid) + image:
63 return 1
64 return 0
65
66 def timeline(tl):
67 now = str(tl.created_at - timedelta(hours=15))
68 tweet = tl.author.screen_name + ' ' + now + '\n' + tl.text + '\n'
69
70 image = str(tl.author.profile_image_url).split('/')[-1]
71 if check(image,tl.author.id) == 0:
72 download(tl.author.profile_image_url,tl.author.id)
73
74 if blacklist(tl.author.screen_name) and ngword(tl.text):
75 print tweet
76 log_output( tweet )
77 notify( tl.author.screen_name, tl.text , str(tl.author.id) + image)
78
79
80
81 def blacklist(user):
82 for i in black:
83 if i == user:
84 return 0
85 return 1
86
87
88 def ngword(tweet):
89 m = MeCab.Tagger('-Owakati')
90 wakati = m.parse(str(tweet)).split(" ")
91 for i in wakati:
92 for j in ng:
93 if i == j:
94 return 0
95 return 1
96
97
98 tl = api.home_timeline(count=20)
99 tweetid = tl[1].id
100
101 while 1:
102 try:
103 tl = api.home_timeline(since_id = tweetid)
104 except:
105 # restart for unexpected error
106 print "error"
107 # create OAuth handler
108 auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
109 # set access token to OAuth handler
110 auth.set_access_token(access_key, access_secret)
111 # create API
112 api = tweepy.API(auth_handler=auth)
113
114 tl = api.home_timeline(since_id = tweetid)
115
116 if len(tl) > 0:
117 tweetid = tl[0].id
118 else:
119 time.sleep(30)
120
121 for i in tl[::-1]: #reverse
122 timeline(i)
123 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
ライブラリのインストールを行います。http://sourceforge.net/projects/mecab/files/からmecab-pythonをダウンロードする。
$ 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を展開して表示できるといいなって案があったので、そのうち書きます。