def check_hash(cls, f_hash, file): with db_session: FSWorker.log('DB object', file) obj = ArtObject.get(id=f_hash) if obj is None: return False return True
def write_hash(cls, f_hash, path, preview=''): with db_session: obj = ArtObject(id=f_hash, path=path, f_type=FSWorker.get_type(path), ext=FSWorker.get_ext(path), preview=preview)
def execute(self): api_id = 263724 api_hash = "3a8ae22a8e6981038a370c7149d51fc2" sn_path = Path.join(Path.dirname(__file__), 'session_name') client = TelegramClient(sn_path, api_id, api_hash) client.connect() username = '******' try: for message in client.get_messages(username, limit=1): if not DBWorker.get_telega_id_date(message.id): file = client.download_media(message, Config().type_paths.get('import')) FSWorker.log('From telega was downloaded: "{}"'.format(file)) DBWorker.write_telega_id(message.id) except Exception as err: FSWorker.log("Fucken bitch dead again. BIIIIITCH")
def __date_to_folder(self, date, f_type): type_path = Config().type_paths.get(f_type) if type_path is not None: path = Path.join(Path.dirname(__file__), '..', type_path, str(date.year), str(date.month), str(date.day)) if FSWorker.check_create(path, False): return path return None
def __check_folders(self, path): for f_type in FSWorker.get_all_types(path): folder = Config().type_paths.get(f_type) FSWorker.log('type folder', folder) if folder is None: continue FSWorker.log(FSWorker.check_create(Path.join(Path.dirname(__file__), '..', folder), False))
def main(): """ Запуск Наблюдателя(синглтон): Наблюдатель работает весь цикл приложения и отследживает события, инициализированные при запуске. Инициализация: * после обработки передает сообщение наблюдателю - создание синглтона конфигурации - заполнение событиями посредника - проверка наличия необходимых ресурсов (папка import, jpeg, raw, vid, prew) так же необходимо реализовать восстановление бд или подключение к другой базе extra - наличие установленных приложений (gimp, darktable) Запуск проверки наличия новых данных для обработки: * после обработки передает сообщение наблюдателю """ config = Config() Flags() FSWorker.log('config3', config.__dict__) main_init = Initializer() """ здесь можно добавлять обработчики""" main_init.q9 = TelegaUpdateChecker() FSWorker.log('Initializer2', main_init.__dict__) for item in main_init: FSWorker.log((item)) main_init.start_works() FSWorker.log('config4', main_init.__dict__)
path = 'common/default.json' type_paths = {} type_paths['raster-image'] = Path.join('storage', 'photo', 'raster-image') type_paths['raw-image'] = Path.join('storage', 'photo', 'raw-image') type_paths['video'] = Path.join('storage', 'video') type_paths['preview'] = Path.join('storage', 'photo', 'preview') type_paths['import'] = Path.join('import') with open(path, 'w') as f: json.dump(type_paths, f, indent=4) with open(path) as f: tree = json.load(f) return tree if __name__ == '__main__': with open('common/default.json', 'w') as f: pass FSWorker.log(read_paths()) # os.mkdir(paths['video'], mode=0o777, *, dir_fd=None)
def __init__(self, path): self.path = path self.type = FSWorker.get_type(path) self.archive_path = self.path_from_date()
def execute(self): """ перенос """ if not Flags().got_work: return # проверить папки по типам import_path = Path.join(Path.dirname(__file__), '..', Config().type_paths.get('import')) self.__check_folders(import_path) types = FSWorker.get_all_types(import_path) # получить список файлов files = FSWorker.get_all_files(import_path) # пройти по файлам for file, f_type in files.items(): if f_type not in types: continue if not FSWorker.check_free_space(file): FSWorker.log('For "{}" not enaugh space'.format(file)) raise Exception f_hash = FSWorker.get_hash_md5(file) FSWorker.log(f_hash, file) if not DBWorker.check_hash(f_hash, file): # проверить хэш date = FSWorker.get_born_date(file) FSWorker.log('born date of "{}" is "{}"'.format(file, date)) # создать/проверить папку folder = self.__date_to_folder(date, f_type) if folder is None: continue _, f = Path.split(file) src = FSWorker.get_filename(Path.join(folder, f)) if FSWorker.copy_file(file, src) and FSWorker.check_create(src): # переместить и записать хэш if f_hash == FSWorker.get_hash_md5(src): # запись хэша DBWorker.write_hash(f_hash, src) FSWorker.remove(file) else: FSWorker.remove(file) FSWorker.remove_empty(import_path) self.make_done()
def __init__(self): super().__init__() FSWorker.log('ToArchiveMover started')
def __got_work(self): if FSWorker.get_all_types(self.__import_path): FSWorker.log('got work', self.__import_path) return True return False
def execute(self): if self.__got_work(): Flags().got_work = True FSWorker.log('nu kakto tak') self.make_done()
def __init__(self): super().__init__() FSWorker.log('FolderUpdateChecker started') FSWorker.log(Config().type_paths.get('import')) self.__import_path = Config().type_paths.get('import')
def skip_all_done(self): for key, ex in self: ex.make_undone() FSWorker.log(ex, 'made undone')
def start_works(self): FSWorker.log('start_works') for key, ex in self: FSWorker.log('Now working:', key, ex) ex.execute() FSWorker.log('ended works')
def __check_start_paths(self): FSWorker.check_create(Config().type_paths.get('import'), False)