Пример #1
0
def list_menu(pyconfig):
    message = \
    """
    USAGE:
        pycomic.py list-menu COMICNAME [PATTERN]
    """
    try:
        comic_name = sys.argv[2]
    except IndexError:
        print(message)
        sys.exit(1)

    try:
        pattern = sys.argv[3]
    except IndexError:
        pattern = ''

    # Check directory structure
    pylib.check_structure(pyconfig, SECTION)

    # Find comic from menu csv file
    eng_name, ch_name, number, _status = _check_comic_existence(
        pyconfig, comic_name)

    # Define comic object
    comic = pylib.Comic(eng_name, ch_name, number)
    comic.file_path(pyconfig.menu(SECTION), 'menu', extension='_menu.csv')

    # Show menu content
    try:
        _print_contents(pylib.list_file_content(comic.path['menu'], pattern))
        # pylib.list_file_content(comic.path['menu'], pattern)
    except pycomic_err.CSVError:
        print('File {} not exist'.format(comic.path['menu']))
        print('Use fetch-menu function to create menu file')
Пример #2
0
def list_url(pyconfig):
    message = \
    """
    USAGE:
        pycomic.py list-url COMICNAME [PATTERN]
    """
    try:
        comic_name = sys.argv[2]
    except IndexError:
        print(message)
        sys.exit(1)

    try:
        pattern = sys.argv[3]
    except IndexError:
        pattern = ''

    # Check directory structure
    pylib.check_structure(pyconfig, SECTION)

    # Find comic from menu csv
    eng_name, ch_name, number, _status = _check_comic_existence(
        pyconfig, comic_name)

    # Define comic object
    comic = pylib.Comic(eng_name, ch_name, number)
    comic.file_path(pyconfig.links(SECTION), 'links-dir')

    # Show matching data
    _print_files(pylib.list_files(comic.path['links-dir'], pattern))
Пример #3
0
def fetch_menu(pyconfig):
    message = \
    """
    USAGE:
        pycomic.py fetch-menu COMICNAME
    """
    try:
        comic_name = sys.argv[2]
    except IndexError:
        print(message)
        sys.exit(1)

    # Check directory structure
    pylib.check_structure(pyconfig, SECTION)

    # Find comic from menu csv file
    eng_name, ch_name, number, _status = _check_comic_existence(
        pyconfig, comic_name)

    # Define comic object
    comic = pylib.Comic(eng_name, ch_name, number)
    comic.file_path(pyconfig.menu(SECTION), 'menu', extension='_menu.csv')
    comic.comic_site(pyconfig.site_url(SECTION), 'site-url')

    # Page request
    try:
        page_parse = pylib.request_page(comic.path['site-url'])
    except requests.exceptions.HTTPError:
        logger.warning('Request to {} fail'.format(comic.path['site-url']))
        sys.exit(31)

    # Information parsing
    css_selector = 'div.chapter-list ul li a'
    date_selector = 'ul.detail-list li.status span'

    chapter_list = page_parse.select(css_selector)
    date = page_parse.select(date_selector)[-1].text

    if '已完結' in page_parse.text:
        comic_state = '已完結'
    else:
        comic_state = '連載中'

    # Construct data
    data = []
    for chapter in chapter_list:
        chapter_url = pyconfig.home_url(SECTION) + chapter.get('href')
        chapter_title = chapter.find('span').text
        data.append((chapter_title, chapter_url, date, comic_state))

    # Write csv file
    try:
        pylib.write_csv(comic.path['menu'], data, index=False)
    except pycomic_err.CSVError as err:
        logger.warning('Error: {}'.format(err))
        logger.info('Failed to write to {}'.format(comic.path['menu']))
        sys.exit(16)
    else:
        logger.info('Write file {} success'.format(comic.path['menu']))
Пример #4
0
def verify_image(pyconfig):
    message = \
    """
    USAGE:
        pycomic.py verify-image COMCINAME FILETAG
    NOTE:
        Use 'pycomic.py list-books' command to get FILETAG value
    """
    try:
        comic_name = sys.argv[2]
        request_tag = int(sys.argv[3])
    except IndexError:
        print(message)
        sys.exit(1)

    # Check directory structure
    pylib.check_structure(pyconfig, SECTION)

    # Find comic from menu csv file
    try:
        eng_name, ch_name, number, _status = pylib.find_menu_comic(
            pyconfig, SECTION, comic_name)
    except pycomic_err.ComicNotFoundError:
        logger.info('No match to {} found'.format(comic_name))
        sys.exit(11)

    # Define comic object
    comic = pylib.Comic(eng_name, ch_name, number)
    comic.file_path(pyconfig.origin(SECTION), 'books-dir')
    # Get request tag's filename
    try:
        request_file = pylib.index_data(comic.path['books-dir'],
                                        request_tag,
                                        file=False)
    except pycomic_err.CSVError as err:
        logger.warning(err)
        logger.info('Failed to list directory {}'.format(
            comic.path['books-dir']))
        sys.exit(16)
    except pycomic_err.DataIndexError:
        logger.info('File Tag {} not found'.format(request_tag))
        sys.exit(18)

    comic.file_path(pyconfig.origin(SECTION),
                    'book',
                    name=request_file.split('.')[0])

    # Verification
    try:
        truncated = pylib.verify_images(comic.path['book'])
    except FileNotFoundError:
        logger.warning('Directory {} not exist'.format(comic.path['books']))
        sys.exit(13)

    for image in truncated:
        logger.info('Image file {} in {} {} is truncated'.format(
            image, comic_name, request_tag))
    logger.info('{} {} verification completed'.format(comic_name, request_tag))
Пример #5
0
def convert(pyconfig):
    message = \
    """
    USAGE:
        pycomic.py convert COMICNAME
    """
    try:
        comic_name = sys.argv[2]
    except IndexError:
        print(message)
        sys.exit(1)

    # Chekc config file integrity
    try:
        pyconfig.config_test(SECTION, output=True)
    except pycomic_err.NoSectionError:
        sys.exit(102)
    except pycomic_err.NoOptionError:
        sys.exit(103)

    # Check directory structure
    pylib.check_structure(pyconfig, SECTION)
    _check(pyconfig, SECTION)

    # Find comic from menu csv file
    try:
        eng_name, ch_name, _number, _status = pylib.find_menu_comic(
            pyconfig, SECTION, comic_name)
    except pycomic_err.ComicNotFoundError:
        logger.info('No match to {} found'.format(comic_name))
        sys.exit(11)

    # Define comic object
    comic = pylib.Comic(eng_name, ch_name)
    comic.file_path(pyconfig.origin(SECTION), 'origin')
    comic.file_path(pyconfig.formatted(SECTION), 'format')

    # Convert images
    try:
        os.mkdir(comic.path['format'])
    except FileExistsError:
        logger.warning('Directory {} already exist'.format(
            comic.path['format']))
        sys.exit(21)

    try:
        pylib.convert_images_jpg(comic.path['origin'], comic.path['format'])
    except IOError:
        logger.warning('Failed to convert {} to jpeg files'.format(
            comic.path['origin']))
        shutil.rmtree(comic.path['format'])
        sys.exit(15)
    else:
        logger.info('Convert {} to jpeg files success'.format(
            comic.path['origin']))
Пример #6
0
def make_pdf(pyconfig):
    message = \
    """
    USAGE:
        pycomic.py make-pdf COMICNAME
    """
    try:
        comic_name = sys.argv[2]
    except IndexError:
        print(message)
        sys.exit(1)

    # Chekc config file integrity
    try:
        pyconfig.config_test(SECTION, output=True)
    except pycomic_err.NoSectionError:
        sys.exit(102)
    except pycomic_err.NoOptionError:
        sys.exit(103)

    # Check directory structure
    pylib.check_structure(pyconfig, SECTION)
    _check(pyconfig, SECTION)

    # Find comic from menu csv file
    try:
        eng_name, ch_name, _number, _status = pylib.find_menu_comic(
            pyconfig, SECTION, comic_name)
    except pycomic_err.ComicNotFoundError:
        logger.info('No match to {} found'.format(comic_name))
        sys.exit(11)

    # Define comic object
    comic = pylib.Comic(eng_name, ch_name)
    comic.file_path(pyconfig.formatted(SECTION), 'books')
    comic.file_path(pyconfig.comics(SECTION), 'pdf')

    # Make pdf
    try:
        pylib.make_pdf(comic.path['books'], comic.path['pdf'])
    except FileNotFoundError:
        logger.warning('Directory {} not exist'.format(comic.path['books']))
        sys.exit(13)
    except pycomic_err.FileExistError:
        logger.info('PDF file {} already exist'.format(comic.path['pdf']))
        sys.exit(14)
    except:
        logger.warning('Failed to make pdf file from {}'.format(
            comic.path['books']))
        os.remove(comic.path['pdf'])
        sys.exit(22)
    else:
        logger.info('Make PDF {} success'.format(comic.path['pdf']))
Пример #7
0
def download(pyconfig):
    message = \
    """
    USAGE:
        pycomic.py download COMICNAME
    """
    try:
        comic_name = sys.argv[2]
    except IndexError:
        print(message)
        sys.exit(1)

    # Chekc config file integrity
    try:
        pyconfig.config_test(SECTION, output=True)
    except pycomic_err.NoSectionError:
        sys.exit(102)
    except pycomic_err.NoOptionError:
        sys.exit(103)

    # Check directory structure
    pylib.check_structure(pyconfig, SECTION)
    _check(pyconfig, SECTION)

    # Find comic from menu csv file
    try:
        eng_name, ch_name, _number, _status = pylib.find_menu_comic(
            pyconfig, SECTION, comic_name)
    except pycomic_err.ComicNotFoundError:
        logger.info('No match to {} found'.format(comic_name))
        sys.exit(11)

    # Define comic object
    comic = pylib.Comic(eng_name, ch_name)
    comic.file_path(pyconfig.origin(SECTION), 'book')
    comic.file_path(pyconfig.refine(SECTION), 'refine')

    # Download images
    header = {'User-Agent': pyconfig.useragent(SECTION)}
    try:
        os.mkdir(comic.path['book'])
    except FileExistsError:
        logger.warning('Directory {} already exist'.format(comic.path['book']))
        sys.exit(21)
    urls = url.extract_images(comic.path['refine'])
    print('Header: {}'.format(header))
    errors = url.download_images(urls, comic.path['book'], header=header)
    # errors = url.download_images(urls, comic.path['book'], header='Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0')

    # Show download error messages
    for error_message in errors:
        logger.info(error_message)
Пример #8
0
def fetch_url(pyconfig):
    message = \
    """
    USAGE:
        pycomic.py fetch-url COMICNAME
    """
    try:
        comic_name = sys.argv[2]
    except IndexError:
        print(message)
        sys.exit(1)

    # Chekc config file integrity
    try:
        pyconfig.config_test(SECTION, output=True)
    except pycomic_err.NoSectionError:
        sys.exit(102)
    except pycomic_err.NoOptionError:
        sys.exit(103)

    # Check directory structure
    pylib.check_structure(pyconfig, SECTION)
    _check(pyconfig, SECTION)

    # Find comic from menu csv file
    try:
        eng_name, ch_name, _number, _status = pylib.find_menu_comic(
            pyconfig, SECTION, comic_name)
    except pycomic_err.ComicNotFoundError:
        logger.info('No match to {} found'.format(comic_name))
        sys.exit(11)

    # Define comic object
    comic = pylib.Comic(eng_name, ch_name)
    comic.file_path(pyconfig.raw(SECTION), 'raw')
    comic.file_path(pyconfig.refine(SECTION), 'refine')

    # Extract links
    urls = url.extract_images(comic.path['raw'])

    # Save links to extract file
    try:
        pylib.write_csv(comic.path['refine'], urls)
    except pycomic_err.CSVError as err:
        logger.warning('Error: {}'.format(err))
        logger.info('Failed to write to {}'.format(comic.path['refine']))
        sys.exit(16)
    else:
        logger.info('Extract file {} success'.format(eng_name))
Пример #9
0
def verify(pyconfig):
    message = \
    """
    USAGE:
        pycomic.py verify COMICNAME
    """
    try:
        comic_name = sys.argv[2]
    except IndexError:
        print(message)
        sys.exit(1)

    # Chekc config file integrity
    try:
        pyconfig.config_test(SECTION, output=True)
    except pycomic_err.NoSectionError:
        sys.exit(102)
    except pycomic_err.NoOptionError:
        sys.exit(103)

    # Check directory structure
    pylib.check_structure(pyconfig, SECTION)
    _check(pyconfig, SECTION)

    # Find comic from menu csv file
    try:
        eng_name, ch_name, _number, _status = pylib.find_menu_comic(
            pyconfig, SECTION, comic_name)
    except pycomic_err.ComicNotFoundError:
        logger.info('No match to {} found'.format(comic_name))
        sys.exit(11)

    # Define comic object
    comic = pylib.Comic(eng_name, ch_name)
    comic.file_path(pyconfig.origin(SECTION), 'books')

    # Verification
    try:
        truncated = pylib.verify_images(comic.path['books'])
    except FileNotFoundError:
        logger.warning('Directory {} not exist'.format(comic.path['books']))
        sys.exit(13)

    for image in truncated:
        logger.info('Image file {} in {} is truncated'.format(
            image, comic_name))
    logger.info('{} verification completed'.format(comic_name))
Пример #10
0
def list_books(pyconfig):
    message = \
    """
    USAGE:
        pycomic.py list-books origin|format COMICNAME [PATTERN]
    """
    _ORIGIN = 'origin'
    _FORMAT = 'format'
    source_type = ''

    try:
        source_type = sys.argv[2]
        comic_name = sys.argv[3]
    except IndexError:
        print(message)
        sys.exit(1)

    try:
        pattern = sys.argv[4]
    except IndexError:
        pattern = ''

    # Check directory structure
    pylib.check_structure(pyconfig, SECTION)

    # Find comic from menu csv
    eng_name, ch_name, number, _status = _check_comic_existence(
        pyconfig, comic_name)

    # Define comic object
    comic = pylib.Comic(eng_name, ch_name, number)
    comic.file_path(pyconfig.origin(SECTION), 'origin')
    comic.file_path(pyconfig.formatted(SECTION), 'format')

    # Show matching data list
    if source_type == _ORIGIN:
        _print_files(pylib.list_files(comic.path['origin'], pattern))
    elif source_type == _FORMAT:
        _print_files(pylib.list_files(comic.path['format'], pattern))
    else:
        print(message)
        sys.exit(1)
Пример #11
0
def error_url(pyconfig):
    message = \
    """
    USAGE:
        pycomic.py error_url COMICNAME FILETAG
    NOTE:
        Use 'pycomic.py list-url' command to get FILETAG
    """
    try:
        comic_name = sys.argv[2]
        request_tag = int(sys.argv[3])
    except (IndexError, ValueError):
        print(message)
        sys.exit(1)

    # Check directory structure
    pylib.check_structure(pyconfig, SECTION)

    # Find comic from menu csv file
    eng_name, ch_name, number, _status = _check_comic_existence(
        pyconfig, comic_name)

    # Define comic object
    comic = pylib.Comic(eng_name, ch_name, number)
    comic.file_path(pyconfig.links(SECTION), 'links-dir')
    # print(pylib.index_data(comic.path['links-dir'], request_tag, file=False))
    try:
        comic.file_path(pyconfig.links(SECTION),
                        'links',
                        name=pylib.index_data(comic.path['links-dir'],
                                              request_tag,
                                              file=False))
    except pycomic_err.DataIndexError:
        logger.info('File Tag {} not found'.format(request_tag))
        sys.exit(18)

    # Show errors
    errors = pylib.list_file_content(comic.path['links'], 'error')
    print('File {}'.format(comic.path['links']))
    for index, error in errors:
        print('Page {} error - {}'.format(index, error[1]))
Пример #12
0
def download(pyconfig):
    message = \
    """
    USAGE:
        pycomic.py download COMICNAME FILETAG
    NOTE:
        Use 'pycomic.py list-url' command to get FILETAG value
    """
    try:
        comic_name = sys.argv[2]
        request_tag = int(sys.argv[3])
    except IndexError:
        print(message)
        sys.exit(1)

    # Check directory structure
    pylib.check_structure(pyconfig, SECTION)

    # Find comic from menu csv file
    try:
        eng_name, ch_name, number, _status = pylib.find_menu_comic(
            pyconfig, SECTION, comic_name)
    except pycomic_err.ComicNotFoundError:
        logger.info('No match to {} found'.format(comic_name))
        sys.exit(11)

    # Define comic object
    comic = pylib.Comic(eng_name, ch_name, number)
    comic.file_path(pyconfig.links(SECTION), 'links-dir')
    # Get request tag's filename
    try:
        request_file = pylib.index_data(comic.path['links-dir'],
                                        request_tag,
                                        file=False)
    except pycomic_err.CSVError as err:
        logger.warning(err)
        logger.info('Failed to list directory {}'.format(
            comic.path['links-dir']))
        sys.exit(16)
    except pycomic_err.DataIndexError:
        logger.info('File Tag {} not found'.format(request_tag))
        sys.exit(18)

    comic.file_path(pyconfig.origin(SECTION),
                    'book',
                    name=request_file.split('.')[0])
    comic.file_path(pyconfig.links(SECTION), 'links', name=request_file)

    # Download images
    try:
        os.makedirs(comic.path['book'])
    except OSError:
        logger.warning('Directory {} already exist'.format(comic.path['book']))
        sys.exit(10)

    urls = url.extract_images(comic.path['links'], duplicates=True)
    # Remove book directory if error occur when downloading images
    header = {'User-Agent': pyconfig.useragent(SECTION)}
    try:
        errors = url.download_images(urls, comic.path['book'], header=header)
    except Exception as err:
        logger.warning(err)
        shutil.rmtree(comic.path['book'])
        sys.exit(41)

    # Show download error messages
    for error_message in errors:
        logger.info(error_message)

    # Download success
    logger.info('Download {} {} complete'.format(comic.english, request_tag))
Пример #13
0
def make_pdf(pyconfig):
    message = \
    """
    USAGE:
        pycomic.py make-pdf COMICNAME FILETAG
    NOTE:
        Use 'pycomic.py list-books' command to get FILETAG value
    """
    try:
        comic_name = sys.argv[2]
        request_tag = int(sys.argv[3])
    except IndexError:
        print(message)
        sys.exit(1)

    # Check directory structure
    pylib.check_structure(pyconfig, SECTION)

    # Find comic from menu csv file
    # To make sure that comic is already added to data structure
    try:
        eng_name, ch_name, number, _status = pylib.find_menu_comic(
            pyconfig, SECTION, comic_name)
    except pycomic_err.ComicNotFoundError:
        logger.info('No match to {} found'.format(comic_name))
        sys.exit(11)

    # Define comic object
    comic = pylib.Comic(eng_name, ch_name, number)
    comic.file_path(pyconfig.formatted(SECTION), 'books-dir')
    # Get request tag's directory name
    try:
        request_file = pylib.index_data(comic.path['books-dir'],
                                        request_tag,
                                        file=False)
    except pycomic_err.DataIndexError:
        logger.info('File Tag {} not found'.format(request_tag))
        sys.exit(18)

    comic.file_path(pyconfig.comics(SECTION), 'pdf-dir')
    comic.file_path(pyconfig.formatted(SECTION),
                    'book',
                    name=request_file.split('.')[0])
    comic.file_path(pyconfig.comics(SECTION),
                    'pdf',
                    name=request_file.split('.')[0])

    # Make pdf
    os.makedirs(comic.path['pdf-dir'], exist_ok=True)

    try:
        pylib.make_pdf(comic.path['book'], comic.path['pdf'])
    except FileNotFoundError:
        logger.warning('Directory {} not exist'.format(comic.path['book']))
        sys.exit(13)
    except pycomic_err.FileExistError:
        logger.info('PDF file {} already exist'.format(comic.path['pdf']))
        sys.exit(14)
    except Exception as err:
        logger.warning('Error: {}'.format(err))
        logger.warning('Failed to make pdf file from {}'.format(
            comic.path['book']))
        shutil.rmtree(comic.path['pdf'])
        sys.exit(22)
    else:
        logger.info('Make PDF {} success'.format(comic.path['pdf']))
Пример #14
0
def fetch_url(pyconfig):
    message = \
    """
    USAGE:
        pycomic.py fetch-url COMICNAME IDENTITYNUM
    NOTE:
        Use 'pycomic.py list-menu' command to get IDENTITYNUM
    """
    try:
        comic_name = sys.argv[2]
        request_identity = int(sys.argv[3])
    except:
        print(message)
        sys.exit(1)

    # Check directory structure
    pylib.check_structure(pyconfig, SECTION)

    # Find comic from menu csv file
    eng_name, ch_name, number, _status = _check_comic_existence(
        pyconfig, comic_name)

    # Define comic object
    comic = pylib.Comic(eng_name, ch_name, number)
    comic.file_path(pyconfig.menu(SECTION), 'menu', extension='_menu.csv')
    # Read comic URL from COMICNAME_menu.csv file
    try:
        comic_data = pylib.index_data(comic.path['menu'], request_identity)
    except pycomic_err.CSVError as err:
        logger.warning(err)
        logger.info('Failed to read file {}'.format(comic.path['menu']))
        sys.exit(16)
    except pycomic_err.DataIndexError:
        logger.info('Identity Number {} not found'.format(request_identity))
        sys.exit(18)

    driver = pylib.Driver(comic_data[0], comic_data[1])

    # comic.chapter_title, comic.url = comic_data[0], comic_data[1]
    comic.file_path(pyconfig.links(SECTION),
                    'links',
                    name=comic.english,
                    extension='_{}.csv'.format(driver.chapter_title))
    comic.file_path(pyconfig.links(SECTION), 'links-dir')

    # Start selenium driver
    try:
        driver.get()
    except:
        logger.info('Driver failed to request {}'.format(driver.chapter_url))
        sys.exit(3)

    # Information parsing
    try:
        driver.find_last_page('#pageSelect option:nth-last-child(1)')
    except pylib.Driver.DriverError as err:
        logger.warning('Error: {}'.format(err))
        logger.info('Failed to get last page value')
        sys.exit(32)

    # Fetch urls
    driver.get_urls('manga', 'next', error_text=pyconfig.error_url(SECTION))

    # Write to csv file
    os.makedirs(comic.path['links-dir'], exist_ok=True)
    try:
        pylib.write_csv(comic.path['links'], driver.urls, index=True)
    except pycomic_err.CSVError as err:
        logger.warning('Error" {}'.format(err))
        logger.info('Failed to write to {}'.format(comic.path['links']))

        os.remove(comic.path['links'])
        sys.exit(16)
    else:
        logger.info('Write file {} success'.format(comic.path['links']))
Пример #15
0
def convert_image(pyconfig):
    message = \
    """
    USAGE:
        pycomic.py convert COMICNAME FILETAG
    NOTE:
        Use 'pycomic.py list-books' command to get FILETAG value
    """
    try:
        comic_name = sys.argv[2]
        request_tag = int(sys.argv[3])
    except IndexError:
        print(message)
        sys.exit(1)

    # Check directory structure
    pylib.check_structure(pyconfig, SECTION)

    # Find comic from menu csv file
    try:
        eng_name, ch_name, number, _status = pylib.find_menu_comic(
            pyconfig, SECTION, comic_name)
    except pycomic_err.ComicNotFoundError:
        logger.info('No match to {} found'.format(comic_name))
        sys.exit(11)

    # Define comic object
    comic = pylib.Comic(eng_name, ch_name, number)
    comic.file_path(pyconfig.links(SECTION), 'links-dir')
    # Get request tag's filename
    try:
        request_file = pylib.index_data(comic.path['links-dir'],
                                        request_tag,
                                        file=False)
    except pycomic_err.CSVError as err:
        logger.warning(err)
        logger.info('Failed to list directory {}'.format(
            comic.path['links-dir']))
        sys.exit(16)
    except pycomic_err.DataIndexError:
        logger.info('File Tag {} not found'.format(request_tag))
        sys.exit(18)

    comic.file_path(pyconfig.origin(SECTION),
                    'origin',
                    name=request_file.split('.')[0])
    comic.file_path(pyconfig.formatted(SECTION),
                    'format',
                    name=request_file.split('.')[0])

    # Convert images
    try:
        os.makedirs(comic.path['format'])
    except FileExistsError:
        logger.warning('Directory {} already exist'.format(
            comic.path['format']))
        sys.exit(21)

    try:
        pylib.convert_images_jpg(comic.path['origin'], comic.path['format'])
    except IOError:
        logger.warning('Failed to convert {} to jpeg files'.format(
            comic.path['origin']))
        shutil.rmtree(comic.path['format'])
        sys.exit(15)
    else:
        logger.info('Convert {} to jpeg files success'.format(
            comic.path['origin']))
Пример #16
0
def add(pyconfig):
    message = \
    """
    USAGE:
        pycomic.py add ENGLISHNAME CHINESENAME
    """

    try:
        english_name = sys.argv[2]
        chinese_name = sys.argv[3]
        book_number = '------'
        process_state = '--------'
    except IndexError:
        print(message)
        sys.exit(1)

    # Chekc config file integrity
    try:
        pyconfig.config_test(SECTION, output=True)
    except pycomic_err.NoSectionError:
        sys.exit(102)
    except pycomic_err.NoOptionError:
        sys.exit(103)

    # Check directory structure
    pylib.check_structure(pyconfig, SECTION)
    _check(pyconfig, SECTION)

    # Check to avoid duplicated comic information
    pylib.check_menu_duplicate(pyconfig, SECTION, chinese_name, english_name)

    # Get file content from user
    print('Enter file content. Ctrl-D to save.')
    contents = []

    while True:
        try:
            line = input()
        except EOFError:
            break
        contents.append(line)

    # Define Comic object
    comic = pylib.Comic(english_name, chinese_name)
    comic.file_path(pyconfig.raw(SECTION), 'raw')

    # Add data to main menu
    data = [english_name, chinese_name, book_number, process_state]
    try:
        pylib.update_menu(pyconfig.main_menu(SECTION), data)
    except pycomic_err.UpdateError:
        logger.warning('Update {} failed'.format(pyconfig.main_menu(SECTION)))
        sys.exit(12)

    logger.info('Write {} to menu csv file success'.format(data))

    # Write raw data file
    try:
        pylib.write_txt(comic.path['raw'], contents)
    except pycomic_err.TXTError as err:
        logger.warning('Error: {}'.format(err))
        logger.info('Failed to write to {}'.format(comic.path['raw']))
        # TODO: Delete data that was added to main menu
        sys.exit(17)
    else:
        logger.info('Write contents to {} success'.format(comic.path['raw']))
Пример #17
0
def eyny_download(pyconfig):
    message = \
    """
    USAGE:
        pycomic.py eyny-download URL
    """
    _EYNY_LOGIN = '******'

    try:
        eyny_url = sys.argv[2]
    except IndexError:
        print(message)
        sys.exit(1)

    # Get user config - eyny
    user_config = pylib.UserConfig(['user_config.ini'])
    user_info = user_config.eyny_info()

    # Chekc config file integrity
    try:
        pyconfig.config_test(SECTION, output=False)
    except pycomic_err.NoSectionError:
        sys.exit(102)
    except pycomic_err.NoOptionError:
        sys.exit(103)

    # Check directory structure
    pylib.check_structure(pyconfig, SECTION)
    _check(pyconfig, SECTION)

    # Get comic information: ENGLISHNAME CHINESENAME
    eng_name = input('Comic english name: ')
    ch_name = input('Comic chinese name: ')

    # Check to avoid duplicated comic information
    pylib.check_menu_duplicate(pyconfig, SECTION, ch_name, eng_name)

    # Open login page
    driver = pytmp.EynyDriver()
    try:
        driver.get(_EYNY_LOGIN)
    except:
        logger.info('Driver failed to request {}'.format(_EYNY_LOGIN))
        sys.exit(3)

    # Login
    driver.login(user_info)

    # Open URL webpage
    try:
        driver.get(eyny_url)
    except:
        logger.info('Driver failed to request {}'.format(eyny_url))
        sys.exit(3)

    # Adult confirmation
    # TODO: Test for adult confirm page
    driver.adult_confirm()

    # Open source code inspect tab
    driver.inspect_source_code(eyny_url)

    # Add
    # Get file content from user
    print('Enter file content. Ctrl-D to save.')
    contents = []

    while True:
        try:
            line = input()
        except EOFError:
            break
        contents.append(line)

    # Define Comic object
    comic = pylib.Comic(eng_name, ch_name)
    comic.file_path(pyconfig.raw(SECTION), 'raw')
    comic.file_path(pyconfig.refine(SECTION), 'refine')
    comic.file_path(pyconfig.origin(SECTION), 'book')

    # Add data to main menu
    book_number = '-----'
    process_state = '--------'
    data = [eng_name, ch_name, book_number, process_state]
    try:
        pylib.update_menu(pyconfig.main_menu(SECTION), data)
    except pycomic_err.UpdateError:
        logger.warning('Update {} failed'.format(pyconfig.main_menu(SECTION)))
        sys.exit(12)

    logger.info('Write {} to menu csv file success'.format(data))

    # Write raw data file
    try:
        pylib.write_txt(comic.path['raw'], contents)
    except pycomic_err.TXTError as err:
        logger.warning('Error: {}'.format(err))
        logger.info('Failed to write to {}'.format(comic.path['raw']))
        # TODO: Delete data that was added to main menu
        sys.exit(17)
    else:
        logger.info('Write contents to {} success'.format(comic.path['raw']))

    # Fetch-url
    # Find comic from menu csv file
    comic_name = eng_name
    try:
        eng_name, ch_name, _number, _status = pylib.find_menu_comic(
            pyconfig, SECTION, comic_name)
    except pycomic_err.ComicNotFoundError:
        logger.info('No match to {} found'.format(comic_name))
        sys.exit(11)

    # Extract links
    urls = url.extract_images(comic.path['raw'])

    # Save links to extract file
    try:
        pylib.write_csv(comic.path['refine'], urls)
    except pycomic_err.CSVError as err:
        logger.warning('Error: {}'.format(err))
        logger.info('Failed to write to {}'.format(comic.path['refine']))
        sys.exit(16)
    else:
        logger.info('Extract file {} success'.format(eng_name))

    # Download >> user logout >> close driver
    driver.download_images(urls)
    driver.logout()
    driver.close()

    # Copy temp images to book location
    try:
        shutil.copytree(driver.dir_path, comic.path['book'])
    except shutil.Error as err:
        # Directory are the same
        logger.warning('Error: {}'.format(err))
    except OSError as err:
        logger.warning('Error: {}'.format(err))
    else:
        logger.info('Save images to  {} success'.format(comic.path['book']))