Clicky

One Manga Downloader v2.0 Beta2 | suksit dot com

One Manga Downloader v2.0 Beta2

Updated

Change log:

  • เปลี่ยนการแสดงสถานะดาวน์โหลดเป็น progress bar
  • แก้บั๊กเลข chapter เป็นทศนิยม
  • ลบ temporary file ทิ้งทุกครั้งที่ออกจากสคริปต์
  • ถ้าไม่ใส่ option อะไรเลย (เช่น onemanga Zetman) จะเหมือนกับการใส่ -u (ถ้าโหลดครั้งแรกจะเริ่มจาก chapter 1)

สำหรับวิธีการใช้งาน ดูได้ที่โพสต์เดิมครับ ส่วนโค้ดก็ตามด้านล่างนี้ (ยิ่งเขียนยิ่งยาว =.=)

#!/bin/bash
#
# usage: onemanga [-dlu] [-c <first chapter>[+|-<last chapter>]] <manga name> [<manga name> ...]
#
 
trap 'rm -f *.jpg tmp_*;' 0
 
base_url="http://www.onemanga.com"
base_dir=`pwd`
 
function calc() {
  echo "scale=2; $*" | bc -q 2>/dev/null | cut -f1 -d.
}
 
function progressbar() {
  width=30
  current=$1
  total=$2
 
  percent=`calc "$current / $total * 100"`
  stack=`calc "$current / $total * $width"`
  stack=${stack:=0}
  space=`calc "$width - $stack"`
 
  for i in `seq 1 $((3 - ${#percent}))`; do
    echo -n " "
  done
 
  echo -n "$percent% ["
 
  for i in `seq 1 $stack`; do
    echo -n "#"
  done
 
  for i in `seq 1 $space`; do
    echo -n " "
  done
 
  echo "]"
}
 
while getopts ":c:dlu" op; do
  case $op in
    c)
      if [[ $LATEST -eq 1 ]]; then
        echo "Invalid option: -c cannot be used with -l" >&2
        exit
      fi
 
      if [[ $UPDATE -eq 1 ]]; then
        echo "Invalid option: -c cannot be used with -u" >&2
        exit
      fi
 
      CHAPTER=1
      CHAPTER_ARG=$OPTARG
      ;;
    d)
      USE_DIR=1
      ;;
    l)
      if [[ $CHAPTER -eq 1 ]]; then
        echo "Invalid option: -l cannot be used with -c" >&2
        exit
      fi
 
      if [[ $UPDATE -eq 1 ]]; then
        echo "Invalid option: -l cannot be used with -u" >&2
        exit
      fi
 
      LATEST=1
      ;;
    u)
      if [[ $CHAPTER -eq 1 ]]; then
        echo "Invalid option: -u cannot be used with -c" >&2
        exit
      fi
 
      if [[ $LATEST -eq 1 ]]; then
        echo "Invalid option: -u cannot be used with -l" >&2
        exit
      fi
 
      UPDATE=1
      ;;
    \?)
      echo "Invalid option: -$OPTARG" >&2
      exit
      ;;
  esac
done
 
shift $(($OPTIND - 1))
 
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
  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`
 
  echo -n "opening $base_url/$manga_name..."
  curl -s $base_url/$manga_name/ > tmp_home
  grep "ch-subject" tmp_home | grep "a href" | cut -f3 -d\/ > tmp_chapters
 
  if [[ ! -s tmp_chapters ]]; then
    echo -e "\b\b\b [ERROR]\ncannot create chapter list from $base_url/$manga_name"
    exit
  fi
 
  echo -e "\b\b\b [OK]"
 
  latest_chapter=`head -1 tmp_chapters`
 
  if [[ $LATEST -eq 1 || $UPDATE -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
    echo "starting chapter: $first_chapter / latest chapter: $latest_chapter"
    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
 
  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"
    page_location=`curl -s --range -1500 $base_url/$manga_name/$chapter/ | grep -i "begin reading" | cut -f2 -d\"`
    curl -s $base_url$page_location > tmp_page
    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
 
    cbr_file_name="${manga_name}_${chapter}.cbr"
 
    echo -ne "\r\e[0K`progressbar $i $total` (packing files...)"
    zip -q $cbr_file_name *.jpg && rm -f *.jpg
    echo -e "\r\e[0K`progressbar $i $total` ($cbr_file_name)"
  done
done

6 comments

apirak's picture
apirak (visitor) says:

ผมพยายามเอามา run บน OS X แต่บนเครื่องผมขาดโปรแกรมไปสองตัวคือ seq กับ wget ครับ

ตัว seq ผมแก้ด้วยการเปลี่ยนบรรทัด

for i in `seq 1 $((3 - ${#percent}))`; do
for i in `seq 1 $stack`; do
for i in `seq 1 $space`; do

เป็น

for i in {1..$((3 - ${#percent}))}; do
for i in {1..$stack}; do
for i in {1..$space}; do

ส่วน wget นี้พยายามเอา curl มาแทนแต่ไม่สำเร็จ สุดท้ายต้องไปเอา wget มา compile เองที่

http://ftp.gnu.org/pub/gnu/wget/wget-1.9.1.tar.gz

สุดท้ายมันไม่คลาย zip ให้ผมใช้ unzip เอา ไม่รู้ว่าใน script คลาย zip ให้หรือเปล่าครับ.. เกิดมาก็พึ่งเคยอ่าน bash file นี่ล่ะครับ ปกติทำแค่ set $PATH

ขอบคุณสำหรับ script นะครับ

kong's picture
kong says:

โอ้วว... ใช้แบบคุณ apirak อ่านง่ายกว่าเยอะเลยแฮะ ขอบคุณครับ ^_^

edit: ผมลองแก้โค้ดแล้ว ปรากฏว่ามันจะทำให้ progress bar แสดงผลไม่ถูกต้อง =.= ยังไงจะลองหาวิธีที่ไม่ต้องใช้ seq ดูครับ

ตัวสคริปต์จะ zip ไฟล์ทั้งหมดใน chapter ให้ออกมาเป็นไฟล์ .cbr ที่ใช้พวกโปรแกรม comic reader เปิดอ่านได้โดยไม่ต้อง unzip ครับ แต่ถ้าไม่ชอบ ก็อาจจะแก้ script ให้ move ไฟล์ไปไว้ใน directory แยกตาม chapter แทนที่จะเรียกคำสั่ง zip ก็ได้ครับ

karn's picture
karn (visitor) says:

เจอว่าเวลาใช้ option -d แล้ว base_dir เป็นพวกที่มี space ในชื่อเช่น "My Documents" เวลาสร้างไดเร็กทอรี่มันสร้างไม่ได้ต้องแก้

  if [[ $USE_DIR -eq 1 ]]; then
    mkdir -p $base_dir/$manga_name
    cd $base_dir/$manga_name
  fi

เป็น

  if [[ $USE_DIR -eq 1 ]]; then
    mkdir -p "$base_dir"/$manga_name
    cd "$base_dir"/$manga_name
  fi
kong's picture
kong says:

ง่า... จริงด้วย แทงยูหลาย

Chris's picture
Chris (visitor) says:

เวลา save เป็น .cbr เนี่ย เติมเลขศูนย์ข้างหน้าเลข chapter ที่เป็น 1 กับ 2 หลักดีมั๊ยครับ? คือเป็น Naruto_005 แทน Naruto_5

ไม่งั้น CDisplay มันกระโดดไป next file (Shift+L), previous file (Shift+k) ไม่ค่อยถูก

kong's picture
kong says:

เดี๋ยวจะลองดูครับ คิดอยู่ว่าอาจมีปัญหากับเรื่องที่มีเลข chapter ที่เป็น 0 และ 00 หรือเปล่า คือเติมแล้วมันอาจจะได้ไฟล์ออกมาชื่อเดียวกัน ไรทำนองนี้!?

Post new comment

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account associated with the e-mail address you provide, it will be used to display your avatar.