diff --git a/efemeridesbot.py b/efemeridesbot.py index c96e096..f2d5bd5 100644 --- a/efemeridesbot.py +++ b/efemeridesbot.py @@ -5,64 +5,39 @@ from datetime import datetime from random import choice from locale import setlocale, LC_ALL from collections import Counter +from time import sleep -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º') +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) +r = requests.get('https://es.m.wikipedia.org/wiki/' + day + '_de_' + month) 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] +events = html.find_all('section')[1].find_all('li') -texts1000 = [] -texts1450 = [] -texts1800 = [] -texts1900 = [] -texts1950 = [] -texts2000 = [] -texts2050 = [] +years = [1000,1500,1800,1850,1900,1935,1947,1960,1970,1980,1990,2000,2010,2050] +texts = {i: [] for i in years } 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)) + 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 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) +