Skip to content

Instantly share code, notes, and snippets.

@nmattam
Last active December 20, 2017 21:33
Show Gist options
  • Save nmattam/08b4c10caf19ef7bc539b17af21f4cec to your computer and use it in GitHub Desktop.
Save nmattam/08b4c10caf19ef7bc539b17af21f4cec to your computer and use it in GitHub Desktop.
require 'aws-sdk'
require 'net/http'
require 'nokogiri'
require 'sendgrid-ruby'
notify = ENV['notify']
sendgrid_token = ENV['SENDGRID_API_TOKEN']
# set this hash to empty so that google doesn't index these URLs from github.
# Key should be the website name and value is the URL.
movie_urls = {'abc' => 'abc-url', 'xyz' => 'xyz-url'}
# Uses Send Grid to send emails
def send_email(message, receiver, subject, SENDGRID_API_TOKEN)
include SendGrid
from_email = 'movie-finder@nmattam.com'
unless message.nil? && message.empty?
from = Email.new(email: from_email)
to = Email.new(email: receiver)
content = Content.new(type: 'text/html', value: message)
mail = Mail.new(from, subject, to, content)
sg = SendGrid::API.new(api_key: SENDGRID_API_TOKEN)
response = sg.client.mail._('send').post(request_body: mail.to_json)
end
end
def find_movies(movie_urls, notify, SENDGRID_API_TOKEN)
movie_count = {}
bucket_name = 'movie-api'
greeting_message = "Hello, <br><br>"
final_greeting = "<br><br>Have fun!!"
intro_message = "Enjoy: <br>"
movie_list = ''
subject = ''
total_num_movies = 0
movie_urls.each do |website, web_url|
num_movies = 0
latest_movies_file = "./latest_movies_#{website.parameterize.underscore}.txt"
latest_movie = ''
escaped_address = URI.escape(web_url)
uri = URI.parse(escaped_address)
source = Net::HTTP.get(uri)
movies = []
# Find the last latest movies. This file will be my DB to save the latest found movie.
# read from s3
s3 = Aws::S3::Resource.new(
credentials: Aws::Credentials.new('akid', 'secret'),
region: 'us-west-2'
)
bucket = s3.bucket(bucket_name)
latest_movie = bucket.object(latest_movies_file).read if bucket.object(latest_movies_file).exists?
#latest_movie = File.read(latest_movies_file) if File.file?(latest_movies_file)
#Assuming that the movies are listed inside the h2 tags. Surprisingly works for both the websites today.
Nokogiri::HTML(source).css("h2").each do |h2|
movie_title = h2.content.split('(').first.strip
break if movie_title == latest_movie
movies << movie_title
num_movies = num_movies + 1
end
# Update the file with the latest movie
# write to s3
bucket.object(latest_movies_file).write(movies.first) unless num_movies == 0
if num_movies > 0
movie_list = (movie_list || '') +'<br><br><u>'+ website +'</u>'
movie_list += movies.map! { |movie| "<br><b>#{movie}</b>" }.join("")
movie_count = { "#{website}" => "#{num_movies}" }.merge(movie_count)
total_num_movies = total_num_movies + num_movies
end
end
subject = "#{total_num_movies} new movies from #{movie_count.keys.join(", ")}"
if total_num_movies == 0
subject = "No new movies :-("
movie_list = "Might be a good idea to check the websites <br><br> #{movie_urls.values.join('<br>')}."
end
message = intro_message + movie_list + final_greeting
send_email(message, notify, subject, SENDGRID_API_TOKEN)
end
# Call find_movies
find_movies(movie_urls, notify, sendgrid_token)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment