import mariadb from dotenv import load_dotenv import os import datetime import json import uuid import pandas as pd import sys sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "../.."))) from app.common.elastic_helper import ElasticHelper from app.common.general_functions import * from app.common.text_normalizer import TextNormalizer from app.majles.majles_options import * load_dotenv(dotenv_path="config.env") host = os.getenv("HOST_160") # host = os.getenv("HOST_160_LOCAL") password = os.getenv("HOST_160_PASS") # host = os.getenv("HOST_60") # password = os.getenv("HOST_60_PASS") # print(host) # print(password) path_base_map = ( "G:\Project2\gateaway_py\elastic_backend/elastic_backend/app/schemas/mappings/" ) elastic = ElasticHelper(host, password, "elastic", path_base_map) text_normalize = TextNormalizer() def import_simple_list_to_elastic( elastic, items, prefix_id, tree_label, tree_key, group_key="", index_name_o="gn_tree", ): bulk_data = [] total_count = 0 t1 = datetime.datetime.now() print(t1) i = 0 for item in items: # print(i) i = i + 1 row = cursor.fetchone() if not item: break id = prefix_id + str(i).zfill(5) # eid = uuid.uuid4().hex[:8] eid = id title = item.strip() title = text_normalize.normalize_qanon(title) parent_id = "0" child_order = i data = { "id": eid, "version_key": "base_1", "tree_key": tree_key, "tree_label": tree_label, "main_type": "list", "group_key": group_key, "title": title, "parent_id": parent_id, "child_order": child_order, } 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") t2 = datetime.datetime.now() print(t2 - t1) def import_project_to_elastic( cursor, elastic, project_id, project_title, index_name_o="gn_tree" ): query = f""" SELECT id, title, parent, child_order, meta, tags from tlist.tl_subjects where project_id={project_id} ORDER BY parent, child_order """ # print('-'*10) # print(query) # print('-'*10) cursor.execute(query) parent_nodes = {} bulk_data = [] total_count = 0 t1 = datetime.datetime.now() print(t1) i = 0 while 1: # print(i) i = i + 1 row = cursor.fetchone() if not row: break id = row[0] # eid = uuid.uuid4().hex[:8] eid = "gnt" + str(id).zfill(5) title = row[1] if title is None: # داده های خراب continue title = row[1].strip() title = text_normalize.normalize_qanon(title) parent = row[2] parent_id = 0 if parent != 0: parent_id = "gnt" + str(parent).zfill(5) child_order = row[3] meta = row[4] tags = row[5] meta_info = {} if meta: meta_info["id1"] = str(meta) tags_list = [] if tags: for tt in tags.strip().split(","): if tt and not tt in tags_list: tags_list.append(tt) main_type_key = "tree" + str(project_id) data = { "id": eid, "version_key": "base_1", "tree_key": main_type_key, "tree_label": project_title, "main_type": "tree", # "group_key" : "", "title": title, "parent_id": parent_id, "child_order": child_order, "tags": tags_list, "meta_info": meta_info, } 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") t2 = datetime.datetime.now() print(t2 - t1) def import_tlist_projects_to_gn_tree(): config_env = load_dotenv(dotenv_path="./config.env") MYSQL_username = os.getenv("MYSQL_username") MYSQL_root_password = os.getenv("MYSQL_root_password") MYSQL_port = os.getenv("MYSQL_port") # MYSQL_host = os.getenv("MYSQL_import_host") # MYSQL_host = os.getenv("MYSQL_import_host_60") MYSQL_host = os.getenv("MYSQL_import_host_160") cnx = mariadb.connect(user="root", password=MYSQL_root_password, host=MYSQL_host) cursor = cnx.cursor() query = "select id, title from tlist.tl_projects" cursor.execute(query) list_projects = [] while 1: row = cursor.fetchone() if not row: break pid = row[0] # "id" title = row[1] # "title" title = text_normalize.normalize_qanon(title) # print(title) # پلانهای استفاده نشده در مجلس if pid >= 100 and pid <= 200: continue # کدهای تنقیح مجلس که در جای دیگه اضافه میشود if pid == 4 or pid == 5 or pid == 8: continue # موضوعات درختی قدیم و نهادهای حاکمیتی که دیگه جایگزین با 6 شد if pid == 1 or pid == 2: continue list_projects.append({"id": pid, "title": title}) for item in list_projects: print("*" * 100) print(item) import_project_to_elastic(cursor, elastic, item["id"], item["title"], "gn_tree") cursor.close() def import_option_to_elastic( elastic, main_key, main_label, group_key="majles_base", eid="", index_name_o="gn_option", ): res = get_options(main_key) if not res : res = get_options_2(main_key) titles = [] for item in res : title = item["value"] title = text_normalize.normalize_qanon(title) titles.append(title) if not eid: eid = uuid.uuid4().hex[:8] data = { "id": eid, "version_key": "b1", "main_key": main_key, "main_label": main_label, "group_key": group_key, "main_type": "list_simple", "titles": titles, } elastic.es.index(index=index_name_o, id=eid, document=data) return data def import_option_base_list(elastic): op_list = [ { "main_key": "qasection_before_info_plan_type", "main_label": "پرونده - تقدیم - نوع پیش نویس", }, { "main_key": "qasection_before_info_discuss_type", "main_label": "پرونده - نوع شور ", }, {"main_key": "qasection_letters_type", "main_label": "پرونده - نوع نامه نگاری"}, { "main_key": "qasection_before_info_commission_type", "main_label": "پرونده - نوع کمیسیون ", }, { "main_key": "qasection_before_info_commission", "main_label": "پروند - عنوان کمیسیونها", }, {"main_key": "rgsection_tsref", "main_label": "شناسنامه مقررات - مرجع تصویب ها"}, {"main_key": "qasection_tsref", "main_label": "شناسنامه قانون - مرجع تصویب ها"}, { "group_key": "majles_it_base", "main_key": "qasection_qanon_etebar", "main_label": "قانون - وضعیت اعتبار", }, { "group_key": "majles_it_base", "main_key": "qasection_state_etebar", "main_label": "قانون - وضعیت اعتبار جزء", }, {"main_key": "qasection_doc_tags", "main_label": "انواع قانون برای فهرست سازی"}, {"main_key": "qasection_doc_states", "main_label": "صحت سنجی - وضعیت قانون"}, { "main_key": "qasection_title_type", "main_label": "شناسنامه قانون - نوع عنوان", }, { "main_key": "qasection_main_type", "main_label": "شناسنامه قانون - نوع اصلی-تبعی", }, {"main_key": "qasection_sub_type", "main_label": "شناسنامه قانون - نوع فرعی"}, {"main_key": "rgsection_sub_type", "main_label": "شناسنامه مقررات - نوع فرعی"}, {"main_key": "qaopinion_main_type", "main_label": "شناسنامه آراء - نوع اصلی"}, {"main_key": "qaopinion_sub_type", "main_label": "شناسنامه آراء - نوع فرعی"}, {"main_key": "qaopinion_tsref", "main_label": "شناسنامه آراء - مرجع تصویب ها"}, { "group_key": "majles_it_base", "main_key": "dfqanon_effect_type", "main_label": "ورود قانون - نوع اثرگذاری", }, {"main_key": "relation_type", "main_label": "ارتباطات قانون - نوع ارتباط"}, { "group_key": "majles_it_base", "main_key": "relation_to", "main_label": "ارتباطات قانون - نوق مقصد", }, { "group_key": "majles_it_base", "main_key": "relation_from", "main_label": "ارتباطات قانون - نوع مبدا", }, {"main_key": "mashruh_majles", "main_label": "مشروح مذاکرات - نوع مجلس"}, {"main_key": "qasection_mom_type", "main_label": "نوع اصلی-تبعی-مهذب"}, # { # "main_key": "ref_id", # "main_label": "" # }, # { # "main_key": "diff_type", # "main_label": "" # }, ] number = 0 for op in op_list: number = number + 1 print(op["main_key"]) eid = "mb_op_" + str(number) group_key = "عناوین انتخابی فرمها" if "group_key" in op: group_key = op["group_key"] import_option_to_elastic( elastic, op["main_key"], op["main_label"], group_key, eid ) def import_option_base_list_2(elastic): op_list = [ { "main_key": "receive_handle_type", "main_label": "پرونده - اعلام وصول - نوع فوریت", }, { "main_key": "refer_commission_type", "main_label": "پرونده - ارجاع - نوع کمیسیون", }, { "main_key": "commission_report_type", "main_label": "پرونده - گزارش کمیسیون - نوع گزارش ", }, { "main_key": "parl_handle_info_type", "main_label": "پرونده - اعلام وصول - نوع رسیدگی ", }, { "main_key": "parl_handle_demand_type", "main_label": "پرونده - صحن - نوع تقاضاها ", }, { "main_key": "letter_result_type", "main_label": "پرونده - ارسال و دریافت - نتیجه نامه ", }, { "main_key": "eblagh_qanon_time_type", "main_label": "پرونده - نوع زمانی قانون (آزمایشی/موقت) ", }, { "main_key": "eblagh_qanon_template_type", "main_label": "پرونده - نوع قالبی قانون ", }, { "main_key": "qasection_conformity", "main_label": "پرونده - بررسی و تایید محتوا ", }, { "main_key": "qasection_eblagh_letters_type", "main_label": "پرونده - ارسال و دریافت امضاء و انتشار ", } ] list_data = [] number = 0 for op in op_list: number = number + 1 print(op["main_key"]) eid = "mb_op2_" + str(number) group_key = "عناوین انتخابی فرمها" if "group_key" in op: group_key = op["group_key"] _source = import_option_to_elastic( elastic, op["main_key"], op["main_label"], group_key, eid ) list_data.append({ 'id': eid, '_source' : _source }) return list_data def import_option_codes(elastic, index_name_o="gn_option"): ###------------------------------------------- titles = get_codes54() eid = "mb_op_codes54" group_key = "کدهای تنقیح" main_key = "codes54" main_label = "کدهای تنقیح 54 باب" data = { "id": eid, "version_key": "b1", "main_key": main_key, "main_label": main_label, "group_key": group_key, "main_type": "list_simple", "titles": titles, } elastic.es.index(index=index_name_o, id=eid, document=data) ###------------------------------------------- titles = get_codes51() eid = "mb_op_codes51" group_key = "کدهای تنقیح" main_key = "codes51" main_label = "کدهای تنقیح 51 باب" data = { "id": eid, "version_key": "b1", "main_key": main_key, "main_label": main_label, "group_key": group_key, "main_type": "list_simple", "titles": titles, } elastic.es.index(index=index_name_o, id=eid, document=data) ###------------------------------------------- titles = get_codes50() eid = "mb_op_codes50" group_key = "کدهای تنقیح" main_key = "codes50" main_label = "کدهای تنقیح 50 باب" data = { "id": eid, "version_key": "b1", "main_key": main_key, "main_label": main_label, "group_key": group_key, "main_type": "list_simple", "titles": titles, } elastic.es.index(index=index_name_o, id=eid, document=data) ###------------------------------------------- titles = get_codes49() eid = "mb_op_codes49" group_key = "کدهای تنقیح" main_key = "codes49" main_label = "کدهای تنقیح 49 باب" data = { "id": eid, "version_key": "b1", "main_key": main_key, "main_label": main_label, "group_key": group_key, "main_type": "list_simple", "titles": titles, } elastic.es.index(index=index_name_o, id=eid, document=data) ###------------------------------------------- def import_option_parleman_members(elastic, index_name_o="gn_option"): excel_file1 = "data/qavanin_files/list_فهرست نمایندگان دوره 9 تا 12.xlsx" df = pd.read_excel(excel_file1, sheet_name=1) column_names = df.columns.tolist() rows_list = df.to_dict(orient="records") print(column_names) prev_term = 0 pm_list = [] for row in rows_list: code = row[column_names[0]] lname = row[column_names[1]] name = row[column_names[2]] print(code) if code is None: continue code = str(code) if not is_number_string(code) or code == "810102" or len(code) != len("77101"): continue id = int(code) scode = code sterm = scode[0:2] if sterm == "99": sterm = "9" elif sterm == "88": sterm = "8" elif sterm == "77": sterm = "7" elif sterm == "66": sterm = "6" elif sterm == "55": sterm = "5" elif sterm == "44": sterm = "4" elif sterm == "33": sterm = "3" elif sterm == "22": sterm = "2" term = int(sterm) if prev_term == 0: prev_term = term if term != prev_term and pm_list: main_key = "pm_" + str(prev_term) main_label = "نمایندگان دوره " + str(prev_term) group_key = "نمایندگان ادوار مجلس" main_type = "table" object_columns = [ {"key": "title", "title": "نام نماینده", "width": 8}, {"key": "id", "title": "کد نماینده", "width": 4}, ] eid = "mj_" + main_key data = { "id": eid, "version_key": "b1", "main_key": main_key, "main_label": main_label, "group_key": group_key, "object_columns": object_columns, "object_values": pm_list, } elastic.es.index(index=index_name_o, id=eid, document=data) pm_list = [] prev_term = term title = lname + " " + name title = text_normalize.normalize_qanon(title) pm_list.append({"id": str(id), "title": title}) if pm_list: main_key = "pm_" + str(prev_term) main_label = "نمایندگان دوره " + str(prev_term) group_key = "نمایندگان ادوار مجلس" main_type = "table" object_columns = [ {"key": "title", "title": "نام نماینده", "width": 8}, {"key": "id", "title": "کد نماینده", "width": 4}, ] eid = "mj_" + main_key data = { "id": eid, "version_key": "b1", "main_key": main_key, "main_label": main_label, "main_type": main_type, "group_key": group_key, "object_columns": object_columns, "object_values": pm_list, } elastic.es.index(index=index_name_o, id=eid, document=data) def import_option_others(elastic, index_name_o="gn_option"): ###------------------------------------------- res = get_organs() titles = [] for title in res : title = text_normalize.normalize_qanon(title) titles.append(title) eid = "mb_op_organs1" group_key = "عناوین انتخابی فرمها" main_key = "mj_qa_organs" main_label = "سازمانها و نهادهای حاکمیتی" data = { "id": eid, "version_key": "b1", "main_key": main_key, "main_label": main_label, "group_key": group_key, "main_type": "list_simple", "titles": titles, } elastic.es.index(index=index_name_o, id=eid, document=data) ###------------------------------------------ res = [ "دفتر مقام معظم رهبری", "دفتر رئیس جمهور", "دفتر ریاست قوه قضائیه", "دفتر ریاست مجلس شورای اسلامی", "دبیرخانه مجمع تشخیص مصلحت نظام", "دبیرخانه شورای نگهبان", "دفتر معاون اول رئیس جمهور", "معاونت امور مجلس رئیس جمهور", "معاونت حقوقی رئیس جمهور", "کلیه وزارتخانه ها،سازمان ها، مؤسسات دولتی،نهادهای انقلاب اسلامی و استانداری های سراسر کشور", "دیوان محاسبات کشور", "دفتر هیأت دولت", "دبیرخانه شورای اطلاع رسانی دولت", "معاونت امور تقنینی معاونت امور مجلس ریاست جمهوری", "معاونت قوانین مجلس شورای اسلامی", "معاونت نظارت مجلس شورای اسلامی", "معاونت ارتباطات و اطلاع رسانی دفتر رئیس جمهور", "معاونت تدوین، تنقیح و انتشار قوانین و مقررات نهاد ریاست جمهوری", "اداره کل پیگیری طرح ها و لوایح معاونت امور مجلس ریاست جمهوری", "اداره کل اسناد و اطلاع رسانی قوانین مجلس شورای اسلامی", "اداره کل امور تقنینی و کمیسیونها مجلس شورای اسلامی", "سامانه ملی قوانین و مقررات جمهوری اسلامی ایران", "روزنامه رسمی جمهوری اسلامی ایران", ] titles = [] for title in res : title = text_normalize.normalize_qanon(title) titles.append(title) eid = "mb_organ_1" group_key = "عناوین انتخابی فرمها" main_key = "organ_defualt" main_label = "سازمانهای پیش فرض قانون جدید" data = { "id": eid, "version_key": "b1", "main_key": main_key, "main_label": main_label, "group_key": group_key, "main_type": "list_simple", "titles": titles, } elastic.es.index(index=index_name_o, id=eid, document=data) if __name__ == "__main__": index_name_o="gn_option" # ---مرحله اول # import_tlist_projects_to_gn_tree() import_option_base_list(elastic) import_option_parleman_members(elastic) import_option_codes(elastic) import_option_others(elastic) # elastic.backupIndexToZipfile("G:/Project2/111", "gn_tree", "gn_tree") # elastic.backupIndexToZipfile("G:/Project2/111", "gn_option", "gn_option") file_json = "app/../data/option_import_1.json" list_data_import = import_option_base_list_2(elastic) with open(file_json, "w", encoding="utf8") as f: json.dump(list_data_import, f, ensure_ascii=False, indent=4)