Advertisement
Guest User

Untitled

a guest
Apr 26th, 2015
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 12.17 KB | None | 0 0
  1. function StreamStats($stream_id)
  2.     {
  3.         if(!$stream_id) { die(); }
  4.         $stream_info = CheckUserID($stream_id);
  5.        
  6.         // Get chatter list
  7.         $chat_list = page_fetch("https://tmi.twitch.tv/group/user/".$stream_info['name']."/chatters");
  8.         $chat_fetch = json_decode($chat_list, true);
  9.         if($chat_fetch['chatter_count'] > 0)
  10.             {          
  11.                 $total_chatters = 0;
  12.                 $update_list = '';
  13.                 $cur_chatter = 0;
  14.                 while(isset($chat_fetch['chatters']['staff'][$cur_chatter]))
  15.                     {
  16.                         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))
  17.                             {
  18.                                 $update_list .= ' || ID = '.apc_fetch('ManaBot-UserID-'.$chat_fetch['chatters']['staff'][$cur_chatter].'-'.$stream_id);
  19.                             }
  20.                         else
  21.                             {
  22.                                 apc_store('ManaBot-Process-Online', apc_fetch('ManaBot-Process-Online').'%'.$stream_id.'-'.$chat_fetch['chatters']['staff'][$cur_chatter], 86400);
  23.                             }
  24.                         apc_store('ManaBot-Online-'.$stream_id.'-'.$chat_fetch['chatters']['staff'][$cur_chatter], 1, 900);
  25.                         $cur_chatter++;
  26.                         $total_chatters++;
  27.                     }
  28.                 $cur_chatter = 0;
  29.                 while(isset($chat_fetch['chatters']['admins'][$cur_chatter]))
  30.                     {
  31.                         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))
  32.                             {
  33.                                 $update_list .= ' || ID = '.apc_fetch('ManaBot-UserID-'.$chat_fetch['chatters']['admins'][$cur_chatter].'-'.$stream_id);
  34.                             }
  35.                         else
  36.                             {
  37.                                 apc_store('ManaBot-Process-Online', apc_fetch('ManaBot-Process-Online').'%'.$stream_id.'-'.$chat_fetch['chatters']['admins'][$cur_chatter], 86400);
  38.                             }
  39.                         apc_store('ManaBot-Online-'.$stream_id.'-'.$chat_fetch['chatters']['admins'][$cur_chatter], 1, 900);
  40.                         $cur_chatter++;
  41.                         $total_chatters++;
  42.                     }
  43.                 $cur_chatter = 0;
  44.                 while(isset($chat_fetch['chatters']['moderators'][$cur_chatter]))
  45.                     {
  46.                         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))
  47.                             {
  48.                                 $update_list .= ' || ID = '.apc_fetch('ManaBot-UserID-'.$chat_fetch['chatters']['moderators'][$cur_chatter].'-'.$stream_id);
  49.                             }
  50.                         else
  51.                             {
  52.                                 apc_store('ManaBot-Process-Online', apc_fetch('ManaBot-Process-Online').'%'.$stream_id.'-'.$chat_fetch['chatters']['moderators'][$cur_chatter], 86400);
  53.                             }
  54.                         apc_store('ManaBot-Online-'.$stream_id.'-'.$chat_fetch['chatters']['moderators'][$cur_chatter], 1, 900);
  55.                         $cur_chatter++;
  56.                         $total_chatters++;
  57.                     }
  58.                 $cur_chatter = 0;
  59.                 while(isset($chat_fetch['chatters']['viewers'][$cur_chatter]))
  60.                     {
  61.                         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))
  62.                             {
  63.                                 $update_list .= ' || ID = '.apc_fetch('ManaBot-UserID-'.$chat_fetch['chatters']['viewers'][$cur_chatter].'-'.$stream_id);
  64.                             }
  65.                         else
  66.                             {
  67.                                 apc_store('ManaBot-Process-Online', apc_fetch('ManaBot-Process-Online').'%'.$stream_id.'-'.$chat_fetch['chatters']['viewers'][$cur_chatter], 86400);
  68.                             }
  69.                         apc_store('ManaBot-Online-'.$stream_id.'-'.$chat_fetch['chatters']['viewers'][$cur_chatter], 1, 900);
  70.                         $cur_chatter++;
  71.                         $total_chatters++;
  72.                     }
  73.                 if($total_chatters AND $update_list)
  74.                     {
  75.                         mysql_query("UPDATE `stream_users` SET online = 15 WHERE (".substr($update_list, 4).") AND online < 7") or die(mysql_error());
  76.                     }
  77.             }
  78.            
  79.         // Get follower list
  80.         $follow_list = page_fetch("https://api.twitch.tv/kraken/channels/".$stream_info['name']."/follows");
  81.         $follow_fetch = json_decode($follow_list, true);
  82.         if($follow_list AND $follow_fetch)
  83.             {
  84.                 $cur_stream = 0;
  85.                 while(isset($follow_fetch['follows'][$cur_stream]['user']['name']))
  86.                     {
  87.                         $cur_user = null;
  88.                         if(!apc_exists('ManaBot-CheckFollow-'.$follow_fetch['follows'][$cur_stream]['user']['name'].'-'.$stream_id))
  89.                             {
  90.                                 apc_store('ManaBot-CheckFollow-'.$follow_fetch['follows'][$cur_stream]['user']['name'].'-'.$stream_id, 1, 120);
  91.                                 $cur_user = CheckStreamUser($follow_fetch['follows'][$cur_stream]['user']['name'], $stream_id);
  92.                                 if($cur_user['follower'] < 1 AND isset($cur_user))
  93.                                     {
  94.                                         $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());  
  95.                                         $cur_user2 = mysql_fetch_array($result2);
  96.                                         if($cur_user2['follower'] < 1)
  97.                                             {
  98.                                                 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());
  99.                                                 if(isset($follow_fetch['follows'][$cur_stream]['created_at']) AND substr($follow_fetch['follows'][$cur_stream]['created_at'], 0, 10) == date('Y-m-d'))
  100.                                                     {
  101.                                                         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());
  102.                                                         $alert_content = '{
  103.     "type": "follower",
  104.     "name": "'.$cur_user['name'].'"
  105. }';
  106.                                                         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());
  107.                                                     }
  108.                                                 apc_delete('ManaBot-User-'.$cur_user['user_id'].'-'.$stream_id);
  109.                                             }
  110.                                     }
  111.                             }
  112.                         $cur_stream++;
  113.                     }
  114.             }
  115.        
  116.         if($stream_info['token'])
  117.             {
  118.                 // Get subscriber list
  119.                 $ch = curl_init();
  120.                 $headers[] = "Client-ID: [KEY]";
  121.                 $headers[] = "Accept: application/vnd.twitchtv.v2+json";
  122.                 $headers[] = "Authorization: OAuth ".$stream_info['token'];
  123.                 curl_setopt($ch, CURLOPT_URL, "https://api.twitch.tv/kraken/channels/".$stream_info['name']."/subscriptions?direction=DESC&limit=100");
  124.                 curl_setopt($ch, CURLOPT_VERBOSE, 1);
  125.                 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  126.                 curl_setopt($ch, CURLOPT_AUTOREFERER, false);
  127.                 curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
  128.                 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  129.                 curl_setopt($ch, CURLOPT_HEADER, 0);
  130.                 curl_setopt($ch, CURLOPT_ENCODING, '');
  131.                 curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
  132.                 $result = curl_exec($ch);
  133.                 curl_close($ch);
  134.                 $sub_fetch = json_decode($result, true);
  135.                 if($result AND $sub_fetch)
  136.                     {
  137.                         $cur_stream = 0;
  138.                         while(isset($sub_fetch['subscriptions'][$cur_stream]['user']['name']))
  139.                             {
  140.                                 if(!apc_exists('ManaBot-CheckSub-'.$sub_fetch['subscriptions'][$cur_stream]['user']['name'].'-'.$stream_id))
  141.                                     {
  142.                                         apc_store('ManaBot-CheckSub-'.$sub_fetch['subscriptions'][$cur_stream]['user']['name'].'-'.$stream_id, 1, 120);
  143.                                         $cur_user = CheckStreamUser($sub_fetch['subscriptions'][$cur_stream]['user']['name'], $stream_id);
  144.                                         if($cur_user['subscriber'] < 1 AND isset($cur_user))
  145.                                             {
  146.                                                 $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());  
  147.                                                 $cur_user2 = mysql_fetch_array($result2);
  148.                                                 if($cur_user2['subscriber'] < 1)
  149.                                                     {
  150.                                                         $datetime1 = strtotime('+1 month', strtotime($sub_fetch['subscriptions'][$cur_stream]['created_at']));
  151.                                                         $datetime1 = new DateTime(date('Y-m-d', $datetime1));
  152.                                                         $datetime2 = new DateTime(date('Y-m-d'));
  153.                                                         $time_left = $datetime1->diff($datetime2);
  154.                                                         $time_left = $time_left->days;
  155.                                                         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());
  156.                                                         if(substr($sub_fetch['subscriptions'][$cur_stream]['created_at'], 0, 10) == date('Y-m-d'))
  157.                                                             {
  158.                                                                 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());
  159.                                                                 $alert_content = '{
  160.     "type": "subscriber",
  161.     "name": "'.$cur_user['name'].'"
  162. }';
  163.                                                                 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());
  164.                                                             }
  165.                                                         apc_delete('ManaBot-User-'.$cur_user['user_id'].'-'.$stream_info['ID']);
  166.                                                     }
  167.                                             }
  168.                                     }
  169.                                 $cur_stream++;
  170.                             }
  171.                     }
  172.             }
  173.        
  174.         // Get channel info
  175.         $chan_info = page_fetch("https://api.twitch.tv/kraken/streams?channel=".$stream_info['name']);
  176.         $chan_fetch = json_decode($chan_info, true);
  177.        
  178.         // Set as online
  179.         $profile_banner = '';
  180.         $profile_logo = '';
  181.         $stream_live = 1;
  182.         if(isset($chan_fetch['_total']) AND $chan_fetch['_total'] > 0)
  183.             {
  184.                 apc_store('ManaBot-Stream-Online-'.$stream_id, 1, 180);
  185.                 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']."'"; }
  186.                 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']."'"; }
  187.                 // Creating stats
  188.                 if(!isset($chan_fetch['_total'])){$chan_fetch['_total']=0;}
  189.                 if(!isset($sub_fetch['_total'])){$sub_fetch['_total']='subscribers';}
  190.                 if(!isset($chan_fetch['streams'][0]['viewers'])){$chan_fetch['streams'][0]['viewers']=0;}
  191.                 if(!isset($chan_fetch['streams'][0]['channel']['views'])){$chan_fetch['streams'][0]['channel']['views']='views';}
  192.                 if(!isset($chan_fetch['streams'][0]['channel']['followers'])){$chan_fetch['streams'][0]['channel']['followers']='followers';}
  193.                 if(!isset($chan_fetch['streams'][0]['channel']['game'])){$chan_fetch['streams'][0]['channel']['game']="";}
  194.                 if(!isset($chan_fetch['streams'][0]['channel']['status'])){$chan_fetch['streams'][0]['channel']['status']=0;}
  195.                 //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()); }
  196.                 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());
  197.             }
  198.         else
  199.             {
  200.                 if(!apc_exists('ManaBot-Stream-Online-'.$stream_id) AND $stream_info['stream_live'] > 0)
  201.                     {
  202.                         $stream_live = 0;
  203.                         mysql_query("UPDATE `users` SET stream_live = '".$stream_live."' WHERE ID = ".$stream_id) or die(mysql_error());
  204.                     }
  205.             }
  206.            
  207.         apc_store('ManaBot-Stream-Stats-'.$stream_id, 1, 20);
  208.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement