import json import requests from config_base import * import pyodbc import requests from bs4 import BeautifulSoup from html import escape import json from decimal import Decimal import datetime import sys import traceback import re from config_base import * from general_functions import * from text_normalizer import TextNormalizer # ضرورتا # این برنامه ضرورت دارد اجرا شود def import_regulation_section_update_ref_state(cursor): query = ''' SELECT gl1.ID as id, gl4.APPROVEDATE as effect_Date, gl3.[SECTIONTEXT] as effect_text, gl3.FULLPATH as effect_section_title, ls2.SECTIONTEXT as effect_prev_text, gl4.CAPTION as effect_qanon, gl1.F_LWLAWID_EFFECTIVE as effect_qanon_id, gl1.[F_LWSECTIONID_EFFECTIVE] as effect_section_id, gl1.[F_LWSECTIONID_EFFECTED] as sectionId, gl2.[SECTIONTEXT], ls1.CAPTION as effect_section_type, gl1.DESCRIPTION as description, gl1.ISIMPLICIT -- تاثیر ضمنی FROM [Qavanin].[dbo].[lwSectionChange] gl1 left join [lwLawSectionStatus] ls1 on ls1.ID=gl1.[F_CMBASETABLEID_SECTIONSTATUS] left join lwSection gl2 on gl2.ID=gl1.[F_LWSECTIONID_EFFECTED] left join lwSection gl3 on gl3.ID=gl1.[F_LWSECTIONID_EFFECTIVE] left join lwLaw gl4 on gl1.F_LWLAWID_EFFECTIVE=gl4.ID left join lwSectionLog ls2 on ls2.ID=gl1.[F_LWSECTIONLOGID_EFFECTED] where gl4.[ISREGULATION] = 1 and gl1.[F_LWSECTIONID_EFFECTED] is not null order by gl1.[F_LWSECTIONID_EFFECTED], gl4.APPROVEDATE ''' cursor.execute(query) url = BASE_URL + "repo/majles/rgsection/multi/update" dict_topic = json.loads(open("list_LWTopic.json").read()) headers = HEADERS total_count = 0 bulk_data = [] previd = '' prev_effect_type = '' prev_efect_section_id = '' ref_state_array = [] t1= datetime.datetime.now() print(t1) i = 0 while 1: # print(i) i=i+1 row = cursor.fetchone() if not row: break # if i > 3: # break # print(row.sectionId) eid='qs'+ str(row.sectionId) # lawId='qq'+ str(row.lawId) if previd == '': previd = eid effect_type = '' if row.effect_section_type == 'معتبر' : effect_type = '' elif row.effect_section_type == 'ساختار الحاقي' or row.effect_section_type == 'ساختار الحاقي مكرر' : effect_type = 'الحاقی' elif row.effect_section_type == 'اصلاحي' or row.effect_section_type == 'نامه اصلاحي': effect_type = 'اصلاحی' elif row.effect_section_type == 'منسوخه' : effect_type = 'نسخی' elif row.effect_section_type == 'احياء' or row.effect_section_type == 'تنفيذ' or row.effect_section_type == 'تمديد': effect_type = 'تمدیدی' elif row.effect_section_type == 'تفسير' : effect_type = 'تفسیری' elif row.effect_section_type == 'ساختار جايگزين' or row.effect_section_type == 'جايگزين شده': effect_type = 'جایگزین' elif row.effect_section_type == 'آزمايشي' : effect_type = '' elif row.effect_section_type == 'موقت' : effect_type = '' elif row.effect_section_type == 'موقوف الاجراء' : effect_type = '' elif row.effect_section_type == 'بااجرامنتفي مي شود' : effect_type = '' if row.ISIMPLICIT : effect_type += ' ضمنی' # if i == 1 : # اولین بار # prev_effect_type = effect_type ref_state = {} if row.effect_qanon_id : efect_section_id='qs'+ str(row.effect_section_id) efect_qanon_id= 'qq'+ str(row.effect_qanon_id) if prev_efect_section_id == '' : prev_efect_section_id = efect_section_id ref_state = { "id" : row.id, "qanon_id": efect_qanon_id, "qanon_title" : normalYehKe(row.effect_qanon), "qanon_date": row.effect_Date, "section_id": efect_section_id, "section_title": normalYehKe(row.effect_section_title), "ref_key": "qavanin", "effect_type" : effect_type, "new_text" : normalYehKe(row.effect_text), "prev_text" : normalYehKe(row.effect_prev_text), "description" : normalYehKe(row.description) } if previd == eid : # موارد کاملا تکراری افزوده نشود if efect_section_id != prev_efect_section_id or prev_effect_type != effect_type: ref_state_array.append(ref_state) prev_efect_section_id = efect_section_id prev_effect_type = effect_type ref_state={} continue data={ 'id' : previd, 'ref_state' : ref_state_array } bulk_data.append(data) ref_state_array = [] previd = eid prev_efect_section_id = efect_section_id prev_effect_type = effect_type ref_state_array.append(ref_state) if len(bulk_data) >= 100: total_count += len(bulk_data) print('=' * 10, ' regulation_section_update_ref_state , total='+str(total_count) , str(i)+ ', id=' + eid) payload = json.dumps(bulk_data, cls=JSONEncoder) #Works! # limit POST Content-Length 8388608 pops_data = [] while len(payload) > 8388608 : el = bulk_data.pop() total_count -= 1 pops_data.append(el) payload = json.dumps(bulk_data, cls=JSONEncoder) #Works! response = requests.request("POST", url, headers=headers, data=payload) if response.status_code != 200 : print(response.text) bulk_data = [] break bulk_data = pops_data # print(response) # print(response.text) # print(row.txt) if ref_state_array.__len__() > 0 : data={ 'id' : previd, 'ref_state' : ref_state_array } bulk_data.append(data) # print(bulk_data) if len(bulk_data) > 0: total_count += len(bulk_data) print('=' * 10, ' regulation_section_update_ref_state , total='+str(total_count) , str(i)+ ', id=' + eid) payload = json.dumps(bulk_data, cls=JSONEncoder) #Works! response = requests.request("POST", url, headers=headers, data=payload) # print(response.text) print('end success import_regulation_section_update_ref_state') t2= datetime.datetime.now() print(t2-t1)