Efemeridesbot v2

This commit is contained in:
Roboron3042 2023-09-22 21:09:21 +02:00
parent a823be2281
commit 44e133ece8
1 changed files with 23 additions and 48 deletions

View File

@ -5,64 +5,39 @@ from datetime import datetime
from random import choice from random import choice
from locale import setlocale, LC_ALL from locale import setlocale, LC_ALL
from collections import Counter from collections import Counter
from time import sleep
def append_random_text(message, texts): setlocale(LC_ALL, 'es_ES.UTF-8')
_message = message
_message += "\n\n- " + choice(texts)
if len(_message) < 1000:
return _message
return message
setlocale(LC_ALL, 'es_ES.UTF-8º')
today = datetime.today() today = datetime.today()
day = today.strftime('%d') day = today.strftime('%d')
month = today.strftime('%B') month = today.strftime('%B')
r = requests.get('https://www.hoyenlahistoria.com/efemerides/' + month + '/' + day) r = requests.get('https://es.m.wikipedia.org/wiki/' + day + '_de_' + month)
html = BeautifulSoup(r.text, "html.parser") html = BeautifulSoup(r.text, "html.parser")
basic_events = html.find_all('li', class_='event') events = html.find_all('section')[1].find_all('li')
featured_events = html.find_all('p')
events = basic_events + featured_events[1 : -1]
texts1000 = [] years = [1000,1500,1800,1850,1900,1935,1947,1960,1970,1980,1990,2000,2010,2050]
texts1450 = [] texts = {i: [] for i in years }
texts1800 = []
texts1900 = []
texts1950 = []
texts2000 = []
texts2050 = []
for event in events: for event in events:
text = event.get_text() text = event.get_text()
year = int(text.split(" ")[0]) text_year = int(text.split(":")[0].split(u'\xa0')[0].split(" ")[0].split(",")[0])
if year <= 1000: for year in years:
texts1000.append(text) if text_year < year:
elif year <= 1450: texts[year].append(text)
texts1450.append(text) break
elif year <= 1800:
texts1800.append(text)
elif year <= 1900:
texts1900.append(text)
elif year <= 1950:
texts1950.append(text)
elif year <= 2000:
texts2000.append(text)
elif year <= 2050:
texts2050.append(text)
message = "Tal día como hoy, en el año:"
message = append_random_text(message, texts1000)
message = append_random_text(message, texts1450)
message = append_random_text(message, texts1800)
message = append_random_text(message, texts1900)
message = append_random_text(message, texts1950)
message = append_random_text(message, texts2000)
message = append_random_text(message, texts2050)
print(message)
print(len(message))
api = get_api('masto.es', 'efemeridesbot') api = get_api('masto.es', 'efemeridesbot')
api.status_post(message) last_id = 0
for year in years:
if len(texts[year]):
message = day + " de " + month + " de " + choice(texts[year]) + " #Efemérides"
if last_id:
status = api.status_post(message, in_reply_to_id=last_id)
last_id = status['id']
else:
status = api.status_post(message)
last_id = status['id']
sleep(60 * 60)