-
Notifications
You must be signed in to change notification settings - Fork 0
/
tftd.py
46 lines (40 loc) · 1.3 KB
/
tftd.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import requests
from bs4 import BeautifulSoup
from random import randrange
def get_strings_from_urls(urls):
strings = []
ignore = [
'Thought for the day',
'Letter',
'Source'
]
for url in urls:
page = requests.get(url)
soup = BeautifulSoup(page.content, 'html.parser')
results = soup.find_all('td', align='center')
for r in results:
cell = r.find('b')
if cell is not None:
text = cell.get_text()
if len(text) > 2 and not any(i in text for i in ignore):
strings.append(text)
return strings
def get_random_str_from_list(list):
max = len(list) - 1
i = randrange(0, max)
return list[i]
def fix_punctuation(string):
punc = ['.', '?', '!']
last_char = string[-1]
if last_char not in punc:
string = string + '.'
return string
if __name__ == '__main__':
strings = get_strings_from_urls([
'https://wh40k.lexicanum.com/wiki/Thought_for_the_day_(A_-_H)',
'https://wh40k.lexicanum.com/wiki/Thought_for_the_day_(I_-_P)',
'https://wh40k.lexicanum.com/wiki/Thought_for_the_day_(Q_-_Z)'
])
random_string = get_random_str_from_list(strings)
formatted_string = fix_punctuation(random_string)
print(formatted_string)