게시판 즐겨찾기
편집
드래그 앤 드롭으로
즐겨찾기 아이콘 위치 수정이 가능합니다.
개인 자료
게시물ID : freeboard_673880짧은주소 복사하기
작성자 : 꼼울
추천 : 0
조회수 : 294회
댓글수 : 0개
등록시간 : 2013/04/02 10:03:42

#!/usr/bin/python


import getopt

import os

import subprocess

import sys

import time


def usage():

    print

    print "Usage:"

    print

    print "options:"

    print "-e <episode range>         (default: 1-1)"

    print "-t <title id>"

    print "-n <title string>          (optional)"

    print "-o <output directory>      (default: ./)"

    print

    print "sample:"

    print "webtoon.py -e 1-10 -t 22897   (episode 1 ~ 10 download)"

    print

    sys.exit(2)


def main(argv):

    title_id = ''

    title = ''

    episode_start = 1

    episode_end = 2

    output_dir = './'

    finish = False


    try:

        opts, args = getopt.getopt(argv, "he:t:n:o:")

    except getopt.GetoptError, e:

        print "[ERROR] GetoptError: "+str(e)

        sys.exit(2)


    for opt, arg in opts:

        if opt == "-h":

            usage()

        elif opt == "-e":

            try:

                parse = arg.split("-")

                episode_start = int(parse[0])

                episode_end = int(parse[1]) + 1

            except:

                print "[ERROR] Incorrect episode range"

                usage()

        elif opt == "-t":

            title_id = arg

        elif opt == "-n":

            title = arg

        elif opt == "-o":

            output_dir = arg

            if output_dir[-1] != '/':

                output_dir += '/'


    if title_id == '':

        usage()


    if (episode_start > episode_end):

        print "[ERROR] Incorrect episode range"


    if not os.path.isdir(output_dir+title_id):

        os.system('mkdir '+output_dir+title_id)


    find_string = 'http://imgcomic.naver.com/webtoon/'+title_id+'/'


    for episode in range(episode_start, episode_end):

        if os.path.isfile('./output.output'):

            os.system('rm ./output.output')

        page_url = "http://comic.naver.com/webtoon/detail.nhn?titleId=%s\\&no=%d"%(title_id, episode)

        curl_cmd = 'curl -s -o ./output.output '+page_url


        curl = subprocess.Popen(curl_cmd, shell=True)


        for i in range(0,30):   # 3 seconds

            time.sleep(0.1)

            if curl.poll() != None:

                break


        if not os.path.isfile('./output.output'):

            print '[INFO] Finish!'

            break


        output_file = open('./output.output', 'r')


        for line in output_file.readlines():

            line = line.strip()

            s_idx = line.find(find_string)

            if s_idx != -1:

                e_idx = line[s_idx:].find('"')

                url = line[s_idx:s_idx+e_idx]

                url_split = url.split('/')

                output_name = "%s%s/%s_%03d_%s.jpg" %\

                            (output_dir, title_id, title, episode, url_split[-1])


                wget_cmd = 'wget -O '+output_name+' '+url

                result = os.system(wget_cmd)

                if result != 0:

                    print '[ERROR] Failed download'


        output_file.close()


if __name__ == '__main__':

    main(sys.argv[1:])


                              


전체 추천리스트 보기
새로운 댓글이 없습니다.
새로운 댓글 확인하기
글쓰기
◀뒤로가기
PC버전
맨위로▲
공지 운영 자료창고 청소년보호