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 from config_base import * from general_functions import * from text_normalizer import TextNormalizer def import_law_reg_qanon_to_elastic(cursor): query = ''' SELECT l1.[ID] as Id ,l1.[CAPTION] as title --,[F_LWBASETABLEID_LAWTYPE] ,l1.[ISLAW] -- قانون ,l1.[ISREGULATION] -- مقرره ,isnull(l1.APPROVEDATE,'') as aproveDate -- تاریخ تصویب ,isnull(l1.EXPIREDATE,'') as expireDate -- تاریخ انقضا --,[F_LWBASETABLEID_CLASSIFICATION] ,[RULENO] ,[LAWNO] --,[F_CMBASETABLEID_LASTSTATUS] ,lss.CAPTION as qanon_etebar ,cbt.CAPTION as lawType ,( SELECT top (1) p3.CAPTION FROM [lwPhase] p1 left join [lwApprover] p2 on p1.ID=p2.F_LWPHASEID left join cmOrganization p3 on p2.F_CMORGANIZATIONID = p3.id where l1.ID = p1.F_LWLAWID order by p2.APPROVEDATE asc ) as ts_ref -- مرجع تصویب ,( SELECT cbt.CAPTION FROM lwLaw l2 left JOIN cmBasetable cbt ON l2.[F_LWBASETABLEID_REGULATIONTYPE] = cbt.ID where l1.ID = l2.id ) as regulationType FROM [Qavanin].[dbo].[lwLaw] l1 left JOIN cmBasetable cbt ON l1.F_LWBASETABLEID_LAWTYPE = cbt.ID left join lwLawSectionStatus lss on lss.id= l1.[F_CMBASETABLEID_LASTSTATUS] where ( l1.ISLAW = 1 or l1.ISREGULATION = 1 ) ''' cursor.execute(query) text_normal = TextNormalizer() url = BASE_URL + "repo/majles/qaqanon/multi/add" headers = HEADERS total_count = 0 bulk_data = [] t1= datetime.datetime.now() print(t1) i = 0 while 1: # print(i) i=i+1 row = cursor.fetchone() if not row: break # if i > 1000: # break # print(row.sectionId) title = text_normal.normalize_qanon( row.title ) title_type = getTitleType(title) eid='qq'+ str(row.Id) main_type = '' if(row.ISLAW == 1): main_type = 'قانون' elif(row.ISREGULATION == 1): main_type = 'مقرره' sub_type = '' if(row.ISLAW == 1): sub_type = row.lawType elif(row.ISREGULATION == 1): sub_type = row.regulationType sub_type = normalYehKe(sub_type) qanon_number = '' if(row.ISLAW == 1): qanon_number = row.LAWNO elif(row.ISREGULATION == 1): qanon_number = row.RULENO # other_type = 'اصلی' اصلاحی.... # action_type ... ts_year = '' if row.aproveDate : items = row.aproveDate.split("/") if items.__len__() > 0: ts_year = items[0] ts_ref = normalYehKe(row.ts_ref) sort_date_timestamp = -15000000000 # 9/2/1494, 12:45:44 AM # خطا یا خالی try: sort_date_timestamp = getSortTimestamp(row.aproveDate) except Exception as e: sort_date_timestamp = -15000000000 # 9/2/1494, 12:45:44 AM # خطا یا خالی expire_date = row.expireDate.strip() qanon_etebar = '' if row.qanon_etebar : qanon_etebar = normalYehKe(row.qanon_etebar) data={ 'id':eid, 'ref_id':row.Id, 'main_type': main_type, 'sub_type': sub_type, 'title': title, 'title_type': title_type, 'expire_date': expire_date, 'ts_date': row.aproveDate, 'ts_year': ts_year, 'ts_ref': ts_ref, 'qanon_etebar' : qanon_etebar, 'sort_date_timestamp': sort_date_timestamp, # created from repairQanon.getSortTimestamp } if len(bulk_data) >= 100: total_count += len(bulk_data) print('=' * 10, ' law_reg_qanon_to_elastic , 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) bulk_data.append(data) if len(bulk_data) > 0: total_count += len(bulk_data) print('=' * 10, ' law_reg_qanon_to_elastic , 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('end success law_reg_qanon_to_elastic ') t2= datetime.datetime.now() print(t2-t1)