Skip to content

Instantly share code, notes, and snippets.

@eyecatchup
Last active December 25, 2015 17:59
Show Gist options
  • Save eyecatchup/7017108 to your computer and use it in GitHub Desktop.
Save eyecatchup/7017108 to your computer and use it in GitHub Desktop.
Automated Android 4.4 Kitkat / Nexus 5 / Factory Image Release email alert.
<?php
ini_set('max_execution_time', 600);
if (!ini_get('date.timezone') && function_exists('date_default_timezone_set')) {
date_default_timezone_set('UTC'); // You may change this (eg 'Europe/Berlin').
}
/**
* Android 4.4 Kitkat / Nexus 5 / Factory Image Release email alert cronjob script.
*
* 1. Download this script.
* Browser download: https://gist.github.com/eyecatchup/7017108/download/
* Or, for the console users, one of:
* # wget --no-check-certificate https://gist.github.com/eyecatchup/7017108/raw/ /path/android.cron.php
* # wget --no-check-certificate https://gist.github.com/eyecatchup/7017108/raw/watchAndroid44Nexus5.cronjob.php
*
* 2. Set the value for the variable $notifyEmailAddress to your email address.
* (Optionally, set a value for the $deviceCodename variable.)
*
* 3. Create a new cronjob (for a Windows Xampp stack alternative see: http://stackoverflow.com/a/4231634/624466).
* On your console, type:
* # crontab -e
*
* 4. Paste the following (adjust script/logfile path):
* 0 * * * * php /absolute/path/to/script.php >> /absolute/path/to/cronjob.log
*
* Exit crontab and you'Re done. You'll get an email as soon as there's something to checkout.
*/
/**
* CHANGE THIS TO YOUR EMAIL ADDRESS !!!
*/
$notifyEmailAddress = '[email protected]';
/**
* Change the following variable's value from "" to e.g. "occam",
* to get notified as soon as the factory image for "occam" (Nexus 4)
* is available.
*/
$deviceCodename = "";
/**
* NO NEED FOR CHANGES BELOW
*/
$urls = array(
//check potential product URLs
'checkHttpStatus' => array(
'www.google.com/nexus/5/',
'www.android.com/devices/detail/nexus-5',
'www.android.com/devices/images/device/nexus-5/large/0',
'www.android.com/about/kitkat/',
'developer.android.com/about/versions/android-4.4.html',
'developer.android.com/sdk/api_diff/19/changes/changes-summary.html',
'android.googlesource.com/device/lge/hammerhead/',
'android.googlesource.com/device/ti/hammerhead',
'static.googleusercontent.com/external_content/untrusted_dlcp/source.android.com/en//compatibility/android-4.4-cdd.pdf',
),
//check for string "Nexus 5" (case-insensitive)
'checkStringNexus5' => array(
'www.android.com/about/',
'www.android.com/whatsnew/',
'play.google.com/store/devices',
'developers.google.com/android/nexus/images',
'developers.google.com/android/nexus/drivers',
'source.android.com/source/build-numbers.html',
'source.android.com/source/building-devices.html',
'source.android.com/source/building-kernels.html',
),
//check for string "Hammerhead" (case-insensitive)
'checkStringHammerhead' => array(
'developers.google.com/android/nexus/images',
'developers.google.com/android/nexus/drivers',
'source.android.com/source/building-devices.html',
'source.android.com/source/building-kernels.html',
),
//check for string "4.4"
'checkStringAndroid44' => array(
'www.android.com/whatsnew/',
'www.android.com/about/',
'source.android.com/',
'developers.google.com/android/nexus/images',
'developers.google.com/android/nexus/drivers',
'source.android.com/source/build-numbers.html',
'source.android.com/compatibility/downloads.html',
)
);
class N5
{
const UA = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.69 Safari/537.36';
public static function match($url, $regex) {
$str = self::getHttpResponse("https://{$url}");
return !$str ? false : (bool)preg_match("/{$regex}/iu", trim($str));
}
public static function getHttpResponse($url) {
$ch = curl_init($url);
curl_setopt_array($ch, array(
CURLOPT_USERAGENT => N5::UA,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_CONNECTTIMEOUT => 30,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_MAXREDIRS => 3,
CURLOPT_SSL_VERIFYPEER => 0,
));
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return 200 == (int)$httpCode ? $response : false;
}
public static function getHttpCode($url) {
$ch = curl_init($url);
curl_setopt_array($ch, array(
CURLOPT_USERAGENT => N5::UA,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_CONNECTTIMEOUT => 15,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_MAXREDIRS => 3,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_NOBODY => 1,
));
curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return (int)$httpCode;
}
}
$found = array();
//check potential product URLs
foreach ($urls['checkHttpStatus'] as $url) {
(200 == N5::getHttpCode("https://{$url}")) && array_push($found, "{$url} (product URL)");
}
//check for string "Nexus 5" (case-insensitive)
foreach ($urls['checkStringNexus5'] as $url) {
(false !== N5::match($url, 'nexus 5')) && array_push($found, "{$url} (contains string 'Nexus 5')");
}
//check for string "Hammerhead" (case-insensitive)
foreach ($urls['checkStringHammerhead'] as $url) {
(false !== N5::match($url, 'hammerhead')) && array_push($found, "{$url} (contains string 'Hammerhead')");
}
//check for string "4.4"
foreach ($urls['checkStringAndroid44'] as $url) {
(false !== N5::match($url, '4\.4')) && array_push($found, "{$url} (contains string '4.4')");
}
if (0 < strlen($deviceCodename)) {
//check Kitkat factory image availability
$deviceCodename = strtolower($deviceCodename);
$str = N5::getHttpResponse('developers.google.com/android/nexus/images');
$isKitkatFactoryImageAvailable = !$str ? false : (bool)preg_match('/#' . $deviceCodename . 'k/iu', trim($str));
if ($isKitkatFactoryImageAvailable) {
$msg = 'The Kitkat Factory Image for ' . ucfirst($deviceCodename) . ' is available! Rush to https://developers.google.com/android/nexus/images#' . $deviceCodename . ' ;-)';
mail($notifyEmailAddress, 'Android 4.4 Kitkat for ' . ucfirst($deviceCodename) . ' Alert', $msg);
print trim($msg);
}
}
if (0 < sizeof($found)) {
$msg = 'Strike! Check the following URL(s):' . PHP_EOL;
foreach ($found as $url) {
$msg .= "https://{$url}" . PHP_EOL;
}
mail($notifyEmailAddress, 'Android 4.4 Kitkat / Nexus 5 Alert', $msg);
print $msg;
}
else {
printf('[%s]: Nothing found.' . PHP_EOL, date('Y/m/d H:i'));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment