One Manga Downloader v2.0 Beta1

Updated

อัพเดตอีกรอบ เนื่องจากใช้สคริปต์เวอร์ชันก่อนแล้วรู้สึกว่า reliability ค่อนข้างต่ำ สาเหตุหลักก็คือการใช้ cURL โหลดหน้าเว็บเพจมาแบบเป็นส่วนๆ ถ้ามีการแก้ไขหน้าเว็บ เช่นเพิ่ม announcement หรือใส่ script จะมีโอกาสสูงมากที่เมื่อโหลดหน้าเว็บส่วนนั้นมาแล้วไม่เจอข้อมูลที่ต้องการ และทำให้ไม่สามารถดาวน์โหลด manga ต่อได้ ประกอบกับเพิ่งพบว่าวิธีที่ใช้ในสคริปต์เวอร์ชันเก่าค่อนข้างอ้อมโลก จริงๆ มันมีวิธีที่ง่ายกว่านั้น ไม่ต้องเรียก cURL บ่อยๆ และทำงานได้เร็วกว่าแบบเดิมเยอะ

จริงๆ จะว่าอัพเดตก็ไม่เชิง เพราะเขียนใหม่เกือบทั้งดุ้น เลยกำหนดให้เวอร์ชันนี้เป็น v2.x โดยถือว่าสคริปต์เวอร์ชันก่อนๆ อยู่ใน series 1.x ทั้งหมด ถ้าลองใช้แล้วเจอบั๊ก ติดปัญหา หรือมีข้อเสนอแนะในการปรับปรุง ก็คอมเมนต์ไว้ได้ครับ สำหรับซอร์สโค้ดก็ตามนี้

#!/bin/bash
#
# usage: onemanga [-d] [-l] [-u] [-c <first chapter>[+ | -<last chapter>]] <manga name> [<manga name> ...]
#
 
base_url="http://www.onemanga.com"
 
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: -c 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 -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 ${manga_name}
    cd ${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=`echo ${local_chapter##*_} | cut -f1 -d\.`
 
  echo "fetching information from ${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
 
  latest_chapter=`head -1 tmp_chapters`
 
  if [[ ${local_chapter} -eq ${latest_chapter} && ! $CHAPTER -eq 1 ]]; then
    echo "you already have the latest chapter of $display_name"
    cd ..
    continue
  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
 
  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}
 
    for page in $PAGES; do
      echo "downloading $image_location/$page.jpg ..."
      wget -q "$image_location/$page.jpg"
    done
 
    echo "creating CBR file ..."
    cbr_file_name="${manga_name}_${chapter}.cbr"
    zip ${cbr_file_name} *.jpg
    rm *.jpg tmp_*
  done
 
  if [[ $USE_DIR -eq 1 ]]; then
    cd ..
  fi
done

วิธีใช้ก็เหมือนเดิม copy code ข้างบนไปใส่ไฟล์ ตั้งชื่ออะไรก็ได้ (ในตัวอย่างตั้งเป็น onemanga2) แล้วเซ็ต flag ให้ execute ได้ สำหรับ option ต่างๆ จะเปลี่ยนไปจากเวอร์ชัน 1.x ค่อนข้างเยอะ วิธีใช้เบื้องต้นมีดังนี้

ดาวน์โหลด chapter ที่ต้องการ

สามารถโหลดทีละ chapter, โหลดเป็นช่วง, หรือโหลดตั้งแต่ chapter ที่กำหนดจนถึง chapter ล่าสุด

onemanga2 -c 440 Naruto
onemanga2 -c 351-360 Bleach
onemanga2 -c 4+ New_Prince_of_Tennis

ดาวน์โหลดเฉพาะ chapter ล่าสุด

ใส่ option -l (latest) เข้าไปอันเดียวจบ

onemanga2 -l Bakuman

อัพเดตจากที่เคยดาวน์โหลดไว้

สคริปต์จะเช็คไฟล์ .cbr ในเครื่อง แล้วดาวน์โหลด chapter ที่ต่อจาก chapter ปัจจุบัน จนถึง chapter ล่าสุดในเว็บ

onemanga2 -u Vagabond

ดาวน์โหลดเป็น directory

สามารถใส่ option -d เพื่อดาวน์โหลดมาเก็บใน directory ตามชื่อ manga ได้

onemanga2 -d -c 450 Naruto
onemanga2 -dl Hajime_no_Ippo
onemanga2 -ud Eyeshield_21 

ดาวน์โหลดทีละหลายๆ เรื่อง

สามารถใส่ชื่อ manga ได้มากกว่าหนึ่งเรื่องในการดาวน์โหลดแต่ละครั้ง มีประโยชน์มากตอนอัพเดตรายสัปดาห์ ถ้าโหลดโดยใช้ option -d ตลอด ก็ใส่ wildcard แทนชื่อ directory ทั้งหมดไปเลยก็ได้

onemanga2 -l Naruto Bleach Eyeshield_21 Vagabond
onemanga2 -ud *

3 comments

Anonymous's picture
Anonymous (visitor) says:

สำหรับคนที่ชอบ rar ก็เปลี่ยนนิดหน่อยที่บรรทัดนี้นะครับ [code] zip ${cbr_file_name} *.jpg [/code] เปลี่ยนเป็น [code] rar a ${cbr_file_name} *.jpg [/code] ข้อดีก็มีอยู่ว่า ไฟล์ cbr ที่ได้ก็จะเปิดด้วย Document Viewer ได้เช่นกัน ซึ่งผมว่ามันก็อ่านได้สะดวกดี

ยังไงก็ขอบคุณแทนนักอ่านด้วยนะครับ

Anonymous's picture
daemonx (visitor) says:

Awesome Kong! I'll test this once I'm done with a few stuffs here. :)

Thanks!

Anonymous's picture
Chris (visitor) says:

เวอร์ชั่นก่อนลองยังไงก็ไม่ได้ แต่อันนี้ใช้ได้ดีครับ

Post new comment

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account, used to display your avatar.