FitnessDepot Inventory Alert

Due to the current epidemic, gym equipment has become scarce. I found myself continuously checking stock at FitnessDepot and decided to automate the process. After scouring the network logs on their product page I found the API endpoint for their inventory check. It requires 3 parameters, a store_id, sku, and price. The store_id seems to be for locale reasons and depending on the value will return your results in English or French. SKU is obvious, but price not so much. It seems that any value for price will return results with no discernible difference. The response is a partial CSS/HTML markup text so I used PHP with it's built-in DOMDocument class to load the HTML and traverse to the information I needed. I set a cron job to run this script every 30 minutes and email me if any of the products I want are in stock.

Notes:

  • Some store inventory is not up to date, looking at you Oakville, so I chose to ignore that node as well as the "Call or Email to Order" node.
  • You will need a functioning mail service on your server for this to work.
*/30 * * * * php -f /path_to_scripts/fitnessdepot_inventory_alert.php
<?php

$sku = 'PBSEXP50';
$url = 'https://www.fitnessdepot.ca/WebService/store-inventory.php?store_id=6&price=549.88&sku=' . $sku;
// List of nodes I don't want to check, some store inventory is not up to date
$ignoreNodes = [
	'Oakville',
	'Call or Email to Order1-877-776-8547ecommerce@fitnessdepot.ca'
];
$foundNodes = [];
$inStock = false;

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
curl_close($ch);

if ($response === false) {
	// report an error
	exit(1);
}

$doc = new DOMDocument();
@$doc->loadHTML($response); //suppress warnings due to invalid HTML

$trs = $doc->getElementsByTagName('tr');
foreach ($trs as $tr) {
	$tds = $tr->getElementsByTagName('td');

	if (!in_array(@$tds[0]->nodeValue, $ignoreNodes) && @$tds[1]->nodeValue == "In Stock") {
		$inStock = true;
		$foundNodes[] = $tds[0]->nodeValue;
	}
}

if ($inStock) {
	$message = "SKU: {$sku}\r\nFound inventory at the following locations: " . implode(', ', $foundNodes) . "\r\n";
	$ret = mail(getenv('PERSONAL_EMAIL'), 'FitnessDepot Inventory', $message);
}

exit;

Source: GitHub Gist - Track FitnessDepot inventory by SKU