This commit is contained in:
hsafaei
2026-02-01 13:40:59 +03:30
3 changed files with 165 additions and 4 deletions
+5
View File
@@ -14,6 +14,7 @@ config.env
.env.local
.env.*.local
/__pycache__
/monir/.env
# Log files
npm-debug.log*
@@ -33,3 +34,7 @@ yarn-error.log*
*.zip
analyzed_domains.json
# monir test
.env
app/monir/.env
041106_لیست تمام موضوعات.json
041106_لیست تمام موضوعات.xlsx
-4
View File
@@ -1,4 +0,0 @@
ES_URL = 'http://192.168.23.60:6900'
ES_USER_NAME = 'elastic'
ES_PASSWORD = '1234'
LLM_URL = 'http://2.188.15.102:8001/v1/'
+160
View File
@@ -0,0 +1,160 @@
import traceback, os, orjson
import pandas as pd
from pathlib import Path
from dataParsers.app.common.elastic_helper import ElasticHelper
from dotenv import load_dotenv
def load_orjson(path: str | Path):
path = Path(path)
with path.open("rb") as f: # باید باینری باز بشه برای orjson
return orjson.loads(f.read())
def save_orjson(path, data):
with open(path, "wb") as f:
f.write(
orjson.dumps(data, option=orjson.OPT_INDENT_2 | orjson.OPT_NON_STR_KEYS)
)
def extract_excel_fields(file_path: str, fields: list = [], sheet_name: str = None):
"""
فایل اکسل را باز کرده و فقط فیلدهای خواسته‌شده را به‌صورت لیست از دیکشنری‌ها برمی‌گرداند.
:param file_path: مسیر فایل اکسل (.xlsx یا .xls)
:param fields: لیست نام ستون‌هایی که باید خوانده شوند
:return: لیستی از دیکشنری‌ها شامل داده‌های ستون‌های انتخاب‌شده
"""
try:
# خواندن فایل
df = pd.read_excel(file_path, sheet_name=sheet_name)
df.columns = df.columns.map(str)
if len(fields) > 0:
missing = [f for f in fields if f not in df.columns]
if missing:
print(f"⚠️ This columns mot found in file {missing}")
# for i in df.columns:
# print(f'-field ->{i}<-')
df = df[[f for f in fields if f in df.columns]]
# تبدیل به لیست از دیکشنری‌ها
data = df.to_dict(orient="records")
return data
except Exception as e:
print(f"❌ خطا در خواندن فایل اکسل: {e}")
return []
def save_excel(file_path: str, data: list):
"""ذخیره‌ی داده‌ها در فایل اکسل جدید"""
try:
df = pd.DataFrame(data)
df.to_excel(file_path, index=False)
print(f"✅ فایل با موفقیت ذخیره شد: {file_path}")
except Exception as e:
print(f"❌ خطا در ذخیره فایل اکسل: {e}")
def convert_data(item):
_id = "gnp" + str(item["id"])
tree_label = item["person_title"]
title = item["title"]
papa_id = item["papa_id"]
if papa_id == 0:
parent_id = str(0)
else:
parent_id = "gnp" + str(papa_id)
child_order = item["order"]
return {
"id": _id, # "gnt01741"
"version_key": "base_1",
"tree_key": "tree1",
"tree_label": tree_label, # "فهرست موضوعات مسائل و پاسخها",
"main_type": "tree",
"title": title, # "پیامدهای روبنایی",
"parent_id": parent_id,
"child_order": child_order,
"tags": [],
"meta_info": {},
}
load_dotenv()
es_pass = os.getenv("ES_PASSWORD")
es_url = os.getenv("ES_URL")
es_user = os.getenv("ES_USER_NAME")
es_index_name = os.getenv("GNP_TREE_INDEX_NAME")
es_helper = ElasticHelper(es_pass=es_pass, es_url=es_url, es_user=es_user)
if __name__ == "__main__":
file_path = r"D:\init_mahdi\dataParsers\041106_لیست تمام موضوعات.xlsx"
data_excel = extract_excel_fields(file_path, sheet_name="Sheet1")
print(f'data_excel {len(data_excel)}')
try:
f_data = []
for i in data_excel:
f_data.append(convert_data(i))
except Exception as e:
print(
f"*************",
)
traceback.print_exc()
print(f'f_data {len(f_data)}')
output_path = r"D:\init_mahdi\dataParsers\041106_لیست تمام موضوعات.json"
save_orjson(path=output_path, data=f_data)
bulk_data=[]
index_name_o = es_index_name
elastic = es_helper
total_count = 0
data_list = f_data
i = 0
for data in data_list :
eid = data["id"]
i = i + 1
if len(bulk_data) >= 100:
total_count += len(bulk_data)
print('=' * 10, 'to_elastic , total='+str(total_count) , str(i)+ ', id=' + eid )
elastic.importBulkList(bulk_data, index_name_o)
bulk_data = []
bulk_data.append(
{
'_id' : eid,
'_source' : data
}
)
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 = []
print("end success import_to_elastic")
# for i in f_data:
# es_res = es_helper.update_index_doc(
# is_update_state=False,
# index_name_o=es_index_name,
# eid=i["id"],
# data=i,
# )
# python -m dataParsers.app.monir.gnp_tree