Files
init_dataParser/22_Relations_qanon_opinion.py
T
2024-09-14 10:32:42 +03:30

190 lines
4.7 KiB
Python

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 *
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)
# اجرای کوئری SQL و خواندن داده‌ها به یک DataFrame
query = """
SELECT
l1.[F_LWLAWID] as id
,[F_LWSECTIONID] as sectionid
, ls.[FULLPATH] as from_section_title
,[ISRELATED]
--,[F_CMBASETABLEID_OPBASEDOCTYPE]
--,l4.CAPTION
,[F_OPOPINIONID] as to_id
,og.CAPTION as to_type
,l3.CAPTION as to_title
FROM [Qavanin].[dbo].[opOpinionLaw] l1
left join [opOpinion] l2 on l1.[F_OPOPINIONID] = l2.id
left join opOpiniongiver og on l2.F_OPOPINIONGIVERID = og.id
left join lwLaw l3 on l2.[F_LWLAWID] = l3.id
left join lwsection ls on l1.F_LWSECTIONID=ls.id
--left join lwLaw l4 on l1.[F_LWLAWID] = l4.id
order by l1.[F_LWLAWID], [F_OPOPINIONID], [F_LWSECTIONID]
"""
cursor.execute(query)
url = BASE_URL + "repo/majles/qaqanon/multi/update"
headers = HEADERS
bulk_count = 1
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 = ''
while 1:
i += 1
row = cursor.fetchone()
if not row:
break
eid='qq'+ str(row.id)
sectionid = str(row.sectionid)
to_id = str(row.to_id)
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.ISRELATED) == 1:
rel_type = 'استناد'
from_type = "کل قانون"
if sectionid :
from_type = "جزء قانون"
from_section_title = ''
if row.from_section_title :
from_section_title = row.from_section_title
to_title = normalYehKe( row.to_title )
to_type = row.to_type.strip()
if to_type == 'راي هيات عمومي ديوان عدالت اداري' :
to_type = 'رای دیوان عدالت اداری'
if to_type == 'راي وحدت رويه ديوان عالي كشور' :
to_type = 'رای وحدت رویه'
if to_type == 'اداره کل حقوقی قوه قضائیه' :
to_type = 'نشست قضایی'
if to_type == 'نظر رياست مجلس - اصول 85 و 138' :
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 (i%100) == 0 :
print('^' * 10, ' id=', eid, ' i=',i)
if prev_eid == '':
prev_eid = eid
continue
elif prev_eid == eid :
relations.append(relation)
continue
else :
data={
'id' : prev_eid,
'opinion_relations' : relations
}
bulk_data.append(data)
bulk_count +=1
prev_eid = eid
relations=[]
relations.append(relation)
if bulk_count > 10:
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)
bulk_data = []
bulk_count = 0
# print(response.text)
#print(row.txt)
if len(relations) :
data={
'id' : prev_eid,
'opinion_relations' : relations
}
bulk_data.append(data)
bulk_count +=1
# print(bulk_count)
if len(bulk_data) :
payload = json.dumps(bulk_data, cls=JSONEncoder) #Works!
response = requests.request("POST", url, headers=headers, data=payload)
print(response)
# print(response.text )
cursor.close()
cnxn.close()
t2= datetime.datetime.now()
print("update rows count: " + str(len(all_data)))
print("finished time: " + str(t2))
print("process time: " + str(t2-t1))
print('*********** end success ***********')