update
This commit is contained in:
parent
3c7fd7ae68
commit
2437a4c03f
43
main.py
43
main.py
|
|
@ -1,5 +1,3 @@
|
||||||
# main.py
|
|
||||||
|
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import requests
|
import requests
|
||||||
|
|
@ -7,7 +5,7 @@ from pathlib import Path
|
||||||
from urllib3.exceptions import InsecureRequestWarning
|
from urllib3.exceptions import InsecureRequestWarning
|
||||||
import urllib3
|
import urllib3
|
||||||
|
|
||||||
urllib3.disable_warnings()
|
urllib3.disable_warnings(InsecureRequestWarning)
|
||||||
|
|
||||||
logging.basicConfig(
|
logging.basicConfig(
|
||||||
level=logging.INFO,
|
level=logging.INFO,
|
||||||
|
|
@ -17,9 +15,25 @@ logging.basicConfig(
|
||||||
)
|
)
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
HEADERS = {
|
||||||
|
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36",
|
||||||
|
"Accept": "application/json, text/plain, */*",
|
||||||
|
"Accept-Language": "ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7",
|
||||||
|
"Accept-Encoding": "gzip, deflate, br",
|
||||||
|
"Referer": "https://www.mrsk-1.ru/customers/customer-service/power-outage/",
|
||||||
|
"X-Requested-With": "XMLHttpRequest",
|
||||||
|
"Connection": "keep-alive",
|
||||||
|
"Sec-Fetch-Dest": "empty",
|
||||||
|
"Sec-Fetch-Mode": "cors",
|
||||||
|
"Sec-Fetch-Site": "same-origin",
|
||||||
|
"Pragma": "no-cache",
|
||||||
|
"Cache-Control": "no-cache"
|
||||||
|
}
|
||||||
|
|
||||||
MAIN_URL = 'https://www.mrsk-1.ru/customers/customer-service/power-outage/ajax.php?request='
|
MAIN_URL = 'https://www.mrsk-1.ru/customers/customer-service/power-outage/ajax.php?request='
|
||||||
DISTRICT_CODE = '0ECC8970-2702-4C14-9E4D-956606EFFEE3'
|
DISTRICT_CODE = '0ECC8970-2702-4C14-9E4D-956606EFFEE3'
|
||||||
LOCALITY_CODE = '02E9C019-AB4D-4FA0-928E-D6C0A41DC256'
|
LOCALITY_CODE = '02E9C019-AB4D-4FA0-928E-D6C0A41DC256'
|
||||||
|
|
||||||
URL_PLAN = f'{MAIN_URL}2®ionCode=31&districtCode={DISTRICT_CODE}&locality=Белгород&localityCode={LOCALITY_CODE}'
|
URL_PLAN = f'{MAIN_URL}2®ionCode=31&districtCode={DISTRICT_CODE}&locality=Белгород&localityCode={LOCALITY_CODE}'
|
||||||
URL_VNEREGLAMENT = f'{MAIN_URL}3®ionCode=31&districtCode={DISTRICT_CODE}&locality=Белгород&localityCode={LOCALITY_CODE}'
|
URL_VNEREGLAMENT = f'{MAIN_URL}3®ionCode=31&districtCode={DISTRICT_CODE}&locality=Белгород&localityCode={LOCALITY_CODE}'
|
||||||
URL_AVAR = f'{MAIN_URL}4®ionCode=31&districtCode={DISTRICT_CODE}&locality=Белгород&localityCode={LOCALITY_CODE}&Street=-'
|
URL_AVAR = f'{MAIN_URL}4®ionCode=31&districtCode={DISTRICT_CODE}&locality=Белгород&localityCode={LOCALITY_CODE}&Street=-'
|
||||||
|
|
@ -28,14 +42,6 @@ PLAN_FILE = Path("plan.txt")
|
||||||
VN_FILE = Path("vnereglament.txt")
|
VN_FILE = Path("vnereglament.txt")
|
||||||
AVAR_FILE = Path("avar.txt")
|
AVAR_FILE = Path("avar.txt")
|
||||||
|
|
||||||
HEADERS = {
|
|
||||||
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36",
|
|
||||||
"Accept": "application/json, text/plain, */*",
|
|
||||||
"Accept-Language": "ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7",
|
|
||||||
"Referer": "https://www.mrsk-1.ru/customers/customer-service/power-outage/",
|
|
||||||
"X-Requested-With": "XMLHttpRequest"
|
|
||||||
}
|
|
||||||
|
|
||||||
def clean_street(street: str) -> str:
|
def clean_street(street: str) -> str:
|
||||||
street = street.replace('Белгород г; ', '').replace('\n\n', '\n').strip()
|
street = street.replace('Белгород г; ', '').replace('\n\n', '\n').strip()
|
||||||
lines = [line.strip() for line in street.split('\n') if line.strip()]
|
lines = [line.strip() for line in street.split('\n') if line.strip()]
|
||||||
|
|
@ -67,7 +73,7 @@ def parse_and_save(data, file_path: Path, mode: str):
|
||||||
|
|
||||||
def get_plan():
|
def get_plan():
|
||||||
try:
|
try:
|
||||||
r = requests.get(url=URL_PLAN, headers=HEADERS, verify=False, timeout=15)
|
r = requests.get(URL_PLAN, headers=HEADERS, verify=False, timeout=15)
|
||||||
r.raise_for_status()
|
r.raise_for_status()
|
||||||
data = r.json()
|
data = r.json()
|
||||||
parse_and_save(data, PLAN_FILE, 'plan')
|
parse_and_save(data, PLAN_FILE, 'plan')
|
||||||
|
|
@ -75,12 +81,13 @@ def get_plan():
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Ошибка при получении плановых отключений: {e}")
|
logger.error(f"Ошибка при получении плановых отключений: {e}")
|
||||||
if 'r' in locals():
|
if 'r' in locals():
|
||||||
logger.error(f"Статус: {r.status_code}, Ответ: {r.text[:300]}")
|
logger.error(f"Статус: {r.status_code} | URL: {r.url}")
|
||||||
|
logger.error(f"Ответ: {r.text[:500]}")
|
||||||
PLAN_FILE.write_text("[Ошибка загрузки данных]", encoding='utf-8')
|
PLAN_FILE.write_text("[Ошибка загрузки данных]", encoding='utf-8')
|
||||||
|
|
||||||
def get_vnereglament():
|
def get_vnereglament():
|
||||||
try:
|
try:
|
||||||
r = requests.get(url=URL_VNEREGLAMENT, headers=HEADERS, verify=False, timeout=15)
|
r = requests.get(URL_VNEREGLAMENT, headers=HEADERS, verify=False, timeout=15)
|
||||||
r.raise_for_status()
|
r.raise_for_status()
|
||||||
data = r.json()
|
data = r.json()
|
||||||
parse_and_save(data, VN_FILE, 'vnereglament')
|
parse_and_save(data, VN_FILE, 'vnereglament')
|
||||||
|
|
@ -88,12 +95,13 @@ def get_vnereglament():
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Ошибка при получении внерегламентных отключений: {e}")
|
logger.error(f"Ошибка при получении внерегламентных отключений: {e}")
|
||||||
if 'r' in locals():
|
if 'r' in locals():
|
||||||
logger.error(f"Статус: {r.status_code}, Ответ: {r.text[:300]}")
|
logger.error(f"Статус: {r.status_code} | URL: {r.url}")
|
||||||
|
logger.error(f"Ответ: {r.text[:500]}")
|
||||||
VN_FILE.write_text("[Ошибка загрузки данных]", encoding='utf-8')
|
VN_FILE.write_text("[Ошибка загрузки данных]", encoding='utf-8')
|
||||||
|
|
||||||
def get_avar():
|
def get_avar():
|
||||||
try:
|
try:
|
||||||
r = requests.get(url=URL_AVAR, headers=HEADERS, verify=False, timeout=15)
|
r = requests.get(URL_AVAR, headers=HEADERS, verify=False, timeout=15)
|
||||||
r.raise_for_status()
|
r.raise_for_status()
|
||||||
data = r.json()
|
data = r.json()
|
||||||
parse_and_save(data, AVAR_FILE, 'avar')
|
parse_and_save(data, AVAR_FILE, 'avar')
|
||||||
|
|
@ -101,7 +109,8 @@ def get_avar():
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Ошибка при получении аварийных отключений: {e}")
|
logger.error(f"Ошибка при получении аварийных отключений: {e}")
|
||||||
if 'r' in locals():
|
if 'r' in locals():
|
||||||
logger.error(f"Статус: {r.status_code}, Ответ: {r.text[:300]}")
|
logger.error(f"Статус: {r.status_code} | URL: {r.url}")
|
||||||
|
logger.error(f"Ответ: {r.text[:500]}")
|
||||||
AVAR_FILE.write_text("[Ошибка загрузки данных]", encoding='utf-8')
|
AVAR_FILE.write_text("[Ошибка загрузки данных]", encoding='utf-8')
|
||||||
|
|
||||||
def start_parser():
|
def start_parser():
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user