Changed to new URL, adjusted XPath queries. More error checking. Put notifications into separate methods.
This commit is contained in:
+44
-20
@@ -6,14 +6,17 @@ $config = parse_ini_file($basedir . '/CONFIG');
|
|||||||
|
|
||||||
$user_url = $config['RUNTASTIC_USER_URL'];
|
$user_url = $config['RUNTASTIC_USER_URL'];
|
||||||
|
|
||||||
$fetch_url = sprintf('https://www.runtastic.com/en/users/%s/friends/live', $user_url);
|
// $fetch_url = sprintf('https://www.runtastic.com/de/benutzer/%s/friends_live_sessions', $user_url);
|
||||||
$status_file = '/tmp/runtastic.json';
|
$fetch_url = sprintf('https://www.runtastic.com/en/users/%s/friends_live_sessions', $user_url);
|
||||||
|
$status_file = sprintf('/tmp/runtastic_%s.json', $user_url);
|
||||||
|
|
||||||
$stamp = time();
|
$stamp = time();
|
||||||
echo sprintf('STARTED: %s', date('Y-m-d H:i:s', $stamp)) . PHP_EOL;
|
echo sprintf('STARTED: %s', date('Y-m-d H:i:s', $stamp)) . PHP_EOL;
|
||||||
|
|
||||||
$data = file_get_contents($fetch_url);
|
$data = file_get_contents($fetch_url);
|
||||||
#$data = file_get_contents('runtastic_live1.html');
|
#$data = file_get_contents('runtastic_live5.html');
|
||||||
|
|
||||||
|
$data = '<root>' . $data . '</root>';
|
||||||
|
|
||||||
libxml_use_internal_errors(true); // prevent generation of PHP Warnings
|
libxml_use_internal_errors(true); // prevent generation of PHP Warnings
|
||||||
$dom = new DOMDocument();
|
$dom = new DOMDocument();
|
||||||
@@ -23,9 +26,7 @@ libxml_use_internal_errors(false);
|
|||||||
|
|
||||||
$xp = new DOMXpath($dom);
|
$xp = new DOMXpath($dom);
|
||||||
|
|
||||||
$friends = $xp->query('//div[@id="friends_js"]')->item(0);
|
$f_list = $xp->query('//div[contains(@class, "run_session")]');
|
||||||
$f_list = $xp->query('div[contains(@class, "friend")]', $friends);
|
|
||||||
|
|
||||||
$f_num = $f_list->length;
|
$f_num = $f_list->length;
|
||||||
|
|
||||||
echo sprintf('Found %u active friend%s:', $f_num, ($f_num!=1)?'s':'') . PHP_EOL;
|
echo sprintf('Found %u active friend%s:', $f_num, ($f_num!=1)?'s':'') . PHP_EOL;
|
||||||
@@ -35,8 +36,8 @@ foreach ($f_list as $f) {
|
|||||||
$name = $xp->query('.//a[@title]/@title', $f)->item(0)->nodeValue;
|
$name = $xp->query('.//a[@title]/@title', $f)->item(0)->nodeValue;
|
||||||
$profile_url = $xp->query('.//a[@title]/@href', $f)->item(0)->nodeValue;
|
$profile_url = $xp->query('.//a[@title]/@href', $f)->item(0)->nodeValue;
|
||||||
$avatar = $xp->query('.//img/@src', $f)->item(0)->nodeValue;
|
$avatar = $xp->query('.//img/@src', $f)->item(0)->nodeValue;
|
||||||
$session_url = $xp->query('.//a[@data-gaq-action]/@href', $f)->item(0)->nodeValue;
|
$session_url = $xp->query('.//a[not(@title)]/@href', $f)->item(0)->nodeValue;
|
||||||
$activity = strtr(trim($xp->query('.//div[@class="userinfo"]/text()', $f)->item(0)->textContent), "\r\n", " ");
|
$activity = strtr(trim($xp->query('.//div[@class="content"]/text()', $f)->item(0)->textContent), "\r\n", " ");
|
||||||
|
|
||||||
echo sprintf('%s: %s (%s)', $name, $activity, $session_url) . PHP_EOL;
|
echo sprintf('%s: %s (%s)', $name, $activity, $session_url) . PHP_EOL;
|
||||||
|
|
||||||
@@ -49,7 +50,12 @@ foreach ($f_list as $f) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (file_exists($status_file)) {
|
||||||
$o_arr = json_decode(@file_get_contents($status_file), true);
|
$o_arr = json_decode(@file_get_contents($status_file), true);
|
||||||
|
} else {
|
||||||
|
$o_arr = array();
|
||||||
|
}
|
||||||
|
|
||||||
file_put_contents($status_file, json_encode($f_arr));
|
file_put_contents($status_file, json_encode($f_arr));
|
||||||
|
|
||||||
/************************************************************************************/
|
/************************************************************************************/
|
||||||
@@ -67,10 +73,6 @@ if (count($new) + count($stopped) == 0) {
|
|||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
$pushover_api = 'https://api.pushover.net/1/messages.json';
|
|
||||||
$pushover_token = $config['RUNTASTIC_PUSHOVER_TOKEN'];
|
|
||||||
$user_key = $config['PUSHOVER_RECIPIENT'];
|
|
||||||
|
|
||||||
if (count($new) + count($stopped) == 1) {
|
if (count($new) + count($stopped) == 1) {
|
||||||
if (count($new) > 0) {
|
if (count($new) > 0) {
|
||||||
$friend = reset($new);
|
$friend = reset($new);
|
||||||
@@ -101,20 +103,27 @@ if (count($new) + count($stopped) == 1) {
|
|||||||
$message .= sprintf('%u other%s still going strong.', count($old), count($old)>1?'s':'');
|
$message .= sprintf('%u other%s still going strong.', count($old), count($old)>1?'s':'');
|
||||||
}
|
}
|
||||||
$title .= ': ' . implode(', ', $names);
|
$title .= ': ' . implode(', ', $names);
|
||||||
$url = $fetch_url;
|
$url = sprintf('https://www.runtastic.com/en/users/%s/friends/live', $user_url);
|
||||||
$url_title = 'Runtastic Live';
|
$url_title = 'Runtastic Live';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/****************************************************************************************/
|
||||||
|
|
||||||
|
function notify_pushover($recipient, $subject, $message, $url, $url_title, $priority = 0, $timestamp = '', $sound = '')
|
||||||
|
{
|
||||||
|
global $config;
|
||||||
|
|
||||||
|
$pushover_api = 'https://api.pushover.net/1/messages.json';
|
||||||
$notify_data = array(
|
$notify_data = array(
|
||||||
'token' => $pushover_token,
|
'token' => $config['RUNTASTIC_PUSHOVER_TOKEN'],
|
||||||
'user' => $user_key,
|
'user' => $recipient,
|
||||||
'title' => $title,
|
'title' => $subject,
|
||||||
'message' => $message,
|
'message' => $message,
|
||||||
'url' => $url,
|
'url' => $url,
|
||||||
'url_title' => $url_title,
|
'url_title' => $url_title,
|
||||||
'priority' => 0,
|
'priority' => $priority,
|
||||||
'timestamp' => $stamp,
|
'timestamp' => $timestamp,
|
||||||
'sound' => 'bike',
|
'sound' => $sound,
|
||||||
);
|
);
|
||||||
|
|
||||||
$data_str = http_build_query($notify_data);
|
$data_str = http_build_query($notify_data);
|
||||||
@@ -130,7 +139,22 @@ $context = stream_context_create(array(
|
|||||||
|
|
||||||
print_r($notify_data);
|
print_r($notify_data);
|
||||||
|
|
||||||
$result = file_get_contents($pushover_api, false, $context);
|
return file_get_contents($pushover_api, false, $context);
|
||||||
|
}
|
||||||
|
|
||||||
|
function notify_email($recipient, $subject, $message, $url, $url_title)
|
||||||
|
{
|
||||||
|
$subject = '[Runtastic] ' . $subject;
|
||||||
|
$message .= PHP_EOL . PHP_EOL . $url_title . ': ' . $url;
|
||||||
|
|
||||||
|
echo 'Subject: ' . $subject . PHP_EOL . $message . PHP_EOL;
|
||||||
|
|
||||||
|
return mail($recipient, $subject, $message);
|
||||||
|
}
|
||||||
|
|
||||||
|
$result = notify_pushover($config['PUSHOVER_RECIPIENT'], $title, $message, $url, $url_title, 0, $stamp, 'bike');
|
||||||
|
// $result = notify_email($config['EMAIL_RECIPIENT'], $title, $message, $url, $url_title);
|
||||||
|
|
||||||
if ($result === false) {
|
if ($result === false) {
|
||||||
echo 'ERROR: Problem sending notification.' . PHP_EOL;
|
echo 'ERROR: Problem sending notification.' . PHP_EOL;
|
||||||
exit(2);
|
exit(2);
|
||||||
|
|||||||
Reference in New Issue
Block a user