import gtts
from playsound2 import playsound
from bs4 import BeautifulSoup
import requests
#GetFacts
page = requests.get("http://www.djtech.net/humor/useless_facts.htm", timeout=1)
soup = BeautifulSoup(page.text, 'html.parser')
souptable = soup.find("table", {"id": "table269"})
for loop in souptable.findAll('p'):
try:
TextEnd = ""
TextEndComplete = ""
Fact = loop.text.strip()
Fact = Fact.strip()
CompleteText = Fact
CompleteText = CompleteText.replace('\n', '').replace('\r', '').replace(' ', '')
if len(CompleteText) > 50:
CompleteText = CompleteText.split(" ")
for i in CompleteText:
if len(TextEnd) < 50:
TextEnd = TextEnd + i + " "
else:
TextEndComplete = TextEndComplete + TextEnd + i + "\n"
TextEnd = ""
if TextEnd != "":
TextEndComplete = TextEndComplete + TextEnd
TextEnd = ""
print(TextEndComplete)
print("=============")
tts = gtts.gTTS(CompleteText)
tts.save("FactVideo1.mp3")
playsound("FactVideo1.mp3")
except Exception as E:
print(E)