예제 #1
0
				def create_gallery(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(list(scandir.scandir(chap.path)))
									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 = utils.title_parser(g)['title']
												chap.path = g
												metafile.update(utils.GMetafile(g, temp_p))
												arch = utils.ArchiveFile(temp_p)
												chap.pages = len(arch.dir_contents(g))
												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

						new_gallery.title = parsed['title']
						new_gallery.path = temp_p
						new_gallery.artist = parsed['artist']
						new_gallery.language = parsed['language']
						new_gallery.info = ""
						metafile.apply_gallery(new_gallery)
						if app_constants.MOVE_IMPORTED_GALLERIES and not app_constants.OVERRIDE_MOVE_IMPORTED_IN_FETCH:
							new_gallery.path = utils.move_files(temp_p)

						self.data.append(new_gallery)
						log_i('Gallery successful created: {}'.format(folder_name.encode('utf-8', 'ignore')))
					else:
						log_i('Gallery already exists: {}'.format(folder_name.encode('utf-8', 'ignore')))
						self.skipped_paths.append((temp_p, 'Already exists'))
예제 #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
예제 #3
0
				def create_gallery(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 = []
						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
							if len(chapters) != 0:
								log_i('Gallery has chapters divided in directories')
								for numb, ch in enumerate(chapters):
									chap_path = os.path.join(path, ch)
									new_gallery.chapters[numb] = chap_path

							else: #else assume that all images are in gallery folder
								new_gallery.chapters[0] = path
				
							##find last edited file
							#times = set()
							#for root, dirs, files in os.walk(path, topdown=False):
							#	for img in files:
							#		fp = os.path.join(root, img)
							#		times.add( os.path.getmtime(fp) )
							#last_updated = time.asctime(time.gmtime(max(times)))
							#new_gallery.last_update = last_updated
							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 n, g in enumerate(archive_g):
												new_gallery.chapters[n] = g
										else:
											new_gallery.chapters[0] = path
									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)
								return

						new_gallery.title = parsed['title']
						new_gallery.path = temp_p
						new_gallery.artist = parsed['artist']
						new_gallery.language = parsed['language']
						new_gallery.info = "No description.."
						if gui_constants.MOVE_IMPORTED_GALLERIES and not gui_constants.OVERRIDE_MOVE_IMPORTED_IN_FETCH:
							new_gallery.path = utils.move_files(temp_p)

						self.data.append(new_gallery)
						log_i('Gallery successful created: {}'.format(folder_name.encode('utf-8', 'ignore')))
					else:
						log_i('Gallery already exists: {}'.format(folder_name.encode('utf-8', 'ignore')))
						self.skipped_paths.append(temp_p)