import numbers import os from dotenv import load_dotenv from elasticsearch import Elasticsearch, helpers from elasticsearch.helpers import scan import json import re import time, traceback, uuid, orjson, jdatetime, re from datetime import datetime, timezone import sys sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "../.."))) from app.common.text_normalizer import TextNormalizer from app.common.general_functions import * from app.common.elastic_helper import ElasticHelper text_normal = TextNormalizer() load_dotenv(dotenv_path='config.env') host=os.getenv('HOST_60') password=os.getenv('HOST_60_PASS') # print( "---------------->>>>>>>>>>>>>>>", host ) # host=os.getenv('HOST_113') # password=os.getenv('HOST_113_PASS') print('.... majles_codes_update_steps ......', host) path_base_map = "G:/Project2/gateway_php/repo/_other/" elastic = ElasticHelper(host, password, "elastic", path_base_map) index_name_o = "mn_voice" path_files = "app/../data/monir/spr_v1/farhangestan-output" bulk_data = [] total_count = 0 files = get_files(path_files, r"(\d+)\.json$") # files = get_files(path_files) for file in files : print(file) data = {} try : with open(file, "r", encoding="utf8") as f: data = json.load(f) eid = data["_id"] try: res = elastic.get_document(index_name_o, eid) source = res["_source"] continue except: pass sound_files = data["sound_files"] sound_link = sound_files[0]["link"] sound_title = sound_files[0]["title"] author = data["author"] branch = data["branch"] title = data["title"] begin_date = data["begin_date"] begin_year = data["begin_year"] meet_code = data["meet_code"] meet_no = data["meet_no"] person_code = data["person_code"] city = data["city"] place = data["place"] _format = data["format"] words = [] transcript = data["transcript"] content = "" if transcript : content = transcript[0]["full_text"] words = transcript[0]["words"] else : if "content" in data : content = data["content"] if "words_align" in data : words = data["words_align"] content = text_normal.normalize_qanon(content) content = content.strip().strip('\r\t\n') word_times = [] for w in words : word_times.append(w["start"]) data_item = { "id": eid, "title": title, "author": author, "branch": branch, "begin_date": begin_date, "meet_code": meet_code, "meet_no": meet_no, "person_code": person_code, "city": city, "place": place, "format": _format, "content": content, "sound_link": sound_link, "sound_title": sound_title, "word_times" : word_times } bulk_data.append( {"_id": eid, "_source": data_item} ) if len(bulk_data) >= 100: total_count += len(bulk_data) print( "=" * 10, "to_elastic , total=" + str(total_count) ) elastic.importBulkList(bulk_data, index_name_o) bulk_data = [] except Exception as e: print("error in file " + file + str(e)) if len(bulk_data) >= 0: total_count += len(bulk_data) print( "=" * 10, "to_elastic , total=" + str(total_count) ) elastic.importBulkList(bulk_data, index_name_o) bulk_data = []