[DTW] Week09

I made a bot that tells you today’s fortune based on your horoscope.

[fortune.py]

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')[0].text
  return f

[horoscopeBot.py]

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[1].upper()
text = get_horoscope(ipt)
html = "<div style='background-color:purple;color:yellow;font-size:20px'>" + txt + "</div>"

message = Envelope(
  from_addr=("dtw.yeonhee@gmail.com", "Fortune Teller"),
  to_addr=("signup.yeonhee.lee@gmail.com", "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

Leave a Reply

Your email address will not be published. Required fields are marked *