replace-bot/parser/parser.py

60 lines
1.2 KiB
Python
Raw Normal View History

2022-02-16 18:13:44 +03:00
import requests
import json
import datetime
from datetime import datetime as dt
from bs4 import BeautifulSoup
try:
from load import config
except: config = None
from .utils import *
headers = {
'user-agent':(
"Mozilla/5.0 (Windows NT 10.0; WOW64) "
"AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/62.0.3202.9 Safari/537.36"
)
}
def date_parser_helper(days:int, parse:str="%d.%m.20%y"):
return dt.strftime(
dt.now() +
datetime.timedelta(days=days),
parse
)
def docs_parse():
output = {
"data":{},
"another_teacher":None
}
page = requests.get(config.bot("link"), headers=headers)
page.encoding = 'utf-8'
2022-02-22 15:29:44 +03:00
soup = BeautifulSoup(page.text, "lxml")
2022-02-16 18:13:44 +03:00
# Это в идеале нужно переписать...
try: output = table_parser(soup, output); #print(output)
except Exception: pass
2022-02-22 15:29:44 +03:00
try: output = test_parser(soup, output)
except Exception as e: raise(e)
2022-02-22 15:29:44 +03:00
2022-02-16 18:13:44 +03:00
with open(config.data_file, 'w') as f:
json.dump(output, f, ensure_ascii=False)
f.close()
def get_about_replacements() -> dict:
with open(config.data_file, 'r') as f:
data = json.loads(f.read())
f.close()
return data