#!/usr/bin/php loadHTML($data, LIBXML_NOERROR|LIBXML_NOWARNING|LIBXML_NOENT); libxml_clear_errors(); // we KNOW it's no well-formed XML, so drop all complaints libxml_use_internal_errors(false); $xp = new DOMXpath($dom); $friends = $xp->query('//div[@id="friends_js"]')->item(0); $f_list = $xp->query('div[contains(@class, "friend")]', $friends); $f_num = $f_list->length; echo sprintf('Found %u active friend%s:', $f_num, ($f_num!=1)?'s':'') . PHP_EOL; $f_arr = array(); foreach ($f_list as $f) { $name = $xp->query('.//a[@title]/@title', $f)->item(0)->nodeValue; $profile_url = $xp->query('.//a[@title]/@href', $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; $activity = strtr(trim($xp->query('.//div[@class="userinfo"]/text()', $f)->item(0)->textContent), "\r\n", " "); echo sprintf('%s: %s (%s)', $name, $activity, $session_url) . PHP_EOL; $f_arr[$profile_url] = array( 'name' => $name, 'profile_url' => $profile_url, 'avatar' => $avatar, 'session_url' => $session_url, 'activity' => $activity, ); } $o_arr = json_decode(@file_get_contents($status_file), true); file_put_contents($status_file, json_encode($f_arr)); /************************************************************************************/ $new = array_diff_key($f_arr, $o_arr); $old = array_intersect_key($f_arr, $o_arr); // will be ignored $stopped = array_diff_key($o_arr, $f_arr); echo sprintf('STARTED:%u, STILL GOING:%u, STOPPED:%u', count($new), count($old), count($stopped)) . PHP_EOL; /************************************************************************************/ if (count($new) + count($stopped) == 0) { echo 'Nothing to notify about.' . PHP_EOL; 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) > 0) { $friend = reset($new); $title = str_replace(' is out ', ' started ', $friend['activity']); } else { $friend = reset($stopped); $title = str_replace(' is out ', ' finished ', $friend['activity']); } $message = $friend['name'] . ': ' . $title; if (count($old) > 0) { $message .= PHP_EOL . sprintf('%u other%s still going strong.', count($old), count($old)>1?'s':''); } $url = $friend['session_url']; $url_title = 'Runtastic session'; } else { $title = sprintf('%u Runtastic updates', count($new) + count($stopped)); $names = array(); $message = ''; foreach ($new as $f) { $names[] = $f['name']; $message .= str_replace(' is out ', ' started ' , $f['activity']) . ' (' . $f['session_url'] . ')' . PHP_EOL; } foreach ($stopped as $f) { $names[] = $f['name']; $message .= str_replace(' is out ', ' finished ' , $f['activity']) . ' (' . $f['session_url'] . ')' . PHP_EOL; } if (count($old) > 0) { $message .= sprintf('%u other%s still going strong.', count($old), count($old)>1?'s':''); } $title .= ': ' . implode(', ', $names); $url = $fetch_url; $url_title = 'Runtastic Live'; } $notify_data = array( 'token' => $pushover_token, 'user' => $user_key, 'title' => $title, 'message' => $message, 'url' => $url, 'url_title' => $url_title, 'priority' => 0, 'timestamp' => $stamp, 'sound' => 'bike', ); $data_str = http_build_query($notify_data); $context = stream_context_create(array( 'http' => array( 'method' => 'POST', 'header' => "Content-Type: application/x-www-form-urlencoded\r\n" . "Content-Length: " . strlen($data_str) . "\r\n", 'content' => $data_str, ), )); print_r($notify_data); $result = file_get_contents($pushover_api, false, $context); if ($result === false) { echo 'ERROR: Problem sending notification.' . PHP_EOL; exit(2); } exit(0);