jasonblum
1/2/2018 - 2:23 PM

Email me today's movies at the AFI

Scrapes the AFI website and sends email with today's shows.

#!/usr/bin/python3.6

import requests

from bs4 import BeautifulSoup

#Variables
afi_website_url  = 'http://www.afi.com/silver/films/calendar.aspx'
mail_sender_service = 'https://api.mailgun.net/v3/sandboxec1e86e70f9641de94f341d840592733.mailgun.org/messages'
mail_sender_service_key = 'key-XXXXXXXXXXXXXXX'
sender = 'Jason <test@test.com>'
recipients = ['test@test.com',]

#Go get the web page
response = requests.get(afi_website_url)

#text comes back ...turn it back into HTML
soup = BeautifulSoup(response.text, 'lxml')

#Get the movies in the today's div
movies = soup.select('.today a')

#The email I'm going to send
email = ''

#This div only contains links to the movies, but I want their descriptions, so loop over each movie
for movie in movies:
	#...and get it's URL
	url = movie['href'].replace('/silver/films/', '')
	#...now go get it
	response = requests.get(url)
	#text comes back ...turn it back into HTML
	soup = BeautifulSoup(response.text, 'lxml')
	#Pick out it's title and description and add it to our email
	title = soup.select('.film-info .boxout-title')[0]
	description = soup.select('.film-info .boxout-blurb')[0]
	email += "<a href='{}'>{}</a>{}<br/>".format(url, title, description)

#Now that the email is ready, send it!
response = requests.post(
	mail_sender_service,
	auth=('api', mail_sender_service_key),
	data={
		'from': sender,
		'to': recipients,
		'subject': 'Today\'s movies at the AFI are...',
		'html': email
	}
)

print('All Done!')