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')
def apply_metadata(g, data, append=True): if app_constants.USE_JPN_TITLE: try: title = data['title']['jpn'] except KeyError: title = data['title']['def'] else: title = data['title']['def'] if 'Language' in data['tags']: try: lang = [ x for x in data['tags']['Language'] if not x == 'translated' ][0].capitalize() except IndexError: lang = "" else: lang = "" title_artist_dict = utils.title_parser(title) if not append: g.title = title_artist_dict['title'] if title_artist_dict['artist']: g.artist = title_artist_dict['artist'] g.language = title_artist_dict['language'].capitalize() if 'Artist' in data['tags']: g.artist = data['tags']['Artist'][0].capitalize() if lang: g.language = lang g.type = data['type'] g.pub_date = data['pub_date'] g.tags = data['tags'] else: if not g.title: g.title = title_artist_dict['title'] if not g.artist: g.artist = title_artist_dict['artist'] if 'Artist' in data['tags']: g.artist = data['tags']['Artist'][0].capitalize() if not g.language: g.language = title_artist_dict['language'].capitalize() if lang: g.language = lang if not g.type or g.type == 'Other': g.type = data['type'] if not g.pub_date: g.pub_date = data['pub_date'] if not g.tags: g.tags = data['tags'] else: for ns in data['tags']: if ns in g.tags: for tag in data['tags'][ns]: if not tag in g.tags[ns]: g.tags[ns].append(tag) else: g.tags[ns] = data['tags'][ns] return g
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')
def choose_dir(self, mode): """ Pass which mode to open the folder explorer in: 'f': directory 'a': files Or pass a predefined path """ self.done.show() self.file_exists_lbl.hide() if mode == 'a': name = QFileDialog.getOpenFileName(self, 'Choose archive', filter=utils.FILE_FILTER) name = name[0] elif mode == 'f': name = QFileDialog.getExistingDirectory(self, 'Choose folder') elif mode: if os.path.exists(mode): name = mode else: return None if not name: return head, tail = os.path.split(name) name = os.path.join(head, tail) parsed = utils.title_parser(tail) self.title_edit.setText(parsed['title']) self.author_edit.setText(parsed['artist']) self.path_lbl.setText(name) if not parsed['language']: parsed['language'] = app_constants.G_DEF_LANGUAGE l_i = self.lang_box.findText(parsed['language']) if l_i != -1: self.lang_box.setCurrentIndex(l_i) if gallerydb.GalleryDB.check_exists(name): self.file_exists_lbl.setText( '<font color="red">Gallery already exists.</font>') self.file_exists_lbl.show() # check galleries gs = 1 if name.endswith(utils.ARCHIVE_FILES): gs = len(utils.check_archive(name)) elif os.path.isdir(name): g_dirs, g_archs = utils.recursive_gallery_check(name) gs = len(g_dirs) + len(g_archs) if gs == 0: self.file_exists_lbl.setText( '<font color="red">Invalid gallery source.</font>') self.file_exists_lbl.show() self.done.hide() if app_constants.SUBFOLDER_AS_GALLERY: if gs > 1: self.file_exists_lbl.setText( '<font color="red">More than one galleries detected in source! Use other methods to add.</font>' ) self.file_exists_lbl.show() self.done.hide()
def apply_metadata(g, data, append=True): if app_constants.USE_JPN_TITLE: try: title = data['title']['jpn'] except KeyError: title = data['title']['def'] else: title = data['title']['def'] if 'Language' in data['tags']: try: lang = [x for x in data['tags']['Language'] if not x == 'translated'][0].capitalize() except IndexError: lang = "" else: lang = "" title_artist_dict = utils.title_parser(title) if not append: g.title = title_artist_dict['title'] if title_artist_dict['artist']: g.artist = title_artist_dict['artist'] g.language = title_artist_dict['language'].capitalize() if 'Artist' in data['tags']: g.artist = data['tags']['Artist'][0].capitalize() if lang: g.language = lang g.type = data['type'] g.pub_date = data['pub_date'] g.tags = data['tags'] else: if not g.title: g.title = title_artist_dict['title'] if not g.artist: g.artist = title_artist_dict['artist'] if 'Artist' in data['tags']: g.artist = data['tags']['Artist'][0].capitalize() if not g.language: g.language = title_artist_dict['language'].capitalize() if lang: g.language = lang if not g.type or g.type == 'Other': g.type = data['type'] if not g.pub_date: g.pub_date = data['pub_date'] if not g.tags: g.tags = data['tags'] else: for ns in data['tags']: if ns in g.tags: for tag in data['tags'][ns]: if not tag in g.tags[ns]: g.tags[ns].append(tag) else: g.tags[ns] = data['tags'][ns] return g
def choose_dir(self, mode): """ Pass which mode to open the folder explorer in: 'f': directory 'a': files Or pass a predefined path """ self.done.show() self.file_exists_lbl.hide() if mode == 'a': name = QFileDialog.getOpenFileName(self, 'Choose archive', filter=utils.FILE_FILTER) name = name[0] elif mode == 'f': name = QFileDialog.getExistingDirectory(self, 'Choose folder') elif mode: if os.path.exists(mode): name = mode else: return None if not name: return head, tail = os.path.split(name) name = os.path.join(head, tail) parsed = utils.title_parser(tail) self.title_edit.setText(parsed['title']) self.author_edit.setText(parsed['artist']) self.path_lbl.setText(name) if not parsed['language']: parsed['language'] = app_constants.G_DEF_LANGUAGE l_i = self.lang_box.findText(parsed['language']) if l_i != -1: self.lang_box.setCurrentIndex(l_i) if gallerydb.GalleryDB.check_exists(name): self.file_exists_lbl.setText('<font color="red">Gallery already exists.</font>') self.file_exists_lbl.show() # check galleries gs = 1 if name.endswith(utils.ARCHIVE_FILES): gs = len(utils.check_archive(name)) elif os.path.isdir(name): g_dirs, g_archs = utils.recursive_gallery_check(name) gs = len(g_dirs) + len(g_archs) if gs == 0: self.file_exists_lbl.setText('<font color="red">Invalid gallery source.</font>') self.file_exists_lbl.show() self.done.hide() if app_constants.SUBFOLDER_AS_GALLERY: if gs > 1: self.file_exists_lbl.setText('<font color="red">More than one galleries detected in source! Use other methods to add.</font>') self.file_exists_lbl.show() self.done.hide()
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
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'))
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)