Skip to content

Instantly share code, notes, and snippets.

@cogwirrel
Last active January 2, 2016 09:59
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 cogwirrel/8286566 to your computer and use it in GitHub Desktop.
Save cogwirrel/8286566 to your computer and use it in GitHub Desktop.
Check whether certain flavours of a product are in stock on monstersupplements.com, and email you if they are!
require "net/http"
require "uri"
require "net/smtp"
product = "C4 Extreme"
url = "http://monstersupplements.com/store/p/5548/1/Cellucor-C4-Extreme-60-servings.html"
flavours = ["Fruit Punch", "Orange", "Blue Raspberry", "Green Apple", "Watermelon"]
wantedFlavours = ["Orange", "Watermelon"]
emails = ["your@email.com", "another@email.com"]
def sendEmail(tos, subject, body)
tos.each do |to|
command = "echo \"#{body}\" | mail -s \"#{subject}\" #{to}"
system command
end
end
responseBody = Net::HTTP.get_response(URI.parse(url)).body
outOfStock = flavours.select do |flavour|
isOutOfStock = false
responseBody.each_line do |line|
isOutOfStock ||= line.include?("label") && line.include?(flavour) && line.include?("Out of Stock")
end
isOutOfStock
end
inStock = flavours - outOfStock
wantedInStock = inStock & wantedFlavours
if wantedInStock.length > 0
subject = "#{product} In Stock!!"
body = "The flavour(s) in stock: #{wantedInStock.inspect}"
sendEmail(emails, subject, body)
puts "#{wantedInStock.inspect} in stock!!!"
exit 0
else
puts "Out of stock of wanted flavours :("
exit 1
end
@cogwirrel
Copy link
Author

monster.rb

Parameters

Parameters are defined at the top as follows:

product: Name of the product you're polling at monster supplements (can be anything, just used in the email)
url: The url of the product on Monster Supplements
flavours: A list of all of the flavours of a product available (must be exactly as written on monster supplements page)
wantedFlavours: A list of the flavours you want to check for (must be a subset of flavours)
emails: The email addresses to send an email to if the product is available in one or more of the wanted flavours.

Requirements

Requires mail unix command all set up.

Suggested Use

Use it to poll Monster Supplements to notify you when a certain product you're waiting for becomes available with a bash script like:

#!/bin/bash

CODE=1
while [ $CODE -eq 1 ]; do
    date
    ruby monster.rb
    CODE=$?
    sleep `shuf -i 300-500 -n 1`
done

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