I made a bot that tells you today’s fortune based on your horoscope.
import requests from bs4 import BeautifulSoup def get_horoscope(s): signs = ["ARIES", "TAURUS", "GEMINI", "CANCER", "LEO", "VIRGO", "LIBRA", "SCORPIO", "SAGITTARIUS", "CAPRICORN", "AQUARIUS", "PISCES"] sign = signs.index(s) + 1 url = 'https://www.horoscope.com/us/horoscopes/general/horoscope-general-daily-today.aspx?sign=' + str(sign) html = requests.get(url).text soup = BeautifulSoup(html, 'html.parser') f = soup.select('.horoscope-content p').text return f
import json from envelopes import Envelope from fortune import get_horoscope import sys with open('config.json', 'r') as infile: conf = json.load(infile) ipt = sys.argv.upper() text = get_horoscope(ipt) html = "<div style='background-color:purple;color:yellow;font-size:20px'>" + txt + "</div>" message = Envelope( from_addr=("email@example.com", "Fortune Teller"), to_addr=("firstname.lastname@example.org", "Yeonhee"), subject="Today's Horoscope", html_body=html ) message.send('smtp.googlemail.com', login=conf['myEmail'], password=conf['myPassword'], tls='true')
In terminal, you need to type your horoscope as well like below:
python horoscopeBot.py sagittarius
At first, fortune.py worked while horoscopeBot.py didn’t. It gave me an SMTP Gmail authentification error.
Now I’ve fixed this problem by turning on the ‘”Allow less secure apps’ option in my Gmail account’s security setting. (I referred the first answer in this Stack Overflow question and it worked for me!)
When I ran horoscopeBot.py with the command above, I got an email like following:
The fortune result comes from www.horoscope.com