Skip to content

Instantly share code, notes, and snippets.

@jeremygibbs
Last active January 12, 2016 03:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeremygibbs/1d62544ee57dc4ee8633 to your computer and use it in GitHub Desktop.
Save jeremygibbs/1d62544ee57dc4ee8633 to your computer and use it in GitHub Desktop.
Script to check Apple.com for Apple pencil in-store availability
<?php
############################################################
# script to check Apple for Apple Pencil availability #
# sku number: MK0C2AM/A #
# by: Jeremy A. Gibbs #
# https://gist.github.com/jeremygibbs/1d62544ee57dc4ee8633 #
############################################################
error_reporting(E_ALL);
ini_set('display_errors', 'On');
//-- Search for a given zip code --//
$json = file_get_contents("http://store.apple.com/us/retail/availabilitySearch?parts.0=MK0C2AM/A&zip=<YOUR_ZIP_CODE>");
$data = json_decode($json);
$stores = $data->body->stores;
$pencil_info = Array();
foreach ($stores as $store) {
$pencil_info[] = array(
"store_name" => $store->storeName,
"store_dist" => $store->storedistance,
"store_stat" => $store->partsAvailability->{'MK0C2AM/A'}->pickupDisplay
);
}
//-- Filter only those stores with Apple Pencil availability --//
$filter_array = array(
'store_stat' => "available"
);
$filtered_array = array_filter($pencil_info, function ($val_array) use ($filter_array) {
$intersection = array_intersect_assoc($val_array, $filter_array);
return (count($intersection)) === count($filter_array);
});
$stores = $filtered_array;
$instore = count($stores)>0 ? true : false;
//-- If available in-store within the search area, send an email alert --//
if ($instore) {
$to = '<YOUR@EMAIL.COM>';
$subject = 'Pencil available near <LOCATION>';
$message = "";
foreach($stores as $store) {
$name = $store["store_name"];
$dist = $store["store_dist"];
$message .= "$name, $dist miles from <LOCATION>\n\n";
}
$message .= "http://www.apple.com/shop/product/MK0C2/apple-pencil-for-ipad-pro";
mail($to, $subject, $message, null, '-f<YOUR@FROM_EMAIL.COM>');
}
//-- Write a simple html table (either for web viewing or in Panic's StatusBoard app) --//
$html = '
<html>
<style>
.name {
width: 50%;
font-size: 22px;
}
.dist {
width: 25%;
text-align: center;
font-size: 22px;
}
.stat {
width: 25%;
text-align: center;
font-size: 22px;
}
</style>
<table data-refresh-every-n-seconds="300">';
if ($instore) {
$html .='
<tr>
<th class="name">Store</th>
<th class="dist">Distance</th>
<th class="stat">Availability</th>
</tr>';
} else {
$html .='
<tr>
<th class="name">Apple Pencil not available</th>
</tr>';
}
if ($instore) {
foreach($stores as $store) {
$html .='
<tr>
<td class="name">'. $store["store_name"] .'</td>
<td class="dist">'. $store["store_dist"] .'</td>
<td class="stat">'. $store["store_stat"] .'</td>
</tr>';
}
}
$html .= '</table></html>';
$output = fopen("/PATH/TO/YOUR/WEB/FILES/status_apple.html", "w");
fwrite($output, $html);
fclose($output);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment