예제 #1
0
    def set_chapters(self, gallery_object, add_to_model=True):
        path = gallery_object.path
        chap_container = gallerydb.ChaptersContainer(gallery_object)
        metafile = utils.GMetafile()
        try:
            log_d('Listing dir...')
            con = scandir.scandir(path)  # list all folders in gallery dir
            log_i('Gallery source is a directory')
            log_d('Sorting')
            chapters = sorted([
                sub.path for sub in con
                if sub.is_dir() or sub.name.endswith(utils.ARCHIVE_FILES)
            ])  #subfolders
            # if gallery has chapters divided into sub folders
            if len(chapters) != 0:
                log_d('Chapters divided in folders..')
                for ch in chapters:
                    chap = chap_container.create_chapter()
                    chap.title = utils.title_parser(ch)['title']
                    chap.path = os.path.join(path, ch)
                    metafile.update(utils.GMetafile(chap.path))
                    chap.pages = len(list(scandir.scandir(chap.path)))

            else:  #else assume that all images are in gallery folder
                chap = chap_container.create_chapter()
                chap.title = utils.title_parser(
                    os.path.split(path)[1])['title']
                chap.path = path
                metafile.update(utils.GMetafile(path))
                chap.pages = len(list(scandir.scandir(path)))

        except NotADirectoryError:
            if path.endswith(utils.ARCHIVE_FILES):
                gallery_object.is_archive = 1
                log_i("Gallery source is an archive")
                archive_g = sorted(utils.check_archive(path))
                for g in archive_g:
                    chap = chap_container.create_chapter()
                    chap.path = g
                    chap.in_archive = 1
                    metafile.update(utils.GMetafile(g, path))
                    arch = utils.ArchiveFile(path)
                    chap.pages = len(arch.dir_contents(g))
                    arch.close()

        metafile.apply_gallery(gallery_object)
        if add_to_model:
            self.SERIES.emit([gallery_object])
            log_d('Sent gallery to model')
예제 #2
0
파일: fetch.py 프로젝트: twiddli/happypanda
    def create_gallery(self,
                       path,
                       folder_name,
                       do_chapters=True,
                       archive=None):
        is_archive = True if archive else False
        temp_p = archive if is_archive else path
        folder_name = folder_name or path if folder_name or path else os.path.split(
            archive)[1]
        if utils.check_ignore_list(temp_p) and not GalleryDB.check_exists(
                temp_p, self.galleries_from_db, False):
            log_i('Creating gallery: {}'.format(
                folder_name.encode('utf-8', 'ignore')))
            new_gallery = Gallery()
            images_paths = []
            metafile = utils.GMetafile()
            try:
                con = scandir.scandir(
                    temp_p)  #all of content in the gallery folder
                log_i('Gallery source is a directory')
                chapters = sorted([sub.path for sub in con if sub.is_dir() or sub.name.endswith(utils.ARCHIVE_FILES)])\
                    if do_chapters else [] #subfolders
                # if gallery has chapters divided into sub folders
                numb_of_chapters = len(chapters)
                if numb_of_chapters != 0:
                    log_i('Gallery has {} chapters'.format(numb_of_chapters))
                    for ch in chapters:
                        chap = new_gallery.chapters.create_chapter()
                        chap.title = utils.title_parser(ch)['title']
                        chap.path = os.path.join(path, ch)
                        chap.pages = len([
                            x for x in scandir.scandir(chap.path)
                            if x.name.endswith(utils.IMG_FILES)
                        ])
                        metafile.update(utils.GMetafile(chap.path))

                else:  #else assume that all images are in gallery folder
                    chap = new_gallery.chapters.create_chapter()
                    chap.title = utils.title_parser(
                        os.path.split(path)[1])['title']
                    chap.path = path
                    metafile.update(utils.GMetafile(chap.path))
                    chap.pages = len(list(scandir.scandir(path)))

                parsed = utils.title_parser(folder_name)
            except NotADirectoryError:
                try:
                    if is_archive or temp_p.endswith(utils.ARCHIVE_FILES):
                        log_i('Gallery source is an archive')
                        contents = utils.check_archive(temp_p)
                        if contents:
                            new_gallery.is_archive = 1
                            new_gallery.path_in_archive = '' if not is_archive else path
                            if folder_name.endswith('/'):
                                folder_name = folder_name[:-1]
                                fn = os.path.split(folder_name)
                                folder_name = fn[1] or fn[2]
                            folder_name = folder_name.replace('/', '')
                            if folder_name.endswith(utils.ARCHIVE_FILES):
                                n = folder_name
                                for ext in utils.ARCHIVE_FILES:
                                    n = n.replace(ext, '')
                                parsed = utils.title_parser(n)
                            else:
                                parsed = utils.title_parser(folder_name)

                            if do_chapters:
                                archive_g = sorted(contents)
                                if not archive_g:
                                    log_w('No chapters found for {}'.format(
                                        temp_p.encode(errors='ignore')))
                                    raise ValueError
                                for g in archive_g:
                                    chap = new_gallery.chapters.create_chapter(
                                    )
                                    chap.in_archive = 1
                                    chap.title = parsed[
                                        'title'] if not g else utils.title_parser(
                                            g.replace('/', ''))['title']
                                    chap.path = g
                                    metafile.update(utils.GMetafile(g, temp_p))
                                    arch = utils.ArchiveFile(temp_p)
                                    chap.pages = len([
                                        x for x in arch.dir_contents(g)
                                        if x.endswith(utils.IMG_FILES)
                                    ])
                                    arch.close()
                            else:
                                chap = new_gallery.chapters.create_chapter()
                                chap.title = utils.title_parser(
                                    os.path.split(path)[1])['title']
                                chap.in_archive = 1
                                chap.path = path
                                metafile.update(utils.GMetafile(path, temp_p))
                                arch = utils.ArchiveFile(temp_p)
                                chap.pages = len(arch.dir_contents(''))
                                arch.close()
                        else:
                            raise ValueError
                    else:
                        raise ValueError
                except ValueError:
                    log_w('Skipped {} in local search'.format(
                        path.encode(errors='ignore')))
                    self.skipped_paths.append((
                        temp_p,
                        'Empty archive',
                    ))
                    return
                except app_constants.CreateArchiveFail:
                    log_w('Skipped {} in local search'.format(
                        path.encode(errors='ignore')))
                    self.skipped_paths.append((
                        temp_p,
                        'Error creating archive',
                    ))
                    return
                except app_constants.TitleParsingError:
                    log_w('Skipped {} in local search'.format(
                        path.encode(errors='ignore')))
                    self.skipped_paths.append((
                        temp_p,
                        'Error while parsing folder/archive name',
                    ))
                    return

            new_gallery.title = parsed['title']
            new_gallery.path = temp_p
            new_gallery.artist = parsed['artist']
            new_gallery.language = parsed['language']
            new_gallery.info = ""
            new_gallery.view = app_constants.ViewType.Addition
            metafile.apply_gallery(new_gallery)

            if app_constants.MOVE_IMPORTED_GALLERIES and not app_constants.OVERRIDE_MOVE_IMPORTED_IN_FETCH:
                new_gallery.move_gallery()

            self.LOCAL_EMITTER.emit(new_gallery)
            self._data.append(new_gallery)
            log_i('Gallery successful created: {}'.format(
                folder_name.encode('utf-8', 'ignore')))
            return True
        else:
            log_i('Gallery already exists or ignored: {}'.format(
                folder_name.encode('utf-8', 'ignore')))
            self.skipped_paths.append((temp_p, 'Already exists or ignored'))
            return False