1
Information Media Center, Kanazawa Univ.�Hiroyuki Ohno, Ph.D.�Universal Shell Programming Lab.�Tomoyuki Matsuura
You can get this slide from https://richlab.org/j/2y52
2
第4回概要(2019-05-09)
3
Overview of #4(2019-05-09)
4
恐怖!小鳥男
5
“KOTORIOTOKO”�(The Little Birdman)
6
出たな小鳥男、お前は何者だ!
7
“Kotoriotoko”, what are you?
8
小鳥男の恐るべき戦闘能力(性能) その1
9
Kotoiotoko’s terrible specs #1
10
小鳥男の恐るべき戦闘能力(性能) その2
11
Kotoiotoko’s terrible specs #2
12
小鳥男のねらい
13
What Kotoriotoko aims
Bwahaha!
14
恐怖!小鳥男を使ってみる
15
Try to use Kotoriotoko
16
1-1) Twitterへの登録(アカウント登録)
17
1-1) Get a Twitter account (1 of 2)
18
1-2) 【任意】Twitterへの登録(電話番号登録)
19
1-2) [option] Register your phone # on the account
20
1-3) 【任意】Twitterへ登録(アプリケーション登録)
21
1-3) [option] Create an application account on Twitter
22
1-4) 【任意】Twitterへの登録(アプリID類の取得)
23
1-4) [option] Get keys and tokens for the application
24
2) 小鳥男のインストール(コピー)
25
2) Install (copy) Kotoriotoko
26
3-a) 小鳥男の設定(ID登録、1-1)だけやった人向け)
27
3-a) Configuration for the people who did only 1-1)
That’s all.
28
3-b) 小鳥男の設定(ID登録、1-2)~をした人向け)
cd kotoriotoko (kotoriotokoをインストールしたディレクトリーに入る)
cd CONFIG (その中のCONFIGディレクトリーに入る )
cp COMMON.SHLIB.SAMPLE COMMON.SHLIB (サンプル設定ファイルを本番用にコピーする)
vi COMMON.SHLIB (設定ファイルを開く)
###############################################
# My account info
###############################################
readonly MY_scname='hogehoge'
readonly MY_apikey='1234567890123456789012345'
readonly MY_apisec='1234567890123456789012……'
readonly MY_atoken='1234567890-12345678901……'
readonly MY_atksec='1234567890123456789012……'
29
3-b) Configuration for the people who did up to 1-4)
cd kotoriotoko Go to kotoriotoko’s home directory
cd CONFIG And also go into “CONFIG” directory
cp COMMON.SHLIB.SAMPLE COMMON.SHLIB (Duplicate config file from the template)
vi COMMON.SHLIB (Open the config file)
###############################################
# My account info
###############################################
readonly MY_scname='hogehoge'
readonly MY_apikey='1234567890123456789012345'
readonly MY_apisec='1234567890123456789012……'
readonly MY_atoken='1234567890-12345678901……'
readonly MY_atksec='1234567890123456789012……'
30
4) 小鳥男をとりあえず動かしてみる
31
4) Try to run
32
4') 小鳥男 主なコマンドリファレンス
tweet.sh 指定した文章をツイートする。(字数制限に注意)
(例)
./tweet.sh ツイート文 ①基本形
echo ツイート文 | ./tweet.sh ②標準入力からも送れる
./tweet.sh -r ツイートID ツイート文(返信したい人のID付きで) ③返信ツイートしたい場合
./tweet.sh -f 添付したい画像ファイル名 ツイート文 ④画像(4つまで)や動画(1つまで)付きにしたい場合
retweet.sh 指定したIDのツイートをリツイートする。
(例)
./retweet.sh ツイートID
deltweet.sh 指定したIDのツイートやリツイートを削除する。
(例)
./retweet.sh ツイートID
twfav.sh 指定したIDのツイートをいいねする。 いいねを取り消す。
twunfav.sh (例)
./twfav.sh ツイートID
./twunfav.sh ツイートID
33
4') Kotoriotoko Command reference
tweet.sh Tweet the message (Be careful of the length limitation!)
[example]
./tweet.sh Hello, there. (1)Basic
echo Hello, there. | ./tweet.sh (2)You can also use STDIN.
./tweet.sh -r tweetID message_with_“@somebody“ (3)Reply
./tweet.sh -f imagefile message (4)Tweet with images (up to 4 images)
retweet.sh Retweet the tweet
[example]
./retweet.sh tweetid
deltweet.sh Deleted the tweet
[example]
./deltweet.sh tweetid
twfav.sh Likt/Unlike the tweet
twunfav.sh [example]
./twfav.sh tweetid
./twunfav.sh tweetid
34
4') 小鳥男 主なコマンドリファレンス
twtl.sh 指定したユーザー(無指定時は自分)のタイムラインを表示する
(例)
./twtl.sh ①自分のタイムライン
./twtl.sh ユーザーID ②他人のタイムライン
./twtl.sh -n 最大表示件数 ユーザーID ③-nオプションで表示件数を変更可(最大100件)
twsrch.sh 指定したキーワードに該当するツイートを表示する(友達の非公開ツイートも可、5秒に1回の頻度で使用可)
(例)
./twsrch.sh キーワード1 キーワード2 ... ①複数キーワード(AND)指定可能
./twsrch.sh -m ツイートID キーワード ... ②指定ツイートID以前のものだけ検索(過去に遡れる)
./twsrch.sh -v キーワード ... ③-vオプションでツイートの詳細情報も取得できる
./twsrch.sh --rawout=ファイル名 キーワード ... ④--rawoutオプションで生のJSONデータも取得できる
btwsrch.sh 指定キーワードを含むツイートを検索する(公開ツイートのみ、2秒に1回の頻度で使用可)
stwsrch.sh 指定キーワードを含むツイートを検索する(ストリーミング、リアルタイムに検索しつづける、ただし英数)
35
4') Kotoriotoko Command reference
twtl.sh View the user’s timeline
[example]
./twtl.sh (1)my own timeline
./twtl.sh userID (2)other’s
./twtl.sh -n num userID (3)“-n” is to change the max number of tweet to view
twsrch.sh Search out tweets by the keywords
[example]
./twsrch.sh keword1 keyword2 ... (1)multiple keywords means “AND” search
./twsrch.sh -m tweetID keyword ... (2)“-m” means to search the more previous tweets than it
./twsrch.sh -v keyword ... (3)“-v” gives you more in detail
./twsrch.sh --rawout=filename keyword ... (4)“--rawout” also gives you the original date from Twitter API
btwsrch.sh Search out tweet more frequently (up to once in 2 seconds even though “twsrch.sh” is up to once in 5 secs)
stwsrch.sh Search out tweet in streaming mode (this is proper for real-time searching)
36
4') 小鳥男 主なコマンドリファレンス
twfollow.sh 指定したユーザーをフォローする、
twunfollow.sh またはフォローをやめる
(例)
./twfollow.sh ユーザーID
./twunfollow.sh ユーザーID
twfer.sh 指定したユーザー(無指定時は自分)の
フォロワー一覧を表示する
(例)
./twfav.sh ユーザーID
./twfav.sh -n 最大表示件数 ユーザーID
twfing.sh 指定したユーザー(無指定時は自分)の
フォロー一覧を表示する
(例)
./twfing.sh ユーザーID
./twfing.sh -n 最大表示件数 ユーザーID
37
フォロー外しも、小鳥男+シェルスクリプトなら簡単だぞ
4') Kotoriotoko Command reference
twfollow.sh Follow the user
twunfollow.sh Stop following the user
[example]
./twfollow.sh userID
./twunfollow.sh userID
twfer.sh View the follower list of the user
[example]
./twfav.sh userID
./twfav.sh -n number userID
twfing.sh View the following user list of the user
[example]
./twfing.sh userID
./twfing.sh -n number userID
38
Hey, @NHK_PR. It’s easy to unfollow a large number of users and make it automatic with using Kotoriotoko and ShellScript.
4') 小鳥男 全コマンド
39
[richie@ajito ~]$ ls kotoriotoko/BIN
bretwer.sh dmtwview.sh tweet.sh twtl.sh
btwsrch.sh favtws.sh twfav.sh twunfav.sh
btwtl.sh getbtwid.sh twfer.sh twunfollow.sh
deldmtw.sh getstarted.sh twfing.sh twvideoup.sh
deltweet.sh retweet.sh twfollow.sh twview.sh
dmtweet.sh retwer.sh twmediup.sh
dmtwrcv.sh retwers.sh twplsrch.sh
dmtwsnt.sh stwsrch.sh twsrch.sh
[richie@ajito ~]$
4') All command of Kotoriotoko
40
[richie@ajito ~]$ ls kotoriotoko/BIN
bretwer.sh dmtwview.sh tweet.sh twtl.sh
btwsrch.sh favtws.sh twfav.sh twunfav.sh
btwtl.sh getbtwid.sh twfer.sh twunfollow.sh
deldmtw.sh getstarted.sh twfing.sh twvideoup.sh
deltweet.sh retweet.sh twfollow.sh twview.sh
dmtweet.sh retwer.sh twmediup.sh
dmtwrcv.sh retwers.sh twplsrch.sh
dmtwsnt.sh stwsrch.sh twsrch.sh
[richie@ajito ~]$
小鳥男の使い道を考える
41
Suggest the Kotoriotoko’s usage
42
コマンドは動詞、シェルスクリプトで作文して活きる
小鳥男に無限の可能性を与えるのはシェルスクリプト!
43
Commands should be verbs. And should be used by ShellScript.
ShellScript gives infinite ability to kotoriotoko!
44
小鳥男の応用例①
./twsrch.sh -n 100 キーワード1 | awk 'END{print NR/5}'
./twsrch.sh -n 100 キーワード1 キーワード2 … | awk 'END{print NR/5}'
45
Application of Kotoriotoko #1
./twsrch.sh -n 100 KEYWORD1 | awk 'END{print NR/5}'
./twsrch.sh -n 100 KEYWORD1 KEYWORD2 … | awk 'END{print NR/5}'
46
小鳥男の応用例②
[参考]発端→ https://twitter.com/NHK_PR/status/722229620871790594
47
#!/bin/sh
dir=/home/HOGEUSER/kotoriotoko/BIN 小鳥男コマンドのディレクトリー設定
while [ 1 ]; do
$dir/twfer.sh -n 200 | ①フォロー者の一覧
awk '{print $NF}' | (ただし一度に得られるのは200人まで)を
tr -d '()' > /tmp/list.$$.txt 一時ファイルに書き出す。
[ -s /tmp/list.$$.txt ] || break ②一時ファイルが空(=フォロー者が0人)ならばループ終了。
cat /tmp/list.$$.txt | ③フォロー者を1人ずつ
xargs -n 1 $dir/twunfollow.sh 解除する。
done
rm -f /tmp/list.$$.txt
Application of Kotoriotoko #2
48
#!/bin/sh
dir=/home/HOGEUSER/kotoriotoko/BIN set kotoriotoko's home
while [ 1 ]; do
$dir/twfer.sh -n 200 | 1)get the member IDs I'm folloing (up to 200),
awk '{print $NF}' |
tr -d '()' > /tmp/list.$$.txt and write it down into a temporary file
[ -s /tmp/list.$$.txt ] || break 2)Quit when the file already has no members
cat /tmp/list.$$.txt | 3)Unfollow the members one by one
xargs -n 1 $dir/twunfollow.sh
done
rm -f /tmp/list.$$.txt
小鳥男の応用例③
49
#!/bin/sh
twid=MY_ID 対象者のTwitterログインID
dir=/home/HOGEUSER/kotoriotoko/BIN 小鳥男コマンドのディレクトリー設定
while [ 1 ]; do
$dir/dmtwrcv.sh -n 100 | 今日の日付で
awk 'NR%4==1||NR%4==2' | 対象者からDMが来ていれば
sed 's/^.*(@//' | 起きていると見なしてループ終了
tr '\n)' ' \n' |
grep $(date '+%Y/%m/%d') |
grep $twid |
awk 'END{exit (NR>0)?0:1}' || break
$dir/dmtweet.sh --to=$twid '朝だ、起きろ!' 起きていなければ対象者にDMを送信し
sleep 60 1分待つ
done
Application of Kotoriotoko #3
50
#!/bin/sh
twid=MY_ID My twitter account ID
dir=/home/HOGEUSER/kotoriotoko/BIN kotoriotoko’s home directory
while [ 1 ]; do
$dir/dmtwrcv.sh -n 100 | Quit the script
awk 'NR%4==1||NR%4==2' | if I have sent a message (reply) this morning
sed 's/^.*(@//' | to me because it means I have already been
tr '\n)' ' \n' | wake.
grep $(date '+%Y/%m/%d') |
grep $twid |
awk 'END{exit (NR>0)?0:1}' || break
$dir/dmtweet.sh --to=$twid 'GET UP!' Send a wake-up call and wait for 1 minute.
sleep 60
done
小鳥男の応用例④
51
#!/bin/sh
dir=/home/HOGEUSER/kotoriotoko/BIN 小鳥男コマンドのディレクトリー設定
$dir/twsrch.sh -n 100 '#豆乳' | ①「#豆乳」を含むツイートを検索
awk 'NR%5==4||NR%5==0' | ②リツイート・いいね行とURL行のみ抽出
sed 's/^.*\///' | ③URL行から行末のツイートIDを抽出
tr '\n-' ' \n' | ④リツイート・いいね・ツイートIDを1行に横並びにする
grep -v RET | ⑤既に自分がリツイート済のもの(大文字”RET”)は除去
awk '{print $3}' | ⑥3列目にツイートIDのみ抽出
xargs -n 1 $dir/retweet.sh ⑦抽出したツイートIDを1つずつリツイートする
Application of Kotoriotoko #4
52
#!/bin/sh
dir=/home/HOGEUSER/kotoriotoko/BIN Kotoriotoko’s homedirectory
$dir/twsrch.sh -n 100 '#soymilk' | 1)search tweets which have the word "#soymilk"
awk 'NR%5==4||NR%5==0' | 2)pick up only lines having about retweet and URL
sed 's/^.*\///' | 3)pick up tweet-ID out of the URL string
tr '\n-' ' \n' | 4)arrange the infomations side by side
grep -v RET | 5)Remove one having "RET"(already retweeted by me)
awk '{print $3}' | 6)pick up only tweet-ID
xargs -n 1 $dir/retweet.sh 7)retweet the tweet one by one
小鳥男の応用例⑤
53
#!/bin/sh
dir=/home/HOGEUSER/kotoriotoko/BIN 小鳥男コマンドのディレクトリー設定
site=headlines.yahoo.co.jp Yahoo!ニュースのURLを検索キーワードに設定
$dir/twsrch.sh -n 100 min_retweets:1000 $site | ①1000リツイート以上のツイート最新100件を検索
awk 'NR%5==4||NR%5==0' | ②「リツイート・いいね」行と、URL行のみに絞り込む
sed 's/^- //' | ③行頭の“- ”を除去
sed 's/ret://' | ④(4行目にある)リツイート数記号の“ret:”を除去
sed 's/fav://' | ⑤(4行目にある)いいね数記号の“fav:”を除去
sed 's/^.*\///' | ⑥ツイートURLからツイートID以外の文字を除去
awk '{printf($0 "%s",(NF==2)?" ":"\n")}' | ⑦リツイート数・いいね数・ツイートIDを横並びにする
sort -k 1nr,1 | ⑧リツイート数で降順に並べ替え
head -n 3 | ⑨最初の3行(ベスト3)だけにする
awk '{print $3}' | ⑩ツイートID(各行の3列目)だけ表示する
xargs $dir/twview.sh ⑪ツイートIDから元のツイートを表示する
Application of Kotoriotoko #5
54
#!/bin/sh
dir=/home/HOGEUSER/kotoriotoko/BIN kotoriotoko's home directory
site=headlines.yahoo.com set yahoo's domain name as a search word
$dir/twsrch.sh -n 100 min_retweets:1000 $site | 1)search tweets having over 1000 retweets
awk 'NR%5==4||NR%5==0' | 2)picup lines having about retweet and URL
sed 's/^- //' | 3)shave "- " exists at the head of all lines
sed 's/ret://' | 4)shave "ret:" (the number of retweeted)
sed 's/fav://' | 5)shave "fav:" (the number of liked)
sed 's/^.*\///' | 6)pick up tweet-ID out of the URL string
awk '{printf($0 "%s",(NF==2)?" ":"\n")}' | 7)arrange the infomations side by side
sort -k 1nr,1 | 8)sort lines by retweet number (descending)
head -n 3 | 9)pick up only 3 lines (best 3)
awk '{print $3}' | 10)pick up only tweet-ID
xargs $dir/twview.sh 11)retweet the tweet one by one
宿題
55
Assignment
56
テーマ.「ツイートbot」をつくる
57
Make a tweeting bot
58
テーマ1.「ツイートbot」をつくる
問1. 定時に挨拶をつぶやくbotを作りなさい。
問2. 時刻に応じて挨拶文を替えなさい。
問3. 不定期につぶやくように工夫しなさい。
問4. その他、あたかも人間がつぶやいているように� 見える工夫をしなさい。
59
Make a tweeting bot
1. Make a bot which tweets something on 0,6,12,18 o’clock.
2. Modify the bot to change message each time.
3. Modify the bot to tweet as occasionally
4. Add your idea to the bot to seem as if the bot is a human.
60
テーマ1.「ツイートbot」をつくる
61
Make a tweeting bot
62
講義スライド案内 ― Lecture Slide Index
63