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 time 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 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 ) ''' def isNeedHtml(html): if " 1000: # break # print(row.sectionId) title = normalYehKe( row.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 = row.qanon_etebar data={ 'id':eid, 'ref_id':row.Id, 'main_type': main_type, 'sub_type': sub_type, 'title': title, '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 bulk_count > 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) # print(response.text) bulk_data = [] bulk_count = 0 # print(response.text) #print(row.txt) bulk_data.append(data) bulk_count +=1 if bulk_count > 0: print(bulk_count) payload = json.dumps(bulk_data, cls=JSONEncoder) #Works! response = requests.request("POST", url, headers=headers, data=payload) cursor.close() cnxn.close() print('end success') t2= datetime.datetime.now() print(t2-t1)