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 * from text_normalizer import TextNormalizer def import_relations_qanon_lwreg(cursor): # اجرای کوئری 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) text_normal = TextNormalizer() url = BASE_URL + "repo/majles/qaqanon/multi/update" headers = HEADERS total_count = 0 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 = '' # rows = cursor.fetchall() # for row in rows: while 1: i += 1 row = cursor.fetchone() if not row: break eid='qq'+ str(row.id) to_id = 'qq'+ str(row.to_id) sectionid = str(row.section_id) if sectionid != 'None' : sectionid = 'qs'+ str(row.section_id) else : sectionid = '' if (i%100) == 0 : print('^' * 10, ' id=', eid, ' i=',i) # ارتباط با خودش بی معنی است # چند مورد در دیتابیس بود 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 = text_normal.normalize_qanon(row.from_section_title) to_title = text_normal.normalize_qanon( 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 prev_eid == '': prev_eid = eid continue elif prev_eid == eid : relations.append(relation) continue else : data={ 'id' : prev_eid, 'qanon_relations' : relations } total_count = total_count+1 # if total_count < 2560 : # continue bulk_data.append(data) prev_eid = eid relations=[] relations.append(relation) if len(bulk_data) >= 10: total_count += len(bulk_data) print('=' * 10, ' relations_qanon_lwreg , 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 len(relations) : data={ 'id' : prev_eid, 'qanon_relations' : relations } bulk_data.append(data) while len(bulk_data) > 0 : total_count += len(bulk_data) print('=' * 10, ' relations_qanon_lwreg , total='+str(total_count)) payload = json.dumps(bulk_data, cls=JSONEncoder) #Works! 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(bulk_data) print(response.text) bulk_data = [] bulk_data = pops_data if not bulk_data : break t2= datetime.datetime.now() print("finished time: " + str(t2)) print("process time: " + str(t2-t1)) print(' end success relations_qanon_lwreg ')