Updated
ตอนนี้สคริปต์ One Manga Downloader เวอร์ชันล่าสุด อยู่บน GitHub ครับ
The latest version of One Manga Downloader is now hosted on GitHub.
อัพเดตเนื่องในโอกาสที่บล็อกหายจากอาการเน่า xD
Change log:
- เปลี่ยนจากใช้
seqเป็น built-in command ของ bash แทน - เลิกใช้ cURL เปลี่ยนเป็นใช้ wget แทนทั้งหมด เนื่องด้วยความสามารถหลายๆ อย่าง
- เมื่อดาวน์โหลดแล้วจะทิ้งไฟล์ index.html ไว้ใน directory สำหรับตรวจสอบ timestamp กับเซิร์ฟเวอร์ ถ้าไม่เปลี่ยนก็ไม่ต้องดาวน์โหลดใหม่
- แก้บั๊ก
$base_dirมี space ในชื่อ - แก้บั๊กลืมลบไฟล์ tmp_* เมื่อดาวน์โหลดเสร็จ
- เติมเลขศูนย์ข้างหน้าให้ไฟล์ที่ chapter น้อยกว่า 100 (ยกเว้น chapter 0) เพื่อให้ CDisplay โหลดไฟล์ได้ถูกต้องตามลำดับ
- เพิ่ม option -o ถ้าต้องการให้บันทึกว่าโหลดอะไรมาบ้างลงใน onemanga.log
- ตัด option -u ออก กำหนดให้ default action เป็นการ update เสมอ
คิดว่าความสามารถหลักๆ น่าจะครบแล้ว ถ้าไม่มีบั๊กก็จะถือเป็น 2.0 final ได้ในเร็วๆ นี้ สำหรับโค้ดก็ตามด้านล่างครับ :)
#!/bin/bash # # usage: onemanga [-dlo] [-c <first chapter>[+|-<last chapter>]] <manga name> [<manga name> ...] # trap "rm -f *.jpg tmp_*;" 0 base_url="http://www.onemanga.com" base_dir=`pwd` log="$base_dir/onemanga.log" function calc() { echo "scale=2; $*" | bc -q 2>/dev/null | cut -f1 -d. } function echoes() { if [[ $2 -gt 0 ]]; then eval "for i in {${3:-1}..$2}; do echo -n '$1'; done" fi } function progressbar() { width=20 current=$1 total=$2 percent=`calc "$current / $total * 100"` stack=`calc "$current / $total * $width"` stack=${stack:=0} space=`calc "$width - $stack"` echoes " " ${#percent} 3 echo -n "$percent% [" echoes "#" $stack echoes " " $space echo "]" } while getopts ":c:dlo" op; do case $op in c) CHAPTER=1 CHAPTER_ARG="$OPTARG" ;; d) USE_DIR=1 ;; l) LATEST=1 ;; o) LOG_FILE=1 ;; \?) echo "unknown option: -$OPTARG" >&2 ERROR=1 ;; esac done shift $(($OPTIND - 1)) if [[ $CHAPTER -eq 1 && $LATEST -eq 1 ]]; then echo "invalid option: -c and -l cannot be used at the same time" >&2 ERROR=1 fi if [[ $ERROR -eq 1 ]]; then exit fi if [[ $CHAPTER = "" && $LATEST = "" ]]; then UPDATE=1 fi if [[ $CHAPTER -eq 1 ]]; then if [[ `expr index "$CHAPTER_ARG" ++` -gt 0 ]]; then first_chapter=${CHAPTER_ARG%+*} last_chapter="+" elif [[ `expr index "$CHAPTER_ARG" -` -gt 0 ]]; then first_chapter=${CHAPTER_ARG%-*} last_chapter=${CHAPTER_ARG#*-} else first_chapter=$CHAPTER_ARG last_chapter=$CHAPTER_ARG fi fi for manga_name in "$@"; do manga_name=${manga_name%/} if [[ $USE_DIR -eq 1 ]]; then mkdir -p "$base_dir/$manga_name" cd "$base_dir/$manga_name" fi display_name=`echo $manga_name | sed "s/_/ /g"` word_count=`echo $display_name | wc -w` sort_key=$((word_count + 1)) local_chapter=`ls ${manga_name}_*.cbr 2> /dev/null | sort -r -n -k $sort_key -t_ | head -n1` local_chapter=`basename "${local_chapter##*_}" .cbr` if [[ $local_chapter -ne 0 ]]; then local_chapter=`echo ${local_chapter} | sed "s/^0*//g"` fi echo -ne "\nopening $base_url/$manga_name..." wget -qN --no-cache $base_url/$manga_name/ if [[ ! -f "index.html" ]]; then echo -e "\b\b\b [ERROR]\nthe url seems to be invalid, or there may be a problem with your internet connection" exit fi grep "ch-subject" index.html 2> /dev/null | grep "a href" | cut -f3 -d\/ > tmp_chapters if [[ ! -s tmp_chapters ]]; then echo -e "\b\b\b [ERROR]\ncannot extract chapters info from the url" exit fi echo -e "\b\b\b [OK]" latest_chapter=`head -1 tmp_chapters` if [[ -n $local_chapter && ! $CHAPTER -eq 1 ]]; then echo "local chapter: ${local_chapter} / latest chapter: $latest_chapter" if [[ $local_chapter == $latest_chapter ]]; then echo "you already have the latest chapter of $display_name" continue fi fi if [[ $LATEST -eq 1 ]]; then first_chapter=$latest_chapter last_chapter=$latest_chapter fi if [[ "$last_chapter" == "+" ]]; then last_chapter=$latest_chapter fi if [[ $UPDATE -eq 1 ]]; then if [[ $local_chapter == "" ]]; then first_chapter=`sort -n tmp_chapters | head -n1` else index=`sort -n tmp_chapters | grep -nm1 $local_chapter | cut -f1 -d:` first_chapter=`sort -n tmp_chapters | tail -n+$((index + 1)) | head -n1` fi last_chapter=$latest_chapter fi if [[ $CHAPTER -eq 1 || -z $local_chapter ]]; then echo "from chapter: $first_chapter / to chapter: $last_chapter" fi index=`sort -n tmp_chapters | grep -nm1 $first_chapter | cut -f1 -d:` last_index=`sort -n tmp_chapters | grep -nm1 $last_chapter | cut -f1 -d:` CHAPTERS=`sort -n tmp_chapters | awk 'FNR >= '$index' && FNR <= '$last_index` for chapter in $CHAPTERS; do echo "downloading $display_name chapter $chapter" echo -ne "\r\e[0K`progressbar 0 1` (initializing...)" page_location=`wget -qO - $base_url/$manga_name/$chapter/ | grep -i "begin reading" | cut -f2 -d\"` wget -qO tmp_page $base_url$page_location PAGES=`grep -i "option value" tmp_page | grep -i -v "select manga series" | cut -f2 -d\"` image_location=`grep -i "manga-page" tmp_page | cut -f4 -d\"` image_location=${image_location%/*.jpg} total=`echo $PAGES | wc -w` i=0 for page in $PAGES; do let i++ echo -ne "\r\e[0K`progressbar $i $total` ($i/$total)" wget -q "$image_location/$page.jpg" done if [[ $chapter -ne 0 && ${#chapter} -lt 3 ]]; then chapter=`echoes "0" ${#chapter} 2`$chapter fi cbr_file_name="${manga_name}_${chapter}.cbr" echo -ne "\r\e[0K`progressbar $i $total` (packing files...)" if zip -q $cbr_file_name *.jpg; then rm -f *.jpg tmp_* echo -e "\r\e[0K`progressbar $i $total` ($cbr_file_name)" [[ $LOG_FILE -eq 1 ]] && echo "[`date +%c`] $cbr_file_name" >> "$log" fi done done

6 comments
เดี๋ยวจะหา bug ให้
บน mac ถ้าใครใช้ wget ตัวเดียวกับผม (http://ftp.gnu.org/pub/gnu/wget/wget-1.9.1.tar.gz) มันจะไม่มี option
--no-cacheให้นะครับ ให้ไปลบออก จะทำให้ script สามารถ download ไฟล์มาได้ครับ ใช้พอกล่อมแกล้มไปก่อนoption
--no-cacheนี่ผมใส่ไปเพราะเคยเจอว่าเปิดดูใน browser มันมี chapter ใหม่ออกมาแล้ว แต่รันสคริปต์แล้วปรากฏว่ามันไปโหลดหน้าเก่ามาจากไหนก็ไม่รู้ ทำให้ไม่เห็น chapter ใหม่ซะงั้น เลยพยายาม force ให้มันโหลดหน้าเว็บเพจแบบไม่ต้องสนใจ cache ครับ เอาออกได้ถ้าไม่เคยเจอเคสประหลาดๆ แบบผมsyntax error: invalid arithmetic operator (error token is ".4")
กรณี ชื่อไฟล์เป็น ทศนิยมเช่น 70.4 แล้วมันก็จะไม่เติมศูนย์ข้างหน้าให้ครับ
เอ่อข้างบนผมใช้ชื่อนี้ประจำอยู่แล้วนะครับ == ไม่ได้ว่าโค้ดคุณ kong นะ ผมแฟนประจำ อิอิ
โอ้... บั๊ก ขอบคุณครับ :)
Post new comment