import json import requests from config_base import * import requests from bs4 import BeautifulSoup from html import escape import json from decimal import Decimal import datetime 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) # اجرای کوئری SQL و خواندن داده‌ها به یک DataFrame query = """ SELECT l1.F_LWLAWID_LAW as id , l1.F_LWSECTIONID as section_id --, l2.CAPTION as from_title -- ,(select top(1) tt1.id from lwSection tt1 where tt1.F_LWLAWID=l1.F_LWLAWID_LAW and tt1.FULLPATH = 'عنوان') as section_id2 , l3.[FULLPATH] as from_section_title , l1.ISREFERENCE , l1.F_LWLAWID_REGULATION as to_id , l4.CAPTION as to_title , 'مقرره' as to_type FROM [Qavanin].[dbo].[lwLawRegulation] l1 left join lwLaw l2 on l1.F_LWLAWID_LAW=l2.id left join lwsection l3 on l1.F_LWSECTIONID=l3.id left join lwLaw l4 on l1.F_LWLAWID_REGULATION=l4.id order by l1.F_lwlawid_law, l1.F_LWLAWID_REGULATION , l1.F_LWSECTIONID """ cursor.execute(query) url = BASE_URL + "repo/majles/qaqanon/multi/update" headers = HEADERS bulk_count = 1 bulk_data = [] previd = '' ref_state_array = [] t1= datetime.datetime.now() print("start time: " + str(t1)) i = 0 all_data = [] repeat_data = [] relations = [] prev_eid = '' prev_sectionid = '' prev_to_id = '' while 1: i += 1 row = cursor.fetchone() if not row: break eid='qq'+ str(row.id) sectionid = str(row.section_id) to_id = str(row.to_id) # ارتباط با خودش بی معنی است # چند مورد در دیتابیس بود if row.id == row.to_id : continue if eid == prev_eid and to_id == prev_to_id : if prev_sectionid == sectionid : continue prev_to_id = to_id prev_sectionid = sectionid rel_type = 'عادی' if int(row.ISREFERENCE) == 1: rel_type = 'استناد' from_type = "کل قانون" if row.section_id : from_type = "جزء قانون" from_section_title = '' if row.from_section_title : from_section_title = row.from_section_title to_title = normalYehKe( row.to_title ) to_type = row.to_type relation = { "from_type": from_type, "from_section_id" : sectionid, "from_section_title" : from_section_title, "rel_type" : rel_type, "to_type" : to_type, "to_id" : to_id , "to_title" : to_title, "wait" : 1, "rel_key" : 'qavanin', } #print('%'*20, 'eid= ', eid, ' , ... prev_eid= ', prev_eid) #print('--------- relations' , len(relations) ) #print(relation ) if (i%100) == 0 : print('^' * 10, ' id=', eid, ' i=',i) if prev_eid == '': prev_eid = eid continue elif prev_eid == eid : relations.append(relation) continue else : data={ 'id' : prev_eid, 'qanon_relations' : relations } bulk_data.append(data) bulk_count +=1 prev_eid = eid relations=[] relations.append(relation) if bulk_count > 10: 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 = 0 # print(response.text) #print(row.txt) if len(relations) : data={ 'id' : prev_eid, 'qanon_relations' : relations } bulk_data.append(data) bulk_count +=1 # print(bulk_count) if len(bulk_data) : payload = json.dumps(bulk_data, cls=JSONEncoder) #Works! response = requests.request("POST", url, headers=headers, data=payload) print(response) # print(response.text ) cursor.close() cnxn.close() t2= datetime.datetime.now() print("update rows count: " + str(len(all_data))) print("finished time: " + str(t2)) print("process time: " + str(t2-t1)) print('*********** end success ***********')