diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..53b061a --- /dev/null +++ b/.editorconfig @@ -0,0 +1,9 @@ +root = true + +[*] +indent_style = space +indent_size = 2 +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true \ No newline at end of file diff --git a/Gemfile b/Gemfile index 6ab45dc..3e44470 100644 --- a/Gemfile +++ b/Gemfile @@ -8,3 +8,4 @@ ruby "2.0.0" gem "scraperwiki", git: "https://github.com/openaustralia/scraperwiki-ruby.git", branch: "morph_defaults" gem "mechanize" +gem "ruby-gmail" diff --git a/scraper.rb b/scraper.rb index 2b2348e..83edc16 100644 --- a/scraper.rb +++ b/scraper.rb @@ -3,16 +3,48 @@ require 'scraperwiki' require 'mechanize' +require 'gmail' -agent = Mechanize.new +class MovistarWebScraper + def initialize + @agent = Mechanize.new + end -# Read in a page -page = agent.get('http://www.tiendamovistar.com.ar/product/MOTO-G-4G-LTE,3195,245.aspx') + def check_stock + page = agent.get('http://www.tiendamovistar.com.ar/product/MOTO-G-4G-LTE,3195,245.aspx') + buy_button = page.at('input[name="ctl00$MainContent$ProductInfo1$ctl02$btnBuyPlan"]') -# Find somehing on the page using css selectors -buy_button = page.at('input[name="ctl00$MainContent$ProductInfo1$ctl02$btnBuyPlan"]') + {timestamp: DateTime.now, has_stock: buy_button['disabled'] != 'disabled'} + end +end -has_stock = buy_button['disabled'] != 'disabled' +class MorphNotifier + def notify(result) + ScraperWiki.save_sqlite(['timestamp'], result) + end +end -# Write out to the sqlite database using scraperwiki library -ScraperWiki.save_sqlite(['timestamp'], { 'timestamp' => DateTime.now, 'has_stock' => has_stock.to_s }) \ No newline at end of file +class GmailNotifier + def notify(result) + Gmail.new(username, password) do |gmail| + send_mail gmail, result + end + end + + def send_mail(gmail, result) + gmail.deliver do + to "email@example.com" + subject "Having fun in Puerto Rico!" + text_part do + body "Text of plaintext message." + end + end + end +end + +result = MovistarWebScraper.new.check_stock +config = {from: ENV['MORPH_MAIL_FROM'], to: ENV['MORPH_MAIL_TO'], password: ENV['MORPH_PASSWORD']} + +[MorphNotifier.new, GmailNotifier.new config].do |notifier| + notifier.notify result +end