اخرین تغییرات برای 50 کد
This commit is contained in:
+72
-23
@@ -1,30 +1,79 @@
|
||||
import os
|
||||
import pandas as pd
|
||||
from config_base import *
|
||||
from general_functions import *
|
||||
from text_normalizer import TextNormalizer
|
||||
import pandas as pd
|
||||
import requests
|
||||
import json
|
||||
from text_normalizer import TextNormalizer
|
||||
from config_base import *
|
||||
from general_functions import *
|
||||
|
||||
ADD_ITEM_ROUTE = "subject/add"
|
||||
# TITLE = ""
|
||||
|
||||
def import_codes_to_list(file_excel, project_id):
|
||||
|
||||
df = pd.read_excel(file_excel, sheet_name=0)
|
||||
count = 0
|
||||
rows_list = df.to_dict(orient='records')
|
||||
total = len(df[0])
|
||||
print('total : ', total)
|
||||
|
||||
column_names = df.columns.tolist()
|
||||
print(column_names)
|
||||
try:
|
||||
for row in rows_list:
|
||||
pass
|
||||
|
||||
except Exception as e:
|
||||
print(e)
|
||||
|
||||
def read_excel_data(file_path ):
|
||||
df = pd.read_excel(file_path )
|
||||
df.fillna("", inplace=True)
|
||||
data = df.to_dict(orient="records")
|
||||
|
||||
text_normal = TextNormalizer()
|
||||
for row in data:
|
||||
row["CAPTION"] = text_normal.normalize_qanon(row["عنوان"])
|
||||
row["ID"] = int(row["ردیف"])
|
||||
row["META"] = row.get("توضیحات", "")
|
||||
return data
|
||||
|
||||
def insert_project():
|
||||
projectid = 8 # فرض کنید projectid ثابت است
|
||||
return projectid
|
||||
|
||||
import_codes_to_list("./data/کدهای 50 گانه.xlsx", 8)
|
||||
def process_excel_data(file_path, send_to_server=True):
|
||||
data = read_excel_data(file_path)
|
||||
data = sorted(data, key=lambda x: x["ID"])
|
||||
projectid = insert_project()
|
||||
id_counter = 1 # برای fake_id
|
||||
local_id_map = {} # برای نگهداشتن ID ساختگی در حالت تست
|
||||
output_data = [] # لیستی برای ذخیره خروجی نهایی
|
||||
for row in data:
|
||||
caption = row["CAPTION"]
|
||||
row_id = row["ID"]
|
||||
meta = row.get("META", "")
|
||||
|
||||
# در حالت تست، parent_id از local map خونده میشه
|
||||
payload = {
|
||||
"title": caption,
|
||||
"projectid": projectid,
|
||||
"meta": meta,
|
||||
"otherid": row_id,
|
||||
"listtype": 0,
|
||||
|
||||
}
|
||||
|
||||
if send_to_server:
|
||||
try:
|
||||
response = requests.post(
|
||||
BASE_URL + 'list/' + ADD_ITEM_ROUTE, data=payload, headers=HEADERS )
|
||||
|
||||
|
||||
|
||||
if response.status_code == 200:
|
||||
res_json = response.json()
|
||||
new_id = res_json["data"][0]["id"]
|
||||
print(f"✅ Inserted {caption} => ID: {new_id}")
|
||||
payload["id"] = new_id
|
||||
else:
|
||||
print(f"❌ Error inserting {caption} => Status Code: {response.status_code}")
|
||||
except Exception as e:
|
||||
print(f"❌ Failed to insert {caption} (ID={row_id}): {e}")
|
||||
else:
|
||||
fake_id = id_counter
|
||||
local_id_map[row_id] = fake_id
|
||||
id_counter += 1
|
||||
print(f"🧪 TEST MODE: Would insert => {payload} | Fake ID: {fake_id}")
|
||||
output_data.append(payload)
|
||||
file_name = "mmm1.json"
|
||||
with open(file_name, "w", encoding="utf-8") as file:
|
||||
json.dump(output_data, file, ensure_ascii=False, indent=4)
|
||||
print(f"\n📁 خروجی در فایل «{file_name}» ذخیره شد.")
|
||||
|
||||
if __name__ == "__main__":
|
||||
file_path = "data/کدهای 50 گانه.xlsx"
|
||||
|
||||
process_excel_data(file_path ,send_to_server=True ) # False for test
|
||||
Reference in New Issue
Block a user