Exemplo n.º 1
0
    def episode_download(queue_entity_episode):
        import plugin
        wr_id = queue_entity_episode.wr_id
        logger.debug('Episode Download wr_id:%s', wr_id)
        try:
            from selenium.webdriver.support.ui import WebDriverWait
            from system import SystemLogicSelenium
            if LogicNormal.driver is None:
                LogicNormal.driver = SystemLogicSelenium.create_driver()

            driver = LogicNormal.driver
            url = '%s/bbs/board.php?bo_table=manga&wr_id=%s' % (
                ModelSetting.get('sitecheck'), wr_id)
            driver.get(url)

            fix_tag = WebDriverWait(
                driver, 30
            ).until(lambda driver: driver.find_element_by_xpath(
                '//*[@id="thema_wrapper"]/div[3]/div/div/div[1]/div[2]/div[3]/div'
            ))
            SystemLogicSelenium.remove_element(driver, fix_tag)

            tag = WebDriverWait(
                driver, 30
            ).until(lambda driver: driver.find_element_by_xpath(
                '//*[@id="thema_wrapper"]/div[3]/div/div/div[1]/div[2]/div[1]/div/div[1]/a[2]'
            ))

            queue_entity_episode.manga_id = tag.get_attribute('href').split(
                '=')[-1]
            title = driver.title
            queue_entity_episode.title = LogicNormal.titlereplace(title)
            match = re.compile(
                ur'(?P<main>.*?)((단행본.*?)?|특별편)?(\s(?P<sub>(\d|\-|\.)*?(화|권)))?(\-)?(전|후|중)?(\s?\d+(\-\d+)?화)?(\s\(완결\))?\s?$'
            ).match(title)

            if match:
                queue_entity_episode.maintitle = match.group('main').strip()
            else:
                match2 = re.compile(ur'(?P<main>.*?)\s시즌')
                if match2:
                    queue_entity_episode.maintitle = match2.group(
                        'main').strip()
                else:
                    queue_entity_episode.maintitle = title
                    logger.debug('not match')
            queue_entity_episode.maintitle = LogicNormal.titlereplace(
                queue_entity_episode.maintitle)

            if ModelSetting.get('use_title_folder') == 'True':
                download_path = os.path.join(ModelSetting.get('dfolder'),
                                             queue_entity_episode.maintitle,
                                             queue_entity_episode.title)
            else:
                download_path = os.path.join(ModelSetting.get('dfolder'),
                                             queue_entity_episode.title)

            logger.debug(title)
            logger.debug(queue_entity_episode.maintitle)

            image_tags = WebDriverWait(
                driver, 30
            ).until(lambda driver: driver.find_elements_by_xpath(
                '//*[@id="thema_wrapper"]/div[3]/div/div/div[1]/div[2]/div[5]/div/img'
            ))

            queue_entity_episode.total_image_count = len(image_tags)
            if not os.path.exists(download_path):
                os.makedirs(download_path)
            queue_entity_episode.status = '캡처중'
            plugin.socketio_callback('episode',
                                     queue_entity_episode.as_dict(),
                                     encoding=False)

            full = SystemLogicSelenium.full_screenshot(driver)
            if full is None:
                queue_entity_episode.status = '실패'
                plugin.socketio_callback('episode',
                                         queue_entity_episode.as_dict(),
                                         encoding=False)
            else:
                queue_entity_episode.status = '파일 생성중'
                for idx, tag in enumerate(image_tags):
                    image_filepath = os.path.join(
                        download_path,
                        str(idx + 1).zfill(5) + '.png')
                    left = tag.location['x']
                    top = tag.location['y']
                    right = tag.location['x'] + tag.size['width']
                    bottom = top + tag.size['height']

                    logger.debug('%s %s %s %s %s', idx, left, top, right,
                                 bottom)

                    im = full.crop(
                        (left, top, right, bottom))  # defines crop points
                    im.save(image_filepath)

                    queue_entity_episode.current_image_index = idx
                    plugin.socketio_callback('episode',
                                             queue_entity_episode.as_dict(),
                                             encoding=False)

                if ModelSetting.get('zip') == 'True':
                    LogicNormal.makezip(download_path)
                queue_entity_episode.status = '완료'
                plugin.socketio_callback('episode',
                                         queue_entity_episode.as_dict(),
                                         encoding=False)
                return True
        except Exception as e:
            logger.error('Exception:%s', e)
            logger.error(traceback.format_exc())
Exemplo n.º 2
0
    def download2(self, entity):
        from selenium.webdriver.support.ui import WebDriverWait
        try:
            from system import SystemLogicSelenium
            from . import plugin
            if LogicNormal.driver is None:
                LogicNormal.driver = SystemLogicSelenium.create_driver()

            driver = LogicNormal.driver
            url = 'https://comic.naver.com/webtoon/detail.nhn?titleId=%s&no=%s' % (
                entity['title_id'], entity['episode_id'])
            logger.debug(url)
            ret = False

            driver.get(url)
            entity['download_count'] += 1
            entity['status'] = 1
            entity['str_status'] = '대기'
            LogicNormal.update_ui(self, entity)

            tag = WebDriverWait(
                driver, 30).until(lambda driver: driver.find_element_by_xpath(
                    '//*[@id="content"]/div[1]/div[1]/div[2]/h2'))
            entity['title'] = SystemLogicSelenium.get_text_excluding_children(
                driver, tag).strip()

            tag = WebDriverWait(
                driver, 30).until(lambda driver: driver.find_element_by_xpath(
                    '//*[@id="content"]/div[1]/div[2]/div[1]/h3'))
            entity['episode_title'] = tag.text
            entity['str_status'] = '분석'
            LogicNormal.update_ui(self, entity)

            tag = WebDriverWait(
                driver, 30).until(lambda driver: driver.find_element_by_xpath(
                    '//*[@id="btnRemoteConOnOff"]'))
            if tag.find_element_by_xpath('em').text == 'ON':
                #logger.debug('ON to OFF')
                tag.click()
            #S = lambda X: driver.execute_script('return document.body.parentNode.scroll'+X)
            #driver.set_window_size(S('Width'),S('Height')) # May need manual adjustment
            #logger.debug(S('Width'))
            #logger.debug(S('Height'))

            tag = WebDriverWait(
                driver, 30).until(lambda driver: driver.find_element_by_xpath(
                    '//*[@id="comic_view_area"]/div[1]'))

            dirname = ModelSetting.get('download_path')
            if ModelSetting.get_bool('use_title_folder'):
                dirname = os.path.join(
                    dirname,
                    Util.change_text_for_use_filename(entity['title']))
            if not os.path.exists(dirname):
                os.makedirs(dirname)
            tmp = u'%s %s %s.png' % (entity['episode_id'].zfill(3),
                                     entity['title'], entity['episode_title'])
            entity['filename'] = os.path.join(
                dirname, Util.change_text_for_use_filename(tmp))
            if os.path.exists(entity['filename']):
                entity['status'] = 12
                entity['str_status'] = '파일 있음'
                LogicNormal.update_ui(self, entity)
            else:
                entity['str_status'] = '다운로드중'
                LogicNormal.update_ui(self, entity)
                from system import SystemLogicSelenium
                full = SystemLogicSelenium.full_screenshot(driver)
                if full is not None:
                    img_tag = tag.find_elements_by_xpath('img')
                    if len(img_tag) > 1:
                        img_tag = img_tag[1]
                    elif len(img_tag) == 1:
                        img_tag = img_tag[0]
                    else:
                        img_tag = tag
                    left = img_tag.location['x']
                    top = tag.location['y']
                    right = img_tag.location['x'] + img_tag.size['width']
                    bottom = tag.location['y'] + tag.size['height']

                    im = full.crop(
                        (left, top, right, bottom))  # defines crop points
                    im.save(entity['filename'])
                    entity['status'] = 11
                    entity['str_status'] = '완료'
                    LogicNormal.update_ui(self, entity)
                else:
                    raise Exception('capture fail.')

        except Exception as e:
            logger.error('Exception:%s', e)
            logger.error(traceback.format_exc())
            entity['status'] = 2
            entity['str_status'] = '실패'
            if entity['download_count'] >= 5:
                entity['status'] = 13
                entity['str_status'] = '재시도초과'
            LogicNormal.update_ui(self, entity)

        ModelItem.save_as_dict(entity)