Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function StreamStats($stream_id)
- {
- if(!$stream_id) { die(); }
- $stream_info = CheckUserID($stream_id);
- // Get chatter list
- $chat_list = page_fetch("https://tmi.twitch.tv/group/user/".$stream_info['name']."/chatters");
- $chat_fetch = json_decode($chat_list, true);
- if($chat_fetch['chatter_count'] > 0)
- {
- $total_chatters = 0;
- $update_list = '';
- $cur_chatter = 0;
- while(isset($chat_fetch['chatters']['staff'][$cur_chatter]))
- {
- if(apc_exists('ManaBot-Online-'.$stream_id.'-'.$chat_fetch['chatters']['staff'][$cur_chatter]) AND apc_exists('ManaBot-UserID-'.$chat_fetch['chatters']['staff'][$cur_chatter].'-'.$stream_id))
- {
- $update_list .= ' || ID = '.apc_fetch('ManaBot-UserID-'.$chat_fetch['chatters']['staff'][$cur_chatter].'-'.$stream_id);
- }
- else
- {
- apc_store('ManaBot-Process-Online', apc_fetch('ManaBot-Process-Online').'%'.$stream_id.'-'.$chat_fetch['chatters']['staff'][$cur_chatter], 86400);
- }
- apc_store('ManaBot-Online-'.$stream_id.'-'.$chat_fetch['chatters']['staff'][$cur_chatter], 1, 900);
- $cur_chatter++;
- $total_chatters++;
- }
- $cur_chatter = 0;
- while(isset($chat_fetch['chatters']['admins'][$cur_chatter]))
- {
- if(apc_exists('ManaBot-Online-'.$stream_id.'-'.$chat_fetch['chatters']['admins'][$cur_chatter]) AND apc_exists('ManaBot-UserID-'.$chat_fetch['chatters']['admins'][$cur_chatter].'-'.$stream_id))
- {
- $update_list .= ' || ID = '.apc_fetch('ManaBot-UserID-'.$chat_fetch['chatters']['admins'][$cur_chatter].'-'.$stream_id);
- }
- else
- {
- apc_store('ManaBot-Process-Online', apc_fetch('ManaBot-Process-Online').'%'.$stream_id.'-'.$chat_fetch['chatters']['admins'][$cur_chatter], 86400);
- }
- apc_store('ManaBot-Online-'.$stream_id.'-'.$chat_fetch['chatters']['admins'][$cur_chatter], 1, 900);
- $cur_chatter++;
- $total_chatters++;
- }
- $cur_chatter = 0;
- while(isset($chat_fetch['chatters']['moderators'][$cur_chatter]))
- {
- if(apc_exists('ManaBot-Online-'.$stream_id.'-'.$chat_fetch['chatters']['moderators'][$cur_chatter]) AND apc_exists('ManaBot-UserID-'.$chat_fetch['chatters']['moderators'][$cur_chatter].'-'.$stream_id))
- {
- $update_list .= ' || ID = '.apc_fetch('ManaBot-UserID-'.$chat_fetch['chatters']['moderators'][$cur_chatter].'-'.$stream_id);
- }
- else
- {
- apc_store('ManaBot-Process-Online', apc_fetch('ManaBot-Process-Online').'%'.$stream_id.'-'.$chat_fetch['chatters']['moderators'][$cur_chatter], 86400);
- }
- apc_store('ManaBot-Online-'.$stream_id.'-'.$chat_fetch['chatters']['moderators'][$cur_chatter], 1, 900);
- $cur_chatter++;
- $total_chatters++;
- }
- $cur_chatter = 0;
- while(isset($chat_fetch['chatters']['viewers'][$cur_chatter]))
- {
- if(apc_exists('ManaBot-Online-'.$stream_id.'-'.$chat_fetch['chatters']['viewers'][$cur_chatter]) AND apc_exists('ManaBot-UserID-'.$chat_fetch['chatters']['viewers'][$cur_chatter].'-'.$stream_id))
- {
- $update_list .= ' || ID = '.apc_fetch('ManaBot-UserID-'.$chat_fetch['chatters']['viewers'][$cur_chatter].'-'.$stream_id);
- }
- else
- {
- apc_store('ManaBot-Process-Online', apc_fetch('ManaBot-Process-Online').'%'.$stream_id.'-'.$chat_fetch['chatters']['viewers'][$cur_chatter], 86400);
- }
- apc_store('ManaBot-Online-'.$stream_id.'-'.$chat_fetch['chatters']['viewers'][$cur_chatter], 1, 900);
- $cur_chatter++;
- $total_chatters++;
- }
- if($total_chatters AND $update_list)
- {
- mysql_query("UPDATE `stream_users` SET online = 15 WHERE (".substr($update_list, 4).") AND online < 7") or die(mysql_error());
- }
- }
- // Get follower list
- $follow_list = page_fetch("https://api.twitch.tv/kraken/channels/".$stream_info['name']."/follows");
- $follow_fetch = json_decode($follow_list, true);
- if($follow_list AND $follow_fetch)
- {
- $cur_stream = 0;
- while(isset($follow_fetch['follows'][$cur_stream]['user']['name']))
- {
- $cur_user = null;
- if(!apc_exists('ManaBot-CheckFollow-'.$follow_fetch['follows'][$cur_stream]['user']['name'].'-'.$stream_id))
- {
- apc_store('ManaBot-CheckFollow-'.$follow_fetch['follows'][$cur_stream]['user']['name'].'-'.$stream_id, 1, 120);
- $cur_user = CheckStreamUser($follow_fetch['follows'][$cur_stream]['user']['name'], $stream_id);
- if($cur_user['follower'] < 1 AND isset($cur_user))
- {
- $result2 = mysql_query("SELECT * FROM stream_users WHERE user_id = '".$cur_user['user_id']."' AND stream_id = ".$stream_id." LIMIT 1") or die(mysql_error());
- $cur_user2 = mysql_fetch_array($result2);
- if($cur_user2['follower'] < 1)
- {
- mysql_query("UPDATE `stream_users` SET follower = 1 WHERE user_id = '".$cur_user['user_id']."' AND stream_id = '".$stream_id."' LIMIT 1") or die(mysql_error());
- if(isset($follow_fetch['follows'][$cur_stream]['created_at']) AND substr($follow_fetch['follows'][$cur_stream]['created_at'], 0, 10) == date('Y-m-d'))
- {
- mysql_query("INSERT INTO notifications (user_id, message_type, message_content, date_sent) VALUES ('".$stream_id."', '1', '".ucfirst($cur_user['name'])." is now following the stream.', '".date('m-d-Yh:i:sa')."')") or die(mysql_error());
- $alert_content = '{
- "type": "follower",
- "name": "'.$cur_user['name'].'"
- }';
- mysql_query("INSERT INTO alerts (stream_id, message_type, message_content, date_created) VALUES ('".$stream_id."', '1', '".$alert_content."', '".date('m-d-Yh:i:sa')."')") or die(mysql_error());
- }
- apc_delete('ManaBot-User-'.$cur_user['user_id'].'-'.$stream_id);
- }
- }
- }
- $cur_stream++;
- }
- }
- if($stream_info['token'])
- {
- // Get subscriber list
- $ch = curl_init();
- $headers[] = "Client-ID: [KEY]";
- $headers[] = "Accept: application/vnd.twitchtv.v2+json";
- $headers[] = "Authorization: OAuth ".$stream_info['token'];
- curl_setopt($ch, CURLOPT_URL, "https://api.twitch.tv/kraken/channels/".$stream_info['name']."/subscriptions?direction=DESC&limit=100");
- curl_setopt($ch, CURLOPT_VERBOSE, 1);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_AUTOREFERER, false);
- curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
- curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
- curl_setopt($ch, CURLOPT_HEADER, 0);
- curl_setopt($ch, CURLOPT_ENCODING, '');
- curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
- $result = curl_exec($ch);
- curl_close($ch);
- $sub_fetch = json_decode($result, true);
- if($result AND $sub_fetch)
- {
- $cur_stream = 0;
- while(isset($sub_fetch['subscriptions'][$cur_stream]['user']['name']))
- {
- if(!apc_exists('ManaBot-CheckSub-'.$sub_fetch['subscriptions'][$cur_stream]['user']['name'].'-'.$stream_id))
- {
- apc_store('ManaBot-CheckSub-'.$sub_fetch['subscriptions'][$cur_stream]['user']['name'].'-'.$stream_id, 1, 120);
- $cur_user = CheckStreamUser($sub_fetch['subscriptions'][$cur_stream]['user']['name'], $stream_id);
- if($cur_user['subscriber'] < 1 AND isset($cur_user))
- {
- $result2 = mysql_query("SELECT * FROM stream_users WHERE user_id = '".$cur_user['user_id']."' AND stream_id = ".$stream_id." LIMIT 1") or die(mysql_error());
- $cur_user2 = mysql_fetch_array($result2);
- if($cur_user2['subscriber'] < 1)
- {
- $datetime1 = strtotime('+1 month', strtotime($sub_fetch['subscriptions'][$cur_stream]['created_at']));
- $datetime1 = new DateTime(date('Y-m-d', $datetime1));
- $datetime2 = new DateTime(date('Y-m-d'));
- $time_left = $datetime1->diff($datetime2);
- $time_left = $time_left->days;
- mysql_query("UPDATE `stream_users` SET subscriber = '".$time_left."' WHERE user_id = '".$cur_user['user_id']."' AND stream_id = '".$stream_info['ID']."' LIMIT 1") or die(mysql_error());
- if(substr($sub_fetch['subscriptions'][$cur_stream]['created_at'], 0, 10) == date('Y-m-d'))
- {
- mysql_query("INSERT INTO notifications (user_id, message_type, message_content, date_sent) VALUES ('".$stream_info['ID']."', '2', '".ucfirst($cur_user['name'])." subscribed to the stream!', '".date('m-d-Yh:i:sa')."')") or die(mysql_error());
- $alert_content = '{
- "type": "subscriber",
- "name": "'.$cur_user['name'].'"
- }';
- mysql_query("INSERT INTO alerts (stream_id, message_type, message_content, date_created) VALUES ('".$stream_info['ID']."', '2', '".$alert_content."', '".date('m-d-Yh:i:sa')."')") or die(mysql_error());
- }
- apc_delete('ManaBot-User-'.$cur_user['user_id'].'-'.$stream_info['ID']);
- }
- }
- }
- $cur_stream++;
- }
- }
- }
- // Get channel info
- $chan_info = page_fetch("https://api.twitch.tv/kraken/streams?channel=".$stream_info['name']);
- $chan_fetch = json_decode($chan_info, true);
- // Set as online
- $profile_banner = '';
- $profile_logo = '';
- $stream_live = 1;
- if(isset($chan_fetch['_total']) AND $chan_fetch['_total'] > 0)
- {
- apc_store('ManaBot-Stream-Online-'.$stream_id, 1, 180);
- if(isset($chan_fetch['streams'][0]['channel']['profile_banner']) AND $chan_fetch['streams'][0]['channel']['profile_banner'] != null AND $chan_fetch['streams'][0]['channel']['profile_banner'] != $stream_info['banner_url']) { $profile_banner = ", banner_url = '".$chan_fetch['streams'][0]['channel']['profile_banner']."'"; }
- if(isset($chan_fetch['streams'][0]['channel']['logo']) AND $chan_fetch['streams'][0]['channel']['logo'] != null AND $chan_fetch['streams'][0]['channel']['logo'] != $stream_info['image_url']) { $profile_logo = ", image_url = '".$chan_fetch['streams'][0]['channel']['logo']."'"; }
- // Creating stats
- if(!isset($chan_fetch['_total'])){$chan_fetch['_total']=0;}
- if(!isset($sub_fetch['_total'])){$sub_fetch['_total']='subscribers';}
- if(!isset($chan_fetch['streams'][0]['viewers'])){$chan_fetch['streams'][0]['viewers']=0;}
- if(!isset($chan_fetch['streams'][0]['channel']['views'])){$chan_fetch['streams'][0]['channel']['views']='views';}
- if(!isset($chan_fetch['streams'][0]['channel']['followers'])){$chan_fetch['streams'][0]['channel']['followers']='followers';}
- if(!isset($chan_fetch['streams'][0]['channel']['game'])){$chan_fetch['streams'][0]['channel']['game']="";}
- if(!isset($chan_fetch['streams'][0]['channel']['status'])){$chan_fetch['streams'][0]['channel']['status']=0;}
- //if($chan_fetch['_total'] > 0) { mysql_query("INSERT INTO stream_stats (stream_id, stream_status, stream_viewers, stream_chatters, stream_views, stream_follows, stream_subscribers, stream_game, stream_title, date_created) VALUES ('".$stream_id."', '".$chan_fetch['_total']."', '".$chan_fetch['streams'][0]['viewers']."', '".$total_chatters."', '".$chan_fetch['streams'][0]['channel']['views']."', '".$chan_fetch['streams'][0]['channel']['followers']."', '".$sub_fetch['_total']."', '".mysql_real_escape_string($chan_fetch['streams'][0]['channel']['game'])."', '".mysql_real_escape_string($chan_fetch['streams'][0]['channel']['status'])."', '".date('m-d-Yh:i:sa')."')") or die(mysql_error()); }
- mysql_query("UPDATE `users` SET stream_live = '".$stream_live."', game = '".mysql_real_escape_string($chan_fetch['streams'][0]['channel']['game'])."', viewers = ".floor($chan_fetch['streams'][0]['viewers']).", views = ".floor($chan_fetch['streams'][0]['channel']['views']).", followers = ".floor($chan_fetch['streams'][0]['channel']['followers']).", subscribers = ".floor($sub_fetch['_total']).$profile_banner.$profile_logo." WHERE ID = ".$stream_id) or die(mysql_error());
- }
- else
- {
- if(!apc_exists('ManaBot-Stream-Online-'.$stream_id) AND $stream_info['stream_live'] > 0)
- {
- $stream_live = 0;
- mysql_query("UPDATE `users` SET stream_live = '".$stream_live."' WHERE ID = ".$stream_id) or die(mysql_error());
- }
- }
- apc_store('ManaBot-Stream-Stats-'.$stream_id, 1, 20);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement