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 re import datetime from config_base import * from general_functions import * ''' # این متد یک متن و ایندکس آغاز و پایان یک عبارت درون آن متن را دریافت می کند # و شماره توکن آغازین و توکن پایانی مربوط به عبارت در متن را بر می گرداند def token_state_finder(normalized_section_content, start_index, end_index): before_substring = normalized_section_content[0:start_index].strip() pattern_substring = normalized_section_content[start_index:end_index].strip() before_substring_token_list = before_substring.strip().split() pattern_token_list = pattern_substring.strip().split() start_token_state = len(before_substring_token_list) + 1 end_token_state = len(before_substring_token_list) + (len(pattern_token_list)) pattern_tokens_state = [ {"start_token_state": start_token_state, "end_token_state": end_token_state} ] return pattern_tokens_state def find_number_indexes_in_string(normalized_string,recognized_numbers): complete_recognized_numbers = [] for item in recognized_numbers: number_start_index, number_end_index = find_token_indexes_in_string(normalized_string,item['start_token_index'],item['end_token_index']) complete_recognized_numbers.append( { 'number_value' : item['number_value'], 'number_token_list' : item['number_token_list'], 'start_token_index' : item['start_token_index'], 'end_token_index' : item['end_token_index'], "start_number_state": number_start_index, "end_number_state" : number_end_index } ) return complete_recognized_numbers # این متد متن اصلی یک متن، توکن آغازین و توکن پایانی مربوط به یک عبارت را می گیرد # و ایندکس آغاز و ایندکس پایان متن وارد شده را بر می گرداند def find_token_indexes_in_string(normalized_string,start_token_index,end_token_index): before_tokens = normalized_string.split()[0:start_token_index] content_tokens = normalized_string.split()[start_token_index:end_token_index + 1] content_start_index = 0 content_end_index = 0 # شمردن تعداد کاراکترهای هر توکن در لیست توکن قبل از عدد for token in before_tokens: content_start_index += len(token) # اضافه کردن تعداد فاصله های خالی یا همان اسپیس به عدد ایندکس شروع عدد content_start_index += len(before_tokens) + 1 # شمردن تعداد کاراکترهای هر توکن در لیست توکن مربوط به عدد for token in content_tokens: content_end_index += len(token) # اضافه کردن تعداد فاصله های خالی یا همان اسپیس به عدد ایندکس پایان عدد content_end_index += (content_start_index - 1) + (len(content_tokens) - 1) return content_start_index, content_end_index # این متد، متنی را دریافت می کند و الگوهای تعریف شده را در آن جستجو می کند و آرایه ای از عبارات مطابق با هر الگو، # شماره ایندکس شروع و پایان هر عبارت، عنوان و محتوای الگو، و شماره توکن شروع و توکن پایانی هر عبارت # پیدا شده را بر می گرداند def regex_patterns_finder(sectoin_content): regex_patterns = [ { "asle N asasi": r"اصل\s*شماره\s*(\d+)\s*قانون\s*اساسی\s*جمهوری\s*اسلامی\s*ایران" }, # اصل شماره فلان قانون اساسی جمهوری اسلامی ایران { "qanone asasi": r"(? 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 = normalYehKe(text) content_len = text.__len__() title = normalYehKe(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_year = "" if row.ts_date: items = row.ts_date.split("/") if items.__len__() > 0: ts_year = items[0] 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(row.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": row.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 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 = 1 # print(response.text) # print(row.txt) bulk_data.append(data) bulk_count += 1 if bulk_count > 0: print(bulk_count) payload = json.dumps(bulk_data, cls=JSONEncoder) #Works! response = requests.request("POST", url, headers=headers, data=payload) cursor.close() cnxn.close() print("end success") t2 = datetime.datetime.now() print(t2 - t1)