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 * # ضرورتا # این برنامه ضرورت دارد اجرا شود cursor, cnxn = create_cursor() #Does quasi the same things as json.loads from here: https://pypi.org/project/dynamodb-json/ class JSONEncoder(json.JSONEncoder): def default(self, obj): if isinstance(obj, Decimal): return float(obj) return json.JSONEncoder.default(self, obj) 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.ISLAW = 1 and gl1.[F_LWSECTIONID_EFFECTED] is not null order by gl1.[F_LWSECTIONID_EFFECTED], gl4.APPROVEDATE ''' # ar = [] # for i,item in enumerate(str): # if item == ' ': # print(i) # ar.append(i) # print(len(str.strip().split())) cursor.execute(query) url = BASE_URL + "repo/majles/section/multi/update" dict_topic = json.loads(open("list_LWTopic.json").read()) headers = HEADERS bulk_count = 1 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) bulk_count +=1 ref_state_array = [] previd = eid prev_efect_section_id = efect_section_id prev_effect_type = effect_type ref_state_array.append(ref_state) if bulk_data.__len__() > 100: print('=' * 10 ) print(str(i)+ ', id=' + eid) payload = json.dumps(bulk_data, cls=JSONEncoder) #Works! response = requests.request("POST", url, headers=headers, data=payload) print(response) bulk_data = [] bulk_count = 1 # 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 bulk_data.__len__() > 0: print(bulk_count) payload = json.dumps(bulk_data, cls=JSONEncoder) #Works! response = requests.request("POST", url, headers=headers, data=payload) # print(response.text) cursor.close() cnxn.close() print('end success') t2= datetime.datetime.now() print(t2-t1)