1 | #!/bin/bash |
---|
2 | # Author: WeiYu Chen <waue _at_ nchc org tw> |
---|
3 | # License: GPL |
---|
4 | # Description: Eazily use for Nutch |
---|
5 | # . |
---|
6 | |
---|
7 | |
---|
8 | #DIALOG='dialog --backtitle " NutchEz Setup Menu -- powered by NCHC "' |
---|
9 | DIALOG=dialog |
---|
10 | |
---|
11 | # set 1 to display more for debug, |
---|
12 | VERB=0 |
---|
13 | |
---|
14 | init_nutchez () { |
---|
15 | if ! [ -e ~/.nutchez ] ;then |
---|
16 | # copy from /etc/nutch |
---|
17 | mkdir ~/.nutchez |
---|
18 | cp -rf /etc/nutch/conf ~/.nutchez/ |
---|
19 | mkdir ~/.nutchez/log |
---|
20 | cp -rf /etc/nutch/sav ~/.nutchez/ |
---|
21 | chown -R $LOGNAME:$LOGNAME ~/.nutchez |
---|
22 | fi |
---|
23 | |
---|
24 | } |
---|
25 | |
---|
26 | echo_dialog_v () { |
---|
27 | if [ $VERB -eq 1 ]; then |
---|
28 | $DIALOG --msgbox "$1" 16 51 |
---|
29 | fi |
---|
30 | } |
---|
31 | |
---|
32 | test_file () { |
---|
33 | if ! test -e "$1" ; then |
---|
34 | echo_dialog_v "test_file: \n can not find $1" |
---|
35 | echo "" > $1 |
---|
36 | else |
---|
37 | echo_dialog_v "test_file: \n Touch $1 ! \n Its content is \n `cat $1`" |
---|
38 | fi |
---|
39 | } |
---|
40 | |
---|
41 | check_if_root() { |
---|
42 | if [ ! "$UID" -eq "0" ]; then |
---|
43 | echo_dialog_v "Hi [$LOGNAME] !! " |
---|
44 | echo_dialog_v "You need to run this script \"`basename $0`\" as root." |
---|
45 | exit 1 |
---|
46 | fi |
---|
47 | } |
---|
48 | |
---|
49 | promote_tempfile () { |
---|
50 | echo_dialog_v "7. chang tmp as txt" |
---|
51 | rm ~/.nutchez/sav/n.*.txt |
---|
52 | mv /tmp/n.urls.tmp ~/.nutchez/sav/n.urls.txt |
---|
53 | mv /tmp/n.robot.tmp ~/.nutchez/sav/n.robot.txt |
---|
54 | mv /tmp/n.crawler.tmp ~/.nutchez/sav/n.crawler.txt |
---|
55 | mv /tmp/n.tomcat.tmp ~/.nutchez/sav/n.tomcat.txt |
---|
56 | } |
---|
57 | |
---|
58 | clean_tempfile () { |
---|
59 | echo_dialog_v "7. delete tmp" |
---|
60 | rm /tmp/n.*.tmp |
---|
61 | } |
---|
62 | |
---|
63 | setup_nutchez () { |
---|
64 | if ! [ -e ~/.nutchez/urls ] ; then |
---|
65 | # make url list dir |
---|
66 | mkdir ~/.nutchez/urls |
---|
67 | fi |
---|
68 | |
---|
69 | if [ -e ~/.nutchez/urls/urls.txt ] ; then |
---|
70 | rm ~/.nutchez/urls/urls.txt |
---|
71 | fi |
---|
72 | |
---|
73 | cp ~/.nutchez/sav/n.urls.txt ~/.nutchez/urls/urls.txt |
---|
74 | |
---|
75 | if [ $NOCONTINUE -eq 1 ]; then |
---|
76 | echo_dialog_v " delete the ~/.nutchez/search/*" |
---|
77 | DATE=`date +%y%m%d%H%M%S` |
---|
78 | mv ~/.nutchez/search ~/.nutchez/search-$DATE |
---|
79 | $DIALOG --msgbox "上次搜尋的結果改放到 ~/.nutchez/search-$DATE " 0 0 |
---|
80 | rm -f /tmp/search |
---|
81 | # rm -rf ~/.nutchez/search/* |
---|
82 | fi |
---|
83 | |
---|
84 | if [ -e ~/.nutchez/conf/nutch-site.xml ] ; then |
---|
85 | # set nutch-site.xml |
---|
86 | sed -i -e "4s/<value>[a-zA-Z0-9]*</<value>$ROBOT</" ~/.nutchez/conf/nutch-site.xml |
---|
87 | fi |
---|
88 | |
---|
89 | } |
---|
90 | |
---|
91 | install_tomcat (){ |
---|
92 | if ! [ -e ~/.nutchez/tomcat ] ;then |
---|
93 | # isntall tomcat to home |
---|
94 | cp -rf /opt/nutch/tomcat ~/.nutchez/ |
---|
95 | chown -R $LOGNAME:$LOGNAME ~/.nutchez/tomcat/ |
---|
96 | # make search dir |
---|
97 | if ! [ -e ~/.nutchez/search ] ;then |
---|
98 | mkdir ~/.nutchez/search |
---|
99 | fi |
---|
100 | fi |
---|
101 | # change explorer port |
---|
102 | sed -i -e "67s/<Connector port=\"[0-9]*\"/<Connector port=\"$PORT\"/" ~/.nutchez/tomcat/conf/server.xml |
---|
103 | } |
---|
104 | |
---|
105 | |
---|
106 | show_urls (){ |
---|
107 | # show urls : ok =0 ,cancel = 1 |
---|
108 | echo_dialog_v "2. show_urls !" |
---|
109 | test_file ~/.nutchez/sav/n.urls.txt |
---|
110 | echo_dialog_v "2.1 test_file ~/.nutchez/sav return : $?" |
---|
111 | # dialog begin |
---|
112 | dialog --title "請輸入你要抓取的網址(一行一個網址)" --editbox ~/.nutchez/sav/n.urls.txt 16 51 2>/tmp/n.urls.tmp |
---|
113 | RET=$? |
---|
114 | echo_dialog_v "2.1 cat url: `cat /tmp/n.urls.tmp`" |
---|
115 | return $RET |
---|
116 | } |
---|
117 | |
---|
118 | setup_robot () { |
---|
119 | test_file ~/.nutchez/sav/n.robot.txt |
---|
120 | echo_dialog_v "3. setup_robot" |
---|
121 | # dialog |
---|
122 | dialog --title "設定機器人名稱" --nocancel --inputbox " 這個爬網機器人,你要將他取名為:\n\n (ps: 這個設定只是禮貌性宣告,並不會對結果造成影響) \n" 16 55 "`cat ~/.nutchez/sav/n.robot.txt`" 2>/tmp/n.robot.tmp |
---|
123 | echo_dialog_v "3.1 cat robot : `cat /tmp/n.robot.tmp`" |
---|
124 | } |
---|
125 | |
---|
126 | setup_crawler () { |
---|
127 | echo_dialog_v "4. setup_crawler" |
---|
128 | test_file ~/.nutchez/sav/n.crawler.txt |
---|
129 | dialog --title "設定抓取深度" --nocancel --inputbox " 對於每個網址,你需要NutchEz爬多深呢?\n\n (ps: 初次體驗建議將深度設為1來感受需要多久) \n " 16 51 "`cat ~/.nutchez/sav/n.crawler.txt`" 2>/tmp/n.crawler.tmp |
---|
130 | echo_dialog_v "4.1 cat robot : `cat /tmp/n.robot.tmp`" |
---|
131 | } |
---|
132 | |
---|
133 | setup_tomcat () { |
---|
134 | echo_dialog_v "5. setup_tomcat" |
---|
135 | test_file ~/.nutchez/sav/n.tomcat.txt |
---|
136 | dialog --title "設定網頁伺服器" --nocancel --inputbox " 你希望NutchEz將網頁伺服器開在哪個port \n\n (ps: 請選擇一個沒用到的port以免造成衝突 \n 也請盡量不要設成80以免造成你誤以為是apache的混淆) \n " 16 51 "`cat ~/.nutchez/sav/n.tomcat.txt`" 2>/tmp/n.tomcat.tmp |
---|
137 | echo_dialog_v "5.1 cat tomcat : `cat /tmp/n.tomcat.tmp`" |
---|
138 | } |
---|
139 | |
---|
140 | continue_previous () { |
---|
141 | echo_dialog_v "6. setup_tomcat" |
---|
142 | $DIALOG --title "清除上次搜尋" --clear \ |
---|
143 | --yesno "你是否要清除上一次爬網所得的結果,\n否則將加入到URL列裡增加搜尋負擔 \n\n ps: 選no的話,會跑相當相當久,\n 請慎重考慮之\n" 16 51 |
---|
144 | |
---|
145 | case $? in |
---|
146 | 0) |
---|
147 | NOCONTINUE=1;; |
---|
148 | 1) |
---|
149 | NOCONTINUE=0;; |
---|
150 | 255) |
---|
151 | echo "ESC pressed.";; |
---|
152 | esac |
---|
153 | echo_dialog_v " 6continue = $CONTINUE" |
---|
154 | } |
---|
155 | |
---|
156 | final_confirm () { |
---|
157 | echo_dialog_v "7. final_confirm : start =0 , back =1 " |
---|
158 | tempfile=/tmp/n.finalcheck.tmp |
---|
159 | |
---|
160 | echo " \n 1. 你所選擇要爬取的網址為 : \n " > $tempfile |
---|
161 | cat /tmp/n.urls.tmp >> $tempfile |
---|
162 | echo " \n\n 2. 對於這個爬網機器人,你取名為 : \n" >> $tempfile |
---|
163 | cat /tmp/n.robot.tmp >> $tempfile |
---|
164 | echo " \n\n 3. 爬網的深度,你設定為 : \n " >> $tempfile |
---|
165 | cat /tmp/n.crawler.tmp >> $tempfile |
---|
166 | echo " \n\n 4. NutchEz將會把你的搜尋結果呈現在這個Port : \n " >> $tempfile |
---|
167 | cat /tmp/n.tomcat.tmp >> $tempfile |
---|
168 | if [ $NOCONTINUE -eq 0 ];then |
---|
169 | echo " \n\n 5. 是否要清除上一次的收尋結果 : \n " >> $tempfile |
---|
170 | echo_dialog_v " 7continue = $CONTINUE" |
---|
171 | echo "NO" >> $tempfile |
---|
172 | elif [ $NOCONTINUE -eq 1 ];then |
---|
173 | echo " \n\n 5. 是否要清除上一次的收尋結果繼續搜尋 : \n " >> $tempfile |
---|
174 | echo_dialog_v " 7continue = $CONTINUE" |
---|
175 | echo "YES" >> $tempfile |
---|
176 | else |
---|
177 | echo_dialog_v " 無資料可匯入 " |
---|
178 | fi |
---|
179 | MSG=`cat $tempfile` |
---|
180 | echo_dialog_v "7.1 final message :\n $MSG" |
---|
181 | #read READ |
---|
182 | $DIALOG --title "請檢查你的選擇 ! \n\n 若所有的設定都是正確的,你可以按 \"ok\",\n 若你按了 \"reset\" 則會重頭開始設定, \n 若你選擇 \"exit\" 則會跳出NutchEz的設定選單 \n ps: reset 與 exit都不會把資料記成預設值,請放心使用 " --clear \ |
---|
183 | --extra-button --extra-label "reset" --ok-label "ok" --cancel-label "exit" \ |
---|
184 | --yesno "$MSG" 26 51 |
---|
185 | RET=$? |
---|
186 | echo_dialog_v "final return = $RET" |
---|
187 | return $RET |
---|
188 | } |
---|
189 | |
---|
190 | # define paramaters |
---|
191 | |
---|
192 | set_nutchez_p () { |
---|
193 | ROBOT=`cat ~/.nutchez/sav/n.robot.txt` |
---|
194 | URLS=`cat ~/.nutchez/sav/n.urls.txt` |
---|
195 | DEPTH=`cat ~/.nutchez/sav/n.crawler.txt` |
---|
196 | PORT=`cat ~/.nutchez/sav/n.tomcat.txt` |
---|
197 | } |
---|
198 | start_crawl () { |
---|
199 | |
---|
200 | echo_dialog_v "7. start_crawl" |
---|
201 | setup_nutchez |
---|
202 | install_tomcat |
---|
203 | echo_dialog_v "/opt/nutch/bin/nutch crawl ~/.nutchez/urls -dir ~/.nutchez/search -depth $DEPTH" |
---|
204 | echo_dialog_v "nutch conf dir = $NUTCH_CONF_DIR" |
---|
205 | /opt/nutch/bin/nutch crawl ~/.nutchez/urls -dir ~/.nutchez/search -depth $DEPTH -topN 5000 -threads 1000 |
---|
206 | } |
---|
207 | |
---|
208 | start_tomcat () { |
---|
209 | echo_dialog_v "8. start_tomcat " |
---|
210 | echo_dialog_v "/opt/nutch/tomcat/bin/startup.sh" |
---|
211 | if [ -e /tmp/search ];then |
---|
212 | rm -rf /tmp/search |
---|
213 | fi |
---|
214 | ln -sf ~/.nutchez/search/ /tmp/ |
---|
215 | |
---|
216 | pid_tc=$(ps axw -eo pid,command |\ |
---|
217 | grep "catalina" | grep "java" |\ |
---|
218 | grep "start" | awk '{print $1}') |
---|
219 | if [ -z "$pid_tc" ]; then |
---|
220 | echo_dialog_v "no another tomcat is running" |
---|
221 | else |
---|
222 | echo_dialog_v "tomcat had been started and the pid is $pid_tc" |
---|
223 | echo_dialog_v "stop it first" |
---|
224 | kill -9 $pid_tc |
---|
225 | if [ -z $? ];then |
---|
226 | echo_dialog_v " tomcat ($pid_tc) is killed ..." |
---|
227 | else |
---|
228 | echo_dialog_v "kill error ..." |
---|
229 | fi |
---|
230 | fi |
---|
231 | echo "Starting Tomcat ...." |
---|
232 | ~/.nutchez/tomcat/bin/startup.sh |
---|
233 | sleep 3 |
---|
234 | } |
---|
235 | |
---|
236 | show_report () { |
---|
237 | echo_dialog_v "9. show_report " |
---|
238 | FIREFOX=`which firefox` |
---|
239 | RET=$? |
---|
240 | if [ $RET -eq 0 ];then |
---|
241 | $FIREFOX -D 0.0 http://localhost:$PORT |
---|
242 | RET=$? |
---|
243 | fi |
---|
244 | if ! [ $RET -eq 0 ];then |
---|
245 | $DIALOG --msgbox "恭喜你已經完成了! \n 你可以用瀏覽器瀏覽: \n http://host_ip:$PORT" 0 0 |
---|
246 | fi |
---|
247 | } |
---|