def process_ajax(self, sub, req):
     try:
         ret = {'ret': 'success'}
         if sub == 'command':
             command = req.form['command']
             logger.error(f"sub : {sub}  /  command : {command}")
             if command == 'select':
                 ModelSetting.set(f'{self.parent.name}_{self.name}_query',
                                  req.form['arg1'])
                 ret['select'] = PlexDBHandle.tool_select(req.form['arg1'])
             elif command == 'refresh_web':
                 PlexWebHandle.refresh_by_id(req.form['arg1'])
                 ret['msg'] = '명령을 전송하였습니다.'
             elif command == 'refresh_bin':
                 PlexBinaryScanner.scan_refresh2(
                     req.form['arg1'], os.path.dirname(req.form['arg2']))
                 ret['msg'] = '완료'
             elif command == 'analyze_web':
                 PlexWebHandle.analyze_by_id(req.form['arg1'])
                 ret['msg'] = '명령을 전송하였습니다.'
             elif command == 'analyze_bin':
                 PlexBinaryScanner.analyze(
                     req.form['arg1'], metadata_item_id=req.form['arg2'])
                 ret['msg'] = '완료'
             elif command == 'remove_metadata':
                 folder_path = os.path.join(
                     ModelSetting.get('base_path_metadata'),
                     'Movies' if req.form['arg1'] == '1' else 'TV Shows',
                     req.form['arg2'][0], f"{req.form['arg2'][1:]}.bundle")
                 if os.path.exists(folder_path):
                     if ToolBaseFile.rmtree(folder_path):
                         ret['msg'] = '삭제하였습니다.'
                     else:
                         ret['ret'] = 'warning'
                         ret['msg'] = '삭제 실패'
                 else:
                     ret['ret'] = 'warning'
                     ret['msg'] = f'{folder_path} 없음'
         elif sub == 'get_preset':
             ret['preset'] = self.preset
         return jsonify(ret)
     except Exception as e:
         P.logger.error(f'Exception:{str(e)}')
         P.logger.error(traceback.format_exc())
         return jsonify({'ret': 'danger', 'msg': str(e)})
Exemple #2
0
    def start(self):
        #app.config['config']['use_celery'] = False
        #if app.config['config']['use_celery']:
        #    self.update_state(state='PROGRESS', meta=data)
        #else:
        #    P.logic.get_module('jav_censored_tool').receive_from_task(data, celery=False)
        logger.warning(f"Analysis Task.start")
        folder_path = ModelSetting.get(f'{name}_path')
        logger.warning(f"분석 폴더 : {folder_path}")

        for base, dirs, files in os.walk(folder_path):
            if ModelSetting.get_bool(f"{name}_task_stop_flag"):
                logger.warning("사용자 중지")
                return 'stop'
            if len(dirs) == 0 and len(files) == 0:
                if ModelSetting.get_bool(f"{name}_remove_empty_folder"):
                    ret = ToolBaseFile.rmtree(base)
                    logger.error(f"폴더 삭제 : {base} {ret}")

            if len(files) > 0:  # 파일만 있는 폴더
                if base.split('/')[-1] in [
                        'behindthescenes', 'deleted', 'featurette',
                        'interview', 'scene', 'short', 'trailer', 'other'
                ]:
                    logger.warning(f"base : {base}")
                    continue
                #logger.warning(base)
                data = {'folder_path': base}
                Task.analysis(data)
                if app.config['config']['use_celery']:
                    self.update_state(state='PROGRESS', meta=data)
                else:
                    P.logic.get_module(f'{name}').receive_from_task(
                        data, celery=False)
                #return
        logger.warning(f"종료")
        return 'wait'
    def start(self, location, meta_type, folder, dryrun):
        dryrun = True if dryrun == 'true'  else False

        if location == 'Metadata':
            root_path = os.path.join(ModelSetting.get('base_path_metadata'), meta_type)
        elif location == 'Media':
            root_path = os.path.join(ModelSetting.get('base_path_media'), 'localhost')
       
        if folder == 'all':
            folders = os.listdir(root_path)
        else:
            folders = [folder]
        
        db_file = ModelSetting.get('base_path_db')
        con = sqlite3.connect(db_file)
        cur = con.cursor()

        status = {'is_working':'run', 'remove_count' : 0, 'remove_size':0, 'count':0, 'current':0}


        for folder in folders:
            folder_path = os.path.join(root_path, folder)
            if os.path.exists(folder_path) == False:
                continue

            bundle_list = os.listdir(folder_path)
            status['count'] += len(bundle_list)
            for bundle in bundle_list:
                try:
                    if ModelSetting.get_bool('clear_bundle_task_stop_flag'):
                        return 'stop'
                    time.sleep(0.05)
                    status['current'] += 1
                    data = {'folder':folder, 'bundle':bundle, 'status':status}
                    bundle_path = os.path.join(folder_path, bundle)
                    hash_value = folder + bundle.split('.')[0]
                    if location == 'Metadata':
                        ce = con.execute('SELECT * FROM metadata_items WHERE hash = ?', (hash_value,))
                    else:
                        ce = con.execute('SELECT * FROM media_parts WHERE hash = ?', (hash_value,))
                    ce.row_factory = dict_factory
                    fetch = ce.fetchall()
                    if len(fetch) == 1:
                        if location == 'Metadata':
                            data['title'] = fetch[0]['title']
                        else:
                            data['file'] = fetch[0]['file']
                    elif len(fetch) == 0:
                        tmp = ToolBaseFile.size(start_path=bundle_path)
                        data['remove'] = tmp
                        status['remove_size'] += tmp
                        status['remove_count'] += 1
                        if dryrun == False:
                            ToolBaseFile.rmtree(bundle_path)
                    if app.config['config']['use_celery']:
                        self.update_state(state='PROGRESS', meta=data)
                    else:
                        self.receive_from_task(data, celery=False)
                except Exception as e: 
                    logger.error(f'Exception:{str(e)}')
                    logger.error(traceback.format_exc())
        return 'wait'
Exemple #4
0
 def func():
     time.sleep(1)
     ret = ToolBaseFile.rmtree(folder)
     new_data = {'index': index, 'status': 'remove', 'target': folder}
     self.data['data'][index] = new_data
     self.refresh_data(index=index)
    def analysis(data, con, cur):
        #logger.warning(f"분석시작 : {data['db']['title']}")

        Task.thumb_process(data)

        if data['command'] == 'start1':
            return
        
        # 2단계 TAG별 URL 로 세팅하고 xml 파일만 남기고 제거
        if data['dryrun'] == False:
            #sql = 'UPDATE metadata_items SET user_thumb_url = "{}", user_art_url = "{}", user_banner_url = "{}" WHERE id = {} ;'.format(
            #    data['process']['poster']['url'],
            #   data['process']['art']['url'],
            #    data['process']['banner']['url'],
            #    data['db']['id']
            #)
            if 'poster' not in data['process']:
                return
            sql = 'UPDATE metadata_items SET '
            if data['process']['poster']['url'] != '':
                sql += ' user_thumb_url = "{}", '.format(data['process']['poster']['url'])
            if data['process']['art']['url'] != '':
                sql += ' user_art_url = "{}", '.format(data['process']['art']['url'])
            if data['process']['banner']['url'] != '':
                sql += ' user_banner_url = "{}", '.format(data['process']['banner']['url'])
            if sql != 'UPDATE metadata_items SET ':
                sql = sql.strip().rstrip(',')
                sql += '  WHERE id = {} ;'.format(data['db']['id'])
                sql_filepath = os.path.join(path_data, 'tmp', f"movie_{data['db']['id']}.sql")
                PlexDBHandle.execute_query(sql, sql_filepath=sql_filepath)
        
        c_metapath = os.path.join(data['meta']['metapath'], 'Contents')  
        if os.path.exists(c_metapath):
                          
            for f in os.listdir(c_metapath):
                _path = os.path.join(c_metapath, f)
                if f == '_combined':
                    for tag, value in TAG.items():
                        tag_path = os.path.join(_path, value[1])
                        if os.path.exists(tag_path):
                            if data['dryrun'] == False:
                                data['meta']['remove'] += ToolBaseFile.size(start_path=tag_path)
                                ToolBaseFile.rmtree(tag_path)
                            
                    tmp = os.path.join(_path, 'extras')
                    if os.path.exists(tmp) and len(os.listdir(tmp)) == 0:
                        if data['dryrun'] == False:
                            ToolBaseFile.rmtree(tmp)
                    tmp = os.path.join(_path, 'extras.xml')
                    if os.path.exists(tmp):
                        if os.path.exists(tmp):
                            data['meta']['remove'] += os.path.getsize(tmp)
                            if data['dryrun'] == False:
                                os.remove(tmp)
                else:
                    tmp = ToolBaseFile.size(start_path=_path)
                    if data['dryrun'] == False:
                        data['meta']['remove'] += tmp
                        ToolBaseFile.rmtree(_path)
                    else:
                        if f == '_stored':
                            data['meta']['remove'] += tmp

        if data['command'] == 'start2':
            return

        

        media_ce = con.execute('SELECT user_thumb_url, user_art_url, media_parts.file, media_parts.hash FROM metadata_items, media_items, media_parts WHERE metadata_items.id = media_items.metadata_item_id AND media_items.id = media_parts.media_item_id AND metadata_items.id = ?;', (data['db']['id'],))
        media_ce.row_factory = dict_factory
        data['media'] = {'total':0, 'remove':0}

        for item in media_ce.fetchall():
            #logger.warning(d(item))
            if item['hash'] == '':
                continue
            mediapath = os.path.join(ModelSetting.get('base_path_media'), 'localhost', item['hash'][0], f"{item['hash'][1:]}.bundle")
            if os.path.exists(mediapath) == False:
                continue
            data['media']['total'] += ToolBaseFile.size(start_path=mediapath)
            if item['user_thumb_url'].startswith('media') == False:
                img = os.path.join(mediapath, 'Contents', 'Thumbnails', 'thumb1.jpg')
                if os.path.exists(img):
                    data['media']['remove'] += os.path.getsize(img)
                    if data['dryrun'] == False:
                        os.remove(img)
            if item['user_art_url'].startswith('media') == False:
                img = os.path.join(mediapath, 'Contents', 'Art', 'art1.jpg')
                if os.path.exists(img):
                    data['media']['remove'] += os.path.getsize(img)
                    if data['dryrun'] == False:
                        os.remove(img)
    def thumb_process(data):
        data['meta'] = {'remove':0}
        #logger.warning(data['db'])
        if data['db']['metadata_type'] == 1:
            data['meta']['metapath'] = os.path.join(ModelSetting.get('base_path_metadata'), 'Movies', data['db']['hash'][0], f"{data['db']['hash'][1:]}.bundle")
            combined_xmlpath = os.path.join(data['meta']['metapath'], 'Contents', '_combined', 'Info.xml')
        elif data['db']['metadata_type'] == 2:
            data['meta']['metapath'] = os.path.join(ModelSetting.get('base_path_metadata'), 'TV Shows', data['db']['hash'][0], f"{data['db']['hash'][1:]}.bundle")
            combined_xmlpath = os.path.join(data['meta']['metapath'], 'Contents', '_combined', 'Info.xml')
            
        data['meta']['total'] = ToolBaseFile.size(start_path=data['meta']['metapath'])
        if data['command'] == 'start0':
            return
        if os.path.exists(combined_xmlpath) == False:
            return

        Task.xml_analysis(combined_xmlpath, data)
    
        data['process'] = {}
        for tag, value in TAG.items():
            data['process'][tag] = {
                'db' : data['db'][f'user_{value[0]}_url'],
                'db_type' : '', 
                'url' : '',
                'filename' : '',
                'location' : '',
            }

        for tag, value in TAG.items():
            if data['process'][tag]['db'] != '':
                data['process'][tag]['db_type'] = data['process'][tag]['db'].split('//')[0]
                data['process'][tag]['filename'] = data['process'][tag]['db'].split('/')[-1]
                for item in data['info'][value[1]]:
                    if data['process'][tag]['filename'] == item['filename']:
                        data['process'][tag]['url'] = item['url']
                        break

        #logger.error(d(data['process']))
        # 1단계.
        # _combined 에서 ..stored 
        
        not_remove_filelist = []
        c_metapath = os.path.join(data['meta']['metapath'], 'Contents')
        if os.path.exists(c_metapath):
            for f in os.listdir(c_metapath):
                _path = os.path.join(c_metapath, f)
                # 윈도우는 combined에 바로 데이터가 있어서 무조건 삭제?
                if f == '_stored':
                    tmp = ToolBaseFile.size(start_path=_path)
                    data['meta']['stored'] = tmp
                    if platform.system() == 'Windows':
                        data['meta']['remove'] += tmp
                        if data['dryrun'] == False:
                            ToolBaseFile.rmtree(_path)
                elif f == '_combined':
                    for tag, value in TAG.items():
                        tag_path = os.path.join(_path, value[1])
                        #logger.warning(tag_path)
                        if os.path.exists(tag_path) == False:
                            continue
                        for img_file in os.listdir(tag_path):
                            img_path = os.path.join(tag_path, img_file)
                            if os.path.islink(img_path):
                                if os.path.realpath(img_path).find('_stored') == -1:
                                    # 저장된 파일에 대한 링크가 아니기 삭제
                                    # db에 저장된 url이 stored가 아닌 에이전트 폴더를 가로 가르키는 경우가 있음
                                    #logger.warning(img_file)
                                    if img_file == data['process'][tag]['filename']:
                                        logger.error(data['process'][tag]['filename'])
                                        not_remove_filelist.append(data['process'][tag]['filename'])
                                        continue
                                    if data['dryrun'] == False:# and os.path.exists(img_path) == True:
                                        os.remove(img_path)
                            else: #윈도우
                                if img_file != data['process'][tag]['filename']:
                                    # 저장파일이 아니기 때문에 삭제
                                    data['meta']['remove'] += os.path.getsize(img_path)
                                    if data['dryrun'] == False and os.path.exists(img_path) == True:
                                        os.remove(img_path)
                    
            #if len(not_remove_filelist) == 0:
            for f in os.listdir(c_metapath):
                _path = os.path.join(c_metapath, f)
                if f == '_stored' or f == '_combined':
                    continue
                tmp = ToolBaseFile.size(start_path=_path)
                data['meta']['remove'] += tmp
                if data['dryrun'] == False:
                    ToolBaseFile.rmtree(_path)
        #else:
        if not_remove_filelist:
            logger.error(not_remove_filelist)