refactor 2
This commit is contained in:
@@ -0,0 +1,199 @@
|
||||
import json
|
||||
import requests
|
||||
import pyodbc
|
||||
import requests
|
||||
from bs4 import BeautifulSoup
|
||||
from html import escape
|
||||
import json
|
||||
from decimal import Decimal
|
||||
import re
|
||||
import datetime
|
||||
|
||||
from common.config_base import *
|
||||
from common.general_functions import *
|
||||
from common.text_normalizer import TextNormalizer
|
||||
|
||||
def import_opinion_to_elastic(cursor) :
|
||||
query = """
|
||||
SELECT
|
||||
isnull(o.[ID],0) as ref_id
|
||||
,isnull(l.CAPTION,'') as title
|
||||
,isnull([SUBJECT],'') as title_popular
|
||||
,isnull([OPINIONDATE],'') as ts_date
|
||||
,isnull(l.APPROVEDATE,'') as ts_date2
|
||||
,isnull(op_r.CAPTION,'') as sub_type
|
||||
,isnull(op_r.[F_CMBASETABLEID_OPINIONGROUP],0) as mainType -- 1419 : رای 1418:نظر
|
||||
--,isnull(F_OPOPINIONGIVERID,0) as ts_ref -- ts_ref
|
||||
--,isnull(bt.CAPTION,'') as classification -- عادی، ندارد مهم نیست
|
||||
,isnull(o.[F_LWLAWID],0) as qanon_id
|
||||
,isnull([DISTRIBUTENO],'') as eblagh_number -- ???????? شماره ابلاغ؟
|
||||
,isnull([DISTRIBUTEDATE],'') as eblagh_date -- ???????? تاریخ ابلاغ
|
||||
,isnull(d.DOCTEXT,'') as content -- !!!!!!!!!!!!!!!!!!!!!! داده های این فیلد حجم زیادی دارد
|
||||
,(SELECT STRING_AGG(CONVERT(NVARCHAR(max),[F_LWTOPICID]), ',') WITHIN GROUP (ORDER BY ID) AS AggregatedResult FROM opOpinionTopic where [F_OPOPINIONID]=o.id) as topics
|
||||
FROM [Qavanin].[dbo].[opOpinion] o
|
||||
left join opOpiniongiver op_r on o.F_OPOPINIONGIVERID = op_r.id
|
||||
left join [cmBasetable] bt on o.F_CMBASETABLEID_CLASSIFICATION = bt.ID
|
||||
left join lwLaw l on o.F_LWLAWID = l.ID
|
||||
left join prDoc d on o.F_PRDOCID = d.ID
|
||||
|
||||
"""
|
||||
|
||||
cursor.execute(query)
|
||||
text_normal = TextNormalizer()
|
||||
|
||||
url = BASE_URL + "repo/majles/opinion/multi/add" # POST /repo/majles/opinion/multi/add
|
||||
dict_topic = json.loads(open("list_LWTopic.json").read())
|
||||
|
||||
headers = HEADERS
|
||||
|
||||
total_count = 0
|
||||
bulk_data = []
|
||||
|
||||
t1 = datetime.datetime.now()
|
||||
print(t1)
|
||||
i = 0
|
||||
while 1:
|
||||
# print(i)
|
||||
i = i + 1
|
||||
row = cursor.fetchone()
|
||||
if not row:
|
||||
break
|
||||
|
||||
# if i > 1000:
|
||||
# break
|
||||
# print(row.sectionId)
|
||||
|
||||
try:
|
||||
soup = BeautifulSoup(row.content, "html.parser")
|
||||
text = soup.get_text("\n", strip=True)
|
||||
except Exception as e:
|
||||
text = row.content
|
||||
text = text_normal.normalize_qanon(text)
|
||||
content_len = text.__len__()
|
||||
|
||||
title = text_normal.normalize_qanon(row.title)
|
||||
|
||||
eid = "qo" + str(row.ref_id)
|
||||
|
||||
qanon_id = "qq" + str(row.qanon_id)
|
||||
|
||||
main_type = ""
|
||||
if row.mainType == 1418:
|
||||
main_type = "نظر"
|
||||
elif row.mainType == 1419:
|
||||
main_type = "رای"
|
||||
|
||||
ts_date = row.ts_date
|
||||
ts_date = ts_date.strip()
|
||||
if not ts_date :
|
||||
ts_date = row.ts_date2
|
||||
ts_date = ts_date.strip()
|
||||
|
||||
ts_year = ""
|
||||
if ts_date:
|
||||
items = ts_date.split("/")
|
||||
if items.__len__() > 1:
|
||||
ts_year = int(items[0].strip())
|
||||
|
||||
version_ifo = {
|
||||
"timestamp": datetime.datetime.now().timestamp(),
|
||||
"operation": "create",
|
||||
"number": 1,
|
||||
}
|
||||
|
||||
topics = []
|
||||
if row.topics:
|
||||
topic_ids = row.topics.split(",")
|
||||
topic_list = list(set(topic_ids))
|
||||
for item in topic_ids:
|
||||
if str(item) in dict_topic:
|
||||
topics.append(
|
||||
{
|
||||
"id": dict_topic[str(item)]["id"],
|
||||
"title": normalYehKe(dict_topic[str(item)]["title"]),
|
||||
}
|
||||
)
|
||||
|
||||
ts_ref = normalYehKe(row.sub_type)
|
||||
ts_ref = ts_ref.replace("رای وحدت رویه", "")
|
||||
ts_ref = ts_ref.replace("رای", "")
|
||||
ts_ref = ts_ref.replace("نظر", "")
|
||||
ts_ref = ts_ref.replace("- اصول 85 و 138", "")
|
||||
ts_ref = ts_ref.strip()
|
||||
|
||||
sort_date_timestamp = -15000000000 # 9/2/1494, 12:45:44 AM # خطا یا خالی
|
||||
try:
|
||||
sort_date_timestamp = getSortTimestamp(ts_date)
|
||||
except Exception as e:
|
||||
sort_date_timestamp = -15000000000 # 9/2/1494, 12:45:44 AM # خطا یا خالی
|
||||
|
||||
sub_type = normalYehKe(row.sub_type)
|
||||
title_popular = normalYehKe(row.title_popular)
|
||||
eblagh_number = normalYehKe(row.eblagh_number)
|
||||
eblagh_date = normalYehKe(row.eblagh_date)
|
||||
|
||||
# print(len(text.split()))
|
||||
# normalized_section_content, recognized_dates, recognized_numbers = normalizerDate2(text.strip())
|
||||
# print(len(normalized_section_content.split()))
|
||||
# recognized_numbers = find_number_indexes_in_string(normalized_section_content,recognized_numbers)
|
||||
# normalized_section_content = normalized_section_content.strip()
|
||||
# founded_patterns_array = regex_patterns_finder(normalized_section_content)
|
||||
|
||||
data = {
|
||||
"id": eid,
|
||||
"title": title,
|
||||
"ref_id": row.ref_id,
|
||||
"ref_key": "m_qavanin",
|
||||
"main_type": main_type,
|
||||
"sub_type": sub_type,
|
||||
"html": row.content,
|
||||
"content": text,
|
||||
# "version_info": version_ifo,
|
||||
"ts_date": ts_date,
|
||||
"ts_year": ts_year,
|
||||
"ts_ref": ts_ref,
|
||||
"qanon_id": qanon_id,
|
||||
"eblagh_number": eblagh_number,
|
||||
"eblagh_date": eblagh_date,
|
||||
"content_len": content_len,
|
||||
"title_popular": title_popular,
|
||||
"sort_date_timestamp": sort_date_timestamp,
|
||||
}
|
||||
|
||||
if len(bulk_data) >= 100:
|
||||
total_count += len(bulk_data)
|
||||
print('=' * 10, 'opinion_to_elastic , 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 != 200 :
|
||||
print(response.text)
|
||||
bulk_data = []
|
||||
break
|
||||
|
||||
bulk_data = pops_data
|
||||
# print(response)
|
||||
# print(response.text)
|
||||
# print(row.txt)
|
||||
|
||||
bulk_data.append(data)
|
||||
|
||||
|
||||
if len(bulk_data) > 0:
|
||||
total_count += len(bulk_data)
|
||||
print('=' * 10, ' opinion_to_elastic , total='+str(total_count) )
|
||||
payload = json.dumps(bulk_data, cls=JSONEncoder) #Works!
|
||||
response = requests.request("POST", url, headers=headers, data=payload)
|
||||
|
||||
print("end success import_opinion_to_elastic")
|
||||
t2 = datetime.datetime.now()
|
||||
print(t2 - t1)
|
||||
Reference in New Issue
Block a user