Skip to content

Instantly share code, notes, and snippets.

@lozzd
Created February 22, 2011 10:37
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 lozzd/838484 to your computer and use it in GitHub Desktop.
Save lozzd/838484 to your computer and use it in GitHub Desktop.
Checks the Nescafe Dolce Gusto website to see if a certain product is in stock
<?
#
# Checks the Nescafe Dolce Gusto website to see if your pods are back in stock
# by Laurie Denness
#
#
# This program sends an email to THIS ADDRESS:
$email = "your.email@here.com";
# When your chosen coffee pod (specify on the command line) comes back into stock.
# e.g. php coffeeinstock.php 7613032349523_08
# Some colours
$red = "\033[31m";
$white = "\033[0m";
$yellow = "\033[1;33m";
$blue = "\033[34m";
$green = "\033[32m";
$productid = $argv[1];
if (!$productid) {
logline("{$red}ERR: {$white}A product ID is required to check, get this from the URL from the DG website");
logline("E.g.: php coffeeinstock.php 7613032349523_08");
exit(2);
}
$url = "https://www.dolce-gusto.co.uk/EN/shop/Pages/flavours.aspx?productId={$productid}";
while (true) {
# go into a loop. The program exits and sends an email when it's in stock, otherwise
# it tries and tries again.
if (false == ($html = file_get_contents($url))) {
# this HTTP GET failed.
logline("{$red}ERR: {$white}This fetch failed. Retrying.");
} else {
# search to make sure the product actually exists
if (strpos($html,"Error")) {
logline("{$yellow}WARN: {$white}An error occured. Does that product exist?");
break;
}
# search for "Out of Stock"
if (stripos($html, "Out of Stock") === false) {
logline("{$green}YAY: {$white}Product is in stock!");
logline("{$blue}INFO: {$white}Sending email to {$email}");
# send an email, rejoice, etc
if (sendmail("Great news, your coffee pods product {$productid} are now in stock!", $email, "Your coffee pods are in stock!")) {
logline("{$green}OK: {$white} Email sent! Goodbye.");
} else {
logline("{$red}ERR: {$white} Email sending failed.");
}
exit(0);
} else {
logline("{$yellow}WARN: {$white}Product is not in stock. Retrying.");
}
}
logline("{$blue}INFO: {$white}Sleeping 1800 seconds...");
sleep(1800);
}
function logline($message) {
echo date(DATE_RFC822) . " " . $message . "\n";
}
function sendmail($message, $emailaddr, $subject) {
$headers = "From: " . $email . "\r\n";
$headers .= 'Reply-To: ' . $emailaddr ."\r\n";
$headers .= "X-Mailer: PHPCoffeeScript\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
if (mail($emailaddr, $subject, $message, $headers)) {
return true;
} else {
return false;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment