PageRenderTime 37ms CodeModel.GetById 31ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/mangafox.sh

http://gsbabil.googlecode.com/
Shell | 110 lines | 84 code | 19 blank | 7 comment | 9 complexity | a5c7b1daa02354de28f2d648b9aa0bbd MD5 | raw file
  1#!/bin/bash 
  2
  3RED='\e[1;31m'
  4CYAN='\e[1;36m'
  5NC='\e[0m' # No Color
  6YLW='\e[1;33m'
  7WHITE='\e[0;37m'
  8
  9main_url="http://www.mangafox.com/manga"
 10wget_param="--tries=10 --retry-connrefused"
 11
 12## usage
 13if [ -z $1 ]
 14then
 15	echo
 16	echo -e "${CYAN}[*]${RED} usage: `basename $0` manga_url${NC}"
 17	echo
 18	exit
 19else
 20	manga_name=`echo $1 | awk -F '/' '{for(i=NF;i>=0;i--){if(length($i)>0){print $i;break;}}}'`
 21	if [ ! -z "$2" ]
 22	then
 23		specific_chapter="$2"
 24	fi
 25fi
 26##
 27
 28function find_chapters()
 29{
 30	TMP="${manga_name}_find_chapters.tmp"
 31	
 32	echo -n -e "${CYAN}[*]${RED} Finding total chapters in ${CYAN} $manga_name ${NC}= "
 33	wget $wget_param -q -nv "${main_url}/${manga_name}/?no_warning=1" -O $TMP
 34	chapters=`cat $TMP | grep -e 'class="chico">' | grep -v -e '</td>' -e '#listing' | awk -F '"' '{print $2}' | sed 's/^\/manga\///g'`
 35
 36	count=0
 37	for c in $chapters
 38	do
 39		mkdir -p ./$c
 40		#echo $c ##debug
 41		count=$((count+1))
 42	done
 43	echo -e "${CYAN}${count}${NC}"
 44}
 45
 46
 47function parse_chapter_n_download()
 48{
 49	PAGES="pages.tmp"
 50	PAGE="page_html.tmp"
 51	
 52	for chapter in $chapters
 53	do
 54		pwd=`pwd`
 55		
 56		if [ "$specific_chapter" ]
 57		then
 58			mkdir -p "$specific_chapter" 2>/dev/null
 59			chapter=$specific_chapter
 60		fi
 61
 62		cd ./$chapter
 63		
 64		## find total number of pages in chapter
 65		echo -n -e "${CYAN}[*]${RED} Total pages in ${CYAN} $chapter ${NC}= "
 66		wget -q -nv $wget_param $main_url/$chapter -O $PAGES
 67		pages=`cat $PAGES | grep '^.*<option value=.*<\/select>.*$' -m1 | awk '{for(i=1;i<=NF;i++){if($(i-1)~"value"){print $i}}}' | sed -e 's/selected//g;s/option//g;s/[<>\/"=]//g;'`
 68		
 69		n=0
 70		for k in $pages
 71		do
 72			#echo $k ##debug
 73			n=$((n+1))
 74		done
 75		echo -e "${CYAN}$n${NC}"
 76
 77		## now i have a list of (1,2,3...).html pages
 78		for p in $pages
 79		do
 80			wget $wget_param  -q -nv $main_url/$chapter/${p}.html -O $PAGE
 81			img_url=`cat $PAGE | grep 'onclick="return enlarge();' | awk '{for(i=1;i<=NF;i++){if($i~"http://"){print $i}}}' | sed 's/src=//g;s/["=]//g'`
 82			img=`echo $img_url | awk -F '/' '{print $NF}'`
 83			echo -e -n "${CYAN}>>${WHITE} $img_url ${RED} ... ${NC}"
 84			wget $wget_param -q -nv $img_url
 85			if [ -e $img ]
 86			then
 87				echo -e "${CYAN}[done]${NC}"
 88			else
 89				echo -e "${YLW}[failed]${NC}"
 90			fi
 91		done
 92
 93		cd $pwd
 94
 95		if [ "$specific_chapter" ]
 96		then
 97			exit;
 98		fi
 99done
100}
101
102
103function main()
104{
105	rm -rf ${manga_name}
106	find_chapters
107	parse_chapter_n_download
108}
109
110main