From a823be2281e5dc106ee82e407215b5fe6daeb0b6 Mon Sep 17 00:00:00 2001 From: Roboron3042 Date: Wed, 20 Sep 2023 23:36:18 +0200 Subject: [PATCH] efemeridesbot v1 --- efemeridesbot.py | 68 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 efemeridesbot.py diff --git a/efemeridesbot.py b/efemeridesbot.py new file mode 100644 index 0000000..c96e096 --- /dev/null +++ b/efemeridesbot.py @@ -0,0 +1,68 @@ +import requests +from bs4 import BeautifulSoup +from common import get_api +from datetime import datetime +from random import choice +from locale import setlocale, LC_ALL +from collections import Counter + +def append_random_text(message, texts): + _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() +day = today.strftime('%d') +month = today.strftime('%B') + +r = requests.get('https://www.hoyenlahistoria.com/efemerides/' + month + '/' + day) +html = BeautifulSoup(r.text, "html.parser") +basic_events = html.find_all('li', class_='event') +featured_events = html.find_all('p') +events = basic_events + featured_events[1 : -1] + +texts1000 = [] +texts1450 = [] +texts1800 = [] +texts1900 = [] +texts1950 = [] +texts2000 = [] +texts2050 = [] + +for event in events: + text = event.get_text() + year = int(text.split(" ")[0]) + if year <= 1000: + texts1000.append(text) + elif year <= 1450: + texts1450.append(text) + 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.status_post(message)