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('db4minaei') #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) plan1 = {} query = """select * from dbo.[lwplan] where [f_parentid]=766 order by [ordno] """ cursor.execute(query) i = 0 while 1: i=i+1 row = cursor.fetchone() if not row: break plan1[row.ID] = normalYehKe(row.caption) # print(plan1) plan2 = {} query = """select * from dbo.[lwplan] where [f_parentid]=21185 order by [ordno] """ cursor.execute(query) i = 0 while 1: i=i+1 row = cursor.fetchone() if not row: break plan2[row.ID] = normalYehKe(row.caption) # print(plan2) # ######################################################### query = ''' SELECT [f_lwplanid] ,[f_lwlawsecid] ,[planstate] ,[ID] FROM [db4minaei].[dbo].[lwlawplan] where f_lwlawsecid is not null and f_lwlawsecid > 0 order by f_lwlawsecid, planstate, f_lwplanid ''' cursor.execute(query) url = BASE_URL + "repo/majles/section/multi/update" dict_topic = json.loads(open("list_LWTopic.json").read()) headers = HEADERS total_count = 0 bulk_data = [] previd = '' tcode_main_old = [] tcode_sub_old = [] tcode_main = [] tcode_sub = [] prev_sectionid = 0 prev_planid = 0 prev_planstate = 0 prev2_sectionid = 0 prev2_planid = 0 prev2_planstate = 0 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) sectionid = row.f_lwlawsecid planid = row.f_lwplanid planstate = row.planstate if prev_sectionid == 0 : prev_sectionid = sectionid prev_planid = planid prev_planstate = planstate continue if sectionid == prev_sectionid and planid == prev_planid and planstate == prev_planstate : continue if sectionid == prev_sectionid : if prev_planid in plan1 : if prev_planstate == 1 : tcode_main_old.append(plan1[prev_planid]) else : tcode_sub_old.append(plan1[prev_planid]) elif prev_planid in plan2 : if prev_planstate == 1 : tcode_main.append(plan2[prev_planid]) else : tcode_sub.append(plan2[prev_planid]) prev_planid = planid prev_planstate = planstate continue eid='qs'+ str(prev_sectionid) data={ 'id' : eid, 'tcode_main' : tcode_main, 'tcode_sub' : tcode_sub, 'tcode_main_old' : tcode_main_old, 'tcode_sub_old' : tcode_sub_old, } # print(eid) bulk_data.append(data) prev_sectionid = sectionid tcode_main_old = [] tcode_sub_old = [] tcode_main = [] tcode_sub = [] if len(bulk_data) >= 100: total_count += len(bulk_data) print('=' * 10, ', 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 == 500 : print(response.text) bulk_data = [] break bulk_data = pops_data # print(response) # print(response.text) # print(row.txt) # ///////////////// prev2_sectionid = prev_sectionid prev2_planid = prev_planid prev2_planstate = prev_planstate prev_sectionid = sectionid prev_planid = planid prev_planstate = planstate # if prev2_sectionid == prev_sectionid : if prev_planid in plan1 : if prev_planstate == 1 : tcode_main_old.append(plan1[prev_planid]) else : tcode_sub_old.append(plan1[prev_planid]) elif prev_planid in plan2 : if prev_planstate == 1 : tcode_main.append(plan2[prev_planid]) else : tcode_sub.append(plan2[prev_planid]) # else : eid='qs'+ str(prev_sectionid) data={ 'id' : eid, 'tcode_main' : tcode_main, 'tcode_sub' : tcode_sub, 'tcode_main_old' : tcode_main_old, 'tcode_sub_old' : tcode_sub_old, } bulk_data.append(data) if len(bulk_data) > 0: total_count += len(bulk_data) print('=' * 10, ', 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(response.text) cursor.close() cnxn.close() print('end success') t2= datetime.datetime.now() print(t2-t1)