Last active
February 20, 2016 11:58
-
-
Save sergejmueller/74b0e0bcca06a0ce85dd to your computer and use it in GitHub Desktop.
VK(.com) Bot for the last_seen value
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* VK(.com) Bot for the last_seen value | |
* | |
* @author Sergej Müller | |
*/ | |
/** | |
* Main class | |
*/ | |
class LastSeenVKBot { | |
/** | |
* @var string $vk_api_url VK API URL for user requests | |
*/ | |
protected static $vk_api_url = 'http://api.vkontakte.ru/method/users.get'; | |
/** | |
* Prepare API call and user notification | |
* | |
* @param integer $vk_user_id VK UserID | |
* @param integer $last_seen_alert Maximum off-time in seconds before a notification is sent out | |
* @param boolean $skip_api_call Break API calls (e.g. timed calls) | |
* | |
* @return void | |
*/ | |
public function __construct($vk_user_id, $last_seen_alert, $skip_api_call) { | |
if ( ! empty($skip_api_call) ) { | |
return; | |
} | |
if ( empty($vk_user_id) ) { | |
die('Empty $vk_user_id'); | |
} | |
if ( empty($last_seen_alert) ) { | |
return; | |
} | |
$last_seen_time = (int) self::_get_last_seen( $vk_user_id ); | |
$offline_time = time() - $last_seen_time; | |
if ( $offline_time >= $last_seen_alert ) { | |
self::_send_user_alert( | |
self::_human_time( $offline_time ) | |
); | |
} | |
die('Done!'); | |
} | |
/** | |
* API Call for the last_seen value binded on UserID | |
* | |
* @param integer $vk_user_id UserID | |
* | |
* @return integer Last seen timestamp for UserID | |
*/ | |
protected static function _get_last_seen($vk_user_id) { | |
if ( ! is_numeric($vk_user_id) ) { | |
die('Empty $vk_user_id'); | |
} | |
$content = file_get_contents( | |
sprintf( | |
'%s?%s', | |
self::$vk_api_url, | |
http_build_query( | |
array( | |
'uids' => $vk_user_id, | |
'fields' => 'last_seen' | |
) | |
) | |
) | |
); | |
if ( empty($content) ) { | |
die('Empty $content'); | |
} | |
$json = json_decode($content, true); | |
if ( json_last_error() != JSON_ERROR_NONE ) { | |
die('Error at $json'); | |
} | |
return $json['response'][0]['last_seen']['time']; | |
} | |
/** | |
* Send a notification | |
* | |
* @param integer $offline_time Offline time aggregated from VK API | |
* | |
* @return void | |
*/ | |
protected static function _send_user_alert($offline_time) { | |
mail( | |
'email@email.de', | |
'Missing on VK', | |
'Last seen: ' .$offline_time. ' ago', | |
'From: email@email.de' | |
); | |
} | |
/** | |
* Determines the difference between two timestamps in a human readable format. | |
* | |
* @param integer $time Unix timestamp from which the difference begins | |
* | |
* @return string Human readable time difference | |
*/ | |
protected static function _human_time($time) { | |
$time = ( $time < 1 ) ? 1 : $time; | |
$tokens = array ( | |
31536000 => 'year', | |
2592000 => 'month', | |
604800 => 'week', | |
86400 => 'day', | |
3600 => 'hour', | |
60 => 'minute', | |
1 => 'second' | |
); | |
foreach ($tokens as $unit => $text) { | |
if ($time < $unit) continue; | |
$numberOfUnits = floor($time / $unit); | |
return sprintf( | |
'%d %s%s', | |
$numberOfUnits, | |
$text, | |
( $numberOfUnits > 1 ? 's' : '' ) | |
); | |
} | |
} | |
} | |
new LastSeenVKBot( | |
1234567, // target user id | |
(3 * 60 * 60), // 3 hours | |
( date('G') < 9 OR date('G') > 21 ) // daytime only | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment