[66] | 1 | #!/bin/bash |
---|
| 2 | # Author: WeiYu Chen <waue _at_ nchc org tw> |
---|
| 3 | # License: GPL |
---|
| 4 | # Description: Eazily use for Nutch |
---|
| 5 | # . |
---|
| 6 | |
---|
| 7 | |
---|
[94] | 8 | #DIALOG='dialog --backtitle " NutchEz Setup Menu -- powered by NCHC "' |
---|
| 9 | DIALOG=dialog |
---|
[66] | 10 | |
---|
[69] | 11 | # set 1 to display more for debug, |
---|
[72] | 12 | VERB=0 |
---|
[66] | 13 | |
---|
[69] | 14 | init_nutchez () { |
---|
| 15 | if ! [ -e ~/.nutchez ] ;then |
---|
| 16 | # copy from /etc/nutch |
---|
| 17 | mkdir ~/.nutchez |
---|
[89] | 18 | cp -rf /etc/nutch/conf ~/.nutchez/ |
---|
[69] | 19 | mkdir ~/.nutchez/log |
---|
[89] | 20 | cp -rf /etc/nutch/sav ~/.nutchez/ |
---|
[69] | 21 | chown -R $LOGNAME:$LOGNAME ~/.nutchez |
---|
| 22 | fi |
---|
[91] | 23 | |
---|
[69] | 24 | } |
---|
| 25 | |
---|
[91] | 26 | echo_dialog_v () { |
---|
[66] | 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 |
---|
[91] | 34 | echo_dialog_v "test_file: \n can not find $1" |
---|
[66] | 35 | echo "" > $1 |
---|
| 36 | else |
---|
[91] | 37 | echo_dialog_v "test_file: \n Touch $1 ! \n Its content is \n `cat $1`" |
---|
[66] | 38 | fi |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | check_if_root() { |
---|
[94] | 42 | if [ ! "$UID" -eq "0" ]; then |
---|
[91] | 43 | echo_dialog_v "Hi [$LOGNAME] !! " |
---|
| 44 | echo_dialog_v "You need to run this script \"`basename $0`\" as root." |
---|
[66] | 45 | exit 1 |
---|
| 46 | fi |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | promote_tempfile () { |
---|
[91] | 50 | echo_dialog_v "7. chang tmp as txt" |
---|
[68] | 51 | rm ~/.nutchez/sav/n.*.txt |
---|
[69] | 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 |
---|
[66] | 56 | } |
---|
| 57 | |
---|
| 58 | clean_tempfile () { |
---|
[91] | 59 | echo_dialog_v "7. delete tmp" |
---|
[66] | 60 | rm /tmp/n.*.tmp |
---|
| 61 | } |
---|
| 62 | |
---|
[68] | 63 | setup_nutchez () { |
---|
| 64 | if ! [ -e ~/.nutchez/urls ] ; then |
---|
| 65 | # make url list dir |
---|
| 66 | mkdir ~/.nutchez/urls |
---|
[67] | 67 | fi |
---|
[68] | 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 | |
---|
[94] | 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 | |
---|
[91] | 84 | if [ -e ~/.nutchez/conf/nutch-site.xml ] ; then |
---|
[68] | 85 | # set nutch-site.xml |
---|
[91] | 86 | sed -i -e "4s/<value>[a-zA-Z0-9]*</<value>$ROBOT</" ~/.nutchez/conf/nutch-site.xml |
---|
[68] | 87 | fi |
---|
| 88 | |
---|
[67] | 89 | } |
---|
[66] | 90 | |
---|
[68] | 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 |
---|
[69] | 100 | fi |
---|
[77] | 101 | # change explorer port |
---|
| 102 | sed -i -e "67s/<Connector port=\"[0-9]*\"/<Connector port=\"$PORT\"/" ~/.nutchez/tomcat/conf/server.xml |
---|
[68] | 103 | } |
---|
| 104 | |
---|
| 105 | |
---|
[66] | 106 | show_urls (){ |
---|
| 107 | # show urls : ok =0 ,cancel = 1 |
---|
[91] | 108 | echo_dialog_v "2. show_urls !" |
---|
[69] | 109 | test_file ~/.nutchez/sav/n.urls.txt |
---|
[91] | 110 | echo_dialog_v "2.1 test_file ~/.nutchez/sav return : $?" |
---|
[66] | 111 | # dialog begin |
---|
[94] | 112 | dialog --title "請輸入你要抓取的網址(一行一個網址)" --editbox ~/.nutchez/sav/n.urls.txt 16 51 2>/tmp/n.urls.tmp |
---|
[66] | 113 | RET=$? |
---|
[91] | 114 | echo_dialog_v "2.1 cat url: `cat /tmp/n.urls.tmp`" |
---|
[66] | 115 | return $RET |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | setup_robot () { |
---|
[68] | 119 | test_file ~/.nutchez/sav/n.robot.txt |
---|
[91] | 120 | echo_dialog_v "3. setup_robot" |
---|
[66] | 121 | # dialog |
---|
[94] | 122 | dialog --title "設定機器人名稱" --nocancel --inputbox " 這個爬網機器人,你要將他取名為:\n\n (ps: 這個設定只是禮貌性宣告,並不會對結果造成影響) \n" 16 55 "`cat ~/.nutchez/sav/n.robot.txt`" 2>/tmp/n.robot.tmp |
---|
[91] | 123 | echo_dialog_v "3.1 cat robot : `cat /tmp/n.robot.tmp`" |
---|
[66] | 124 | } |
---|
| 125 | |
---|
| 126 | setup_crawler () { |
---|
[91] | 127 | echo_dialog_v "4. setup_crawler" |
---|
[68] | 128 | test_file ~/.nutchez/sav/n.crawler.txt |
---|
[94] | 129 | dialog --title "設定抓取深度" --nocancel --inputbox " 對於每個網址,你需要NutchEz爬多深呢?\n\n (ps: 初次體驗建議將深度設為1來感受需要多久) \n " 16 51 "`cat ~/.nutchez/sav/n.crawler.txt`" 2>/tmp/n.crawler.tmp |
---|
[91] | 130 | echo_dialog_v "4.1 cat robot : `cat /tmp/n.robot.tmp`" |
---|
[66] | 131 | } |
---|
| 132 | |
---|
| 133 | setup_tomcat () { |
---|
[91] | 134 | echo_dialog_v "5. setup_tomcat" |
---|
[68] | 135 | test_file ~/.nutchez/sav/n.tomcat.txt |
---|
[94] | 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 |
---|
[91] | 137 | echo_dialog_v "5.1 cat tomcat : `cat /tmp/n.tomcat.tmp`" |
---|
[66] | 138 | } |
---|
| 139 | |
---|
[94] | 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 | |
---|
[66] | 156 | final_confirm () { |
---|
[94] | 157 | echo_dialog_v "7. final_confirm : start =0 , back =1 " |
---|
[69] | 158 | tempfile=/tmp/n.finalcheck.tmp |
---|
[66] | 159 | |
---|
[94] | 160 | echo " \n 1. 你所選擇要爬取的網址為 : \n " > $tempfile |
---|
[69] | 161 | cat /tmp/n.urls.tmp >> $tempfile |
---|
[94] | 162 | echo " \n\n 2. 對於這個爬網機器人,你取名為 : \n" >> $tempfile |
---|
[66] | 163 | cat /tmp/n.robot.tmp >> $tempfile |
---|
[94] | 164 | echo " \n\n 3. 爬網的深度,你設定為 : \n " >> $tempfile |
---|
[66] | 165 | cat /tmp/n.crawler.tmp >> $tempfile |
---|
[94] | 166 | echo " \n\n 4. NutchEz將會把你的搜尋結果呈現在這個Port : \n " >> $tempfile |
---|
[66] | 167 | cat /tmp/n.tomcat.tmp >> $tempfile |
---|
[94] | 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 |
---|
[66] | 179 | MSG=`cat $tempfile` |
---|
[94] | 180 | echo_dialog_v "7.1 final message :\n $MSG" |
---|
[66] | 181 | #read READ |
---|
[94] | 182 | $DIALOG --title "請檢查你的選擇 ! \n\n 若所有的設定都是正確的,你可以按 \"ok\",\n 若你按了 \"reset\" 則會重頭開始設定, \n 若你選擇 \"exit\" 則會跳出NutchEz的設定選單 \n ps: reset 與 exit都不會把資料記成預設值,請放心使用 " --clear \ |
---|
[75] | 183 | --extra-button --extra-label "reset" --ok-label "ok" --cancel-label "exit" \ |
---|
[69] | 184 | --yesno "$MSG" 26 51 |
---|
[66] | 185 | RET=$? |
---|
[91] | 186 | echo_dialog_v "final return = $RET" |
---|
[66] | 187 | return $RET |
---|
| 188 | } |
---|
| 189 | |
---|
[67] | 190 | # define paramaters |
---|
| 191 | |
---|
[77] | 192 | set_nutchez_p () { |
---|
[69] | 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` |
---|
[77] | 197 | } |
---|
| 198 | start_crawl () { |
---|
[68] | 199 | |
---|
[91] | 200 | echo_dialog_v "7. start_crawl" |
---|
[69] | 201 | setup_nutchez |
---|
[68] | 202 | install_tomcat |
---|
[91] | 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" |
---|
[80] | 205 | /opt/nutch/bin/nutch crawl ~/.nutchez/urls -dir ~/.nutchez/search -depth $DEPTH -topN 5000 -threads 1000 |
---|
[66] | 206 | } |
---|
| 207 | |
---|
| 208 | start_tomcat () { |
---|
[91] | 209 | echo_dialog_v "8. start_tomcat " |
---|
| 210 | echo_dialog_v "/opt/nutch/tomcat/bin/startup.sh" |
---|
[72] | 211 | if [ -e /tmp/search ];then |
---|
[69] | 212 | rm -rf /tmp/search |
---|
| 213 | fi |
---|
| 214 | ln -sf ~/.nutchez/search/ /tmp/ |
---|
[88] | 215 | |
---|
[91] | 216 | pid_tc=$(ps axw -eo pid,command |\ |
---|
[94] | 217 | grep "catalina" | grep "java" |\ |
---|
| 218 | grep "start" | awk '{print $1}') |
---|
[88] | 219 | if [ -z "$pid_tc" ]; then |
---|
[94] | 220 | echo_dialog_v "no another tomcat is running" |
---|
[88] | 221 | else |
---|
[94] | 222 | echo_dialog_v "tomcat had been started and the pid is $pid_tc" |
---|
| 223 | echo_dialog_v "stop it first" |
---|
[88] | 224 | kill -9 $pid_tc |
---|
| 225 | if [ -z $? ];then |
---|
[94] | 226 | echo_dialog_v " tomcat ($pid_tc) is killed ..." |
---|
[88] | 227 | else |
---|
[94] | 228 | echo_dialog_v "kill error ..." |
---|
[88] | 229 | fi |
---|
| 230 | fi |
---|
[94] | 231 | echo "Starting Tomcat ...." |
---|
[69] | 232 | ~/.nutchez/tomcat/bin/startup.sh |
---|
[77] | 233 | sleep 3 |
---|
[66] | 234 | } |
---|
| 235 | |
---|
| 236 | show_report () { |
---|
[91] | 237 | echo_dialog_v "9. show_report " |
---|
[68] | 238 | FIREFOX=`which firefox` |
---|
| 239 | RET=$? |
---|
[94] | 240 | if [ $RET -eq 0 ];then |
---|
[68] | 241 | $FIREFOX -D 0.0 http://localhost:$PORT |
---|
[69] | 242 | RET=$? |
---|
[68] | 243 | fi |
---|
[94] | 244 | if ! [ $RET -eq 0 ];then |
---|
| 245 | $DIALOG --msgbox "恭喜你已經完成了! \n 你可以用瀏覽器瀏覽: \n http://host_ip:$PORT" 0 0 |
---|
[69] | 246 | fi |
---|
[66] | 247 | } |
---|