refactor to auto import

This commit is contained in:
hsafaei
2025-01-13 10:50:00 +03:30
parent e991cc84cb
commit d819408715
35 changed files with 3832 additions and 1504 deletions
+211
View File
@@ -0,0 +1,211 @@
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 *
def import_relations_qanon_opinion(cursor):
# اجرای کوئری SQL و خواندن داده‌ها به یک DataFrame
query = """
SELECT
l1.[F_LWLAWID] as id
,[F_LWSECTIONID] as section_id
, 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
total_count = 0
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_section_id = ''
prev_to_id = ''
while 1:
i += 1
row = cursor.fetchone()
if not row:
break
eid='qq'+ str(row.id)
to_id = 'qo'+ str(row.to_id)
section_id = str(row.section_id)
if section_id != 'None' :
section_id = 'qs'+ str(row.section_id)
else :
section_id = ''
if eid == prev_eid and to_id == prev_to_id :
if prev_section_id == section_id :
continue
prev_to_id = to_id
prev_section_id = section_id
rel_type = 'عادی'
if int(row.ISRELATED) == 1:
rel_type = 'استناد'
from_type = "کل قانون"
if section_id :
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 = ''
if row.to_type :
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" : section_id,
"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)
prev_eid = eid
relations=[]
relations.append(relation)
if len(bulk_data) >= 10:
total_count += len(bulk_data)
print('=' * 10, ' relations_qanon_opinion , 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)
if len(relations) :
data={
'id' : prev_eid,
'opinion_relations' : relations
}
bulk_data.append(data)
while len(bulk_data) > 0 :
total_count += len(bulk_data)
print('=' * 10, ' relations_qanon_opinion , total='+str(total_count))
payload = json.dumps(bulk_data, cls=JSONEncoder) #Works!
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 = []
bulk_data = pops_data
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 relations_qanon_opinion')