Skip to content

Instantly share code, notes, and snippets.

@edisonywh
Last active September 20, 2018 17:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edisonywh/f25dce6e0dfa6e737cb1b98cd8012077 to your computer and use it in GitHub Desktop.
Save edisonywh/f25dce6e0dfa6e737cb1b98cd8012077 to your computer and use it in GitHub Desktop.
Pings `ozbargain.com.au` and send a notification if a keyword matches
require 'httparty'
require 'pry'
require 'nokogiri'
require 'terminal-notifier'
class FeedService
attr_accessor :url
attr_accessor :keyword
def initialize(url, keyword)
@url = url
@keyword = keyword
end
def call
html = getResponse
parsed = parseHtml(html)
nodes = parsed.css('.n-right').css('.title')
filtered_nodes = nodes.select do |node|
next unless node.text.downcase.include?(keyword.downcase)
title = node.text
link = node.children.last.values.first
TerminalNotifier.notify(title, title: "Night Watch", open: "https://ozbargain.com.au#{link}")
end
TerminalNotifier.notify("There's no match! Try again.", title: "Night Watch") if filtered_nodes.empty?
TerminalNotifier.notify("Any now my watch has ended", title: "Night Watch")
end
private
def getResponse
HTTParty.get(url).body
end
def parseHtml(html)
Nokogiri::HTML(html)
end
def matches_keyword?(text, keyword)
text.downcase.include?(keyword.downcase)
end
end
url = "https://www.ozbargain.com.au/deals"
keyword = "nintendo"
service = FeedService.new(url, keyword)
service.call
@edisonywh
Copy link
Author

I use whenever to schedule my cron job in Ruby language.

What this does is that it pings ozbargain to see if there's a new deal that matches my criteria.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment