import pyodbc import requests from bs4 import BeautifulSoup from html import escape import sys import json from decimal import Decimal from app.common.general_functions import * cursor = 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 F_LWSECTIONID1 ,l1.[F_LWLAWID] as F_LWLAWID1 ,l1.F_LWPHASEID as F_LWPHASEID1 ,l1.[F_LWSECTIONID_REPLACED] as F_LWSECTIONID2 ,l3.SECTIONTEXT ,l4.F_LWLAWID_EFFECTIVE as F_LWLAWID2 ,l4.F_LWPHASEID_EFFECTIVE as F_LWPHASEID2 FROM [AIPLawDCIT].[dbo].[lwsection] l1 left join [AIPLawDCIT].[dbo].[lwsection] l2 on l1.F_LWSECTIONID_REPLACED = l2.id left join lwsectionlog l3 on l1.id=l3.F_LWSECTIONID left join lwlawchange l4 on l1.[F_LWLAWCHANGEID]=l4.id left join lwphase l6 on l4.F_LWLAWID_EFFECTIVE = l6.id where l1.[F_LWSECTIONID_REPLACED] is not null order by l1.F_LWLAWID''' cursor.execute(query) url = "https://api.tavasi.ir/repo/majles/section/add" url_relation = "https://api.tavasi.ir/relation/main/add" headers = HEADERS headers2 = { "Accept": ACCEPT, "Content-Type": "application/x-www-form-urlencoded", "Authorization": TOKEN, } rel_id_replaced = 100 rel_id_effective = 102 i = 1 while 1: row = cursor.fetchone() if not row: break print(str(i)+ ', qanonid='+str(row.F_LWLAWID1) + ', id=' + str(row.F_LWSECTIONID1)) i=i+1 if row.F_LWLAWID2 is not None and str(row.F_LWLAWID2) != '' : payload = { "check_exist": 1, "service_id1": 'repo', "entity_type1": 'mqanon', "entity_id1": 'mj_mtaghnin_'+ str(row.F_LWLAWID2), "relation_id": rel_id_effective, "service_id2": 'repo', "entity_type2": 'mqanon', "entity_id2": 'mj_mtaghnin_'+ str(row.F_LWLAWID1), } response = requests.request("POST", url_relation, headers=headers2, data=payload) print(response) payload = { "check_exist": 0, "service_id1": 'repo', "entity_type1": 'mqsection', "entity_id1": 'mj_mq_section_'+ str(row.F_LWSECTIONID2), "relation_id": rel_id_replaced, "service_id2": 'repo', "entity_type2": 'mqsection', "entity_id2": 'mj_mq_section_'+ str(row.F_LWSECTIONID1), } response = requests.request("POST", url_relation, headers=headers2, data=payload) print(response) # if row.SECTIONTEXT is not None and str(row.SECTIONTEXT) != '' : # eid = 'mj_mq_section_'+ str(row.F_LWSECTIONID2) # new_content = row.SECTIONTEXT # soup = BeautifulSoup(new_content, "html.parser") # text = soup.get_text("\n", strip=True) # content_len = text.__len__() # data={ # 'id':eid, # 'content': new_content, # 'content_len': content_len, # 'ref_content':escape(new_content) # } # payload = json.dumps(data, cls=JSONEncoder) #Works! # response = requests.request("POST", url, headers=headers, data=payload) # result = json.loads( response ) # # result["meta"][0]["1"] cursor.close() cnxn.close() print('end success')