Last active
September 20, 2018 17:42
-
-
Save edisonywh/f25dce6e0dfa6e737cb1b98cd8012077 to your computer and use it in GitHub Desktop.
Pings `ozbargain.com.au` and send a notification if a keyword matches
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.