mastoes-bots/efemeridesbot.py

44 lines
1.3 KiB
Python
Raw Normal View History

2023-09-20 21:36:18 +00:00
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
2023-09-22 19:09:21 +00:00
from time import sleep
2023-09-20 21:36:18 +00:00
2023-09-22 19:09:21 +00:00
setlocale(LC_ALL, 'es_ES.UTF-8')
2023-09-20 21:36:18 +00:00
today = datetime.today()
day = today.strftime('%d')
month = today.strftime('%B')
2023-09-22 19:09:21 +00:00
r = requests.get('https://es.m.wikipedia.org/wiki/' + day + '_de_' + month)
2023-09-20 21:36:18 +00:00
html = BeautifulSoup(r.text, "html.parser")
2023-09-22 19:09:21 +00:00
events = html.find_all('section')[1].find_all('li')
2023-09-20 21:36:18 +00:00
2023-09-22 19:09:21 +00:00
years = [1000,1500,1800,1850,1900,1935,1947,1960,1970,1980,1990,2000,2010,2050]
texts = {i: [] for i in years }
2023-09-20 21:36:18 +00:00
for event in events:
text = event.get_text()
2023-09-22 19:09:21 +00:00
text_year = int(text.split(":")[0].split(u'\xa0')[0].split(" ")[0].split(",")[0])
for year in years:
if text_year < year:
texts[year].append(text)
break
2023-09-20 21:36:18 +00:00
api = get_api('masto.es', 'efemeridesbot')
2023-09-22 19:09:21 +00:00
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)