replace-bot/parser/parser.py

65 lines
1.4 KiB
Python
Raw Normal View History

2022-10-07 17:50:42 +03:00
import base64
2022-02-16 18:13:44 +03:00
import json
import datetime
from datetime import datetime as dt
2022-10-07 17:50:42 +03:00
import requests
2022-02-16 18:13:44 +03:00
from bs4 import BeautifulSoup
try:
from load import config
2022-10-07 17:50:42 +03:00
except ImportError: config = None
try:
from .utils import *
except ImportError:
from utils import *
2022-02-16 18:13:44 +03:00
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
# Это в идеале нужно переписать...
2022-10-07 17:50:42 +03:00
url = image_parser(soup)
with requests.get(url=url, allow_redirects=True, stream=True) as r:
output['image'] = True
output['date'] = 'невозможно получить!'
output['data']['all'] = base64.b64encode(r.content).decode('utf-8')
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
2022-10-07 17:50:42 +03:00
docs_parse()