Skip to content

Instantly share code, notes, and snippets.

@agustinhaller
Created October 29, 2012 12:38
Show Gist options
  • Select an option

  • Save agustinhaller/3973291 to your computer and use it in GitHub Desktop.

Select an option

Save agustinhaller/3973291 to your computer and use it in GitHub Desktop.
twitter methods (sortwit)
<?php
//session_start();
ini_set('display_errors', 1);
ini_set('error_reporting', E_ALL);
require_once("includes/all.inc.php");
require_once("includes/Twitter.class.php");
// This function stores in the db (table 'methods_errors') all the errors that occurs in the sistem
// and notifies when they happen
function notifyError($method_name, $error_object, $notify_to)
{
// Connect to db
$conn_a = connect_db();
if($conn_a!=false)
{
$stringify_error = json_encode($error_object);
// Save them to the db
$q_ime = "INSERT INTO methods_errors
SET method_name = '".mysql_real_escape_string($method_name)."',
error = '".mysql_real_escape_string($stringify_error)."',
date = '".date("Y-m-d H:i:s")."'";
$r_ime = mysql_query($q_ime);
if($r_ime)
{
// Insert OK
}
else// Error when saving to db
{
}
}
// Destroy db connection
disconnect_db($conn_a);
// Send notification mail
$mail_html = "Error in method '".$method_name."': ".$stringify_error;
$mail = new PHPMailer();
$mail->SetFrom("mail@sortwit.com", 'SorTwit.com');
$mail->Subject = '[Method Error][Sortwit]';
$mail->MsgHTML($mail_html);
$receivers = $notify_to;
foreach($receivers as $mail_to){
$mail->AddAddress($mail_to);
}
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Port = EMAIL_PORT; // set the SMTP port for the GMAIL server
$mail->Host = EMAIL_HOST; // sets GMAIL as the SMTP server
$mail->Username = EMAIL_HOST_USER; // GMAIL username
$mail->Password = EMAIL_HOST_PASSWORD; // GMAIL password
$mail->SMTPSecure = 'tls';
$result = $mail->Send();
}
function initTwitter($users=null)
{
$got_tokens = false;
$errors_trace = array();
if($users!=null)
{
// Connect to db
$conn = connect_db();
if($conn!=false)
{
foreach ($users as $user)
{
if(!$got_tokens)
{
if(isset($user['username']) && $user['username']!=null && $user['username']!="")
{
// $errors_trace[] = "Try to authenticate with user '".$user['username']."'";
$q_a_t = "SELECT oauth_token, oauth_token_secret
FROM users_accounts
WHERE username = '".$user['username']."'
AND social_network = 'TWITTER'
ORDER BY date_created
LIMIT 1";
$r_a_t = mysql_query($q_a_t);
if($r_a_t)
{
if(mysql_num_rows($r_a_t) > 0)
{
$a_t = mysql_fetch_array($r_a_t);
// We've got tokens!
$USER_TOKEN = $a_t['oauth_token'];
$USER_SECRET = $a_t['oauth_token_secret'];
$got_tokens = true;
$authenticated_user = $user['username'];
// $errors_trace[] = "Tokens for user '".$user['username']."' OK!";
}
else
{
$errors_trace[] = "No user '".$user['username']."' in db";
}
}
else
{
$errors_trace[] = "Error when getting user '".$user['username']."' tokens from db";
}
}
}// END GOT TOKENS
}// END FOREACH
}
else
{
// Error when connecting to mysql
$errors_trace[] = "Error when connecting to mysql";
}
// Destroy db connection
disconnect_db($conn);
}
// If we couldn't get tokens for any user, then use own app tokens
if(!$got_tokens)
{
$USER_TOKEN = TWITTER_APP_TOKEN;
$USER_SECRET = TWITTER_APP_SECRET;
$authenticated_user = 'SORTWIT_APP';
// print_str("Couldn't authenticate with any user, using own app tokens");
}
$twitter = new Twitter(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET, $USER_TOKEN, $USER_SECRET);
$twitter->setAuthenticatedUser($authenticated_user);
// Notify errors
if(count($errors_trace)>0)
{
$dev_team = array("agustinhaller@gmail.com", "brunobar79@gmail.com");
notifyError("initTwitter", $errors_trace, $dev_team);
}
return $twitter;
}
function _getTwitterSearch($search_query, $draw_start_date, $draw_end_date, $organizer_username, $since_id=null)
{
$users = array();
$users[] = array(
"username" => $organizer_username
);
$twitter = initTwitter($users);
$twitter_search_unprocessed = $twitter->getTwitterSearch($search_query, $since_id);
// Reduce data to only needed one
$total_results = array();
foreach($twitter_search_unprocessed as $tweet)
{
// print_str($tweet, "tweet");
$tweet_id = $tweet['id_str'];
$tweet_text = $tweet['text'];
// Now we need to get tweet date in timezone 00
$server_timezone_aux = (float)SERVER_TIMEZONE;
$server_timezone = $server_timezone_aux*(-1);
$tweet_created_date_aux = strtotime($tweet['created_at']);
$tweet_created_date = date("Y-m-d H:i:s", strtotime($server_timezone." hour", $tweet_created_date_aux));
$tweet_user_id = $tweet['user']['id_str'];
$tweet_user_username = $tweet['user']['screen_name'];
$tweet_user_name = $tweet['user']['name'];
$tweet_user_profile_image = $tweet['user']['profile_image_url'];
$tweet_user_followers_count = $tweet['user']['followers_count'];
$tweet_user_friends_count = $tweet['user']['friends_count'];
// print_str($tweet_created_date, "tweet_created_date");
// print_str($draw_end_date, "draw_end_date");
// print_str($draw_start_date, "draw_start_date");
if(($tweet_created_date<=$draw_end_date) && ($tweet_created_date>=$draw_start_date))
{
$aux_tweet = array(
"tweet_id" => $tweet_id,
"tweet_text" => $tweet_text,
"tweet_created_date" => $tweet_created_date,
"tweet_user_id" => $tweet_user_id,
"tweet_user_username" => $tweet_user_username,
"tweet_user_name" => $tweet_user_name,
"tweet_user_profile_image" => $tweet_user_profile_image,
"tweet_user_followers_count" =>$tweet_user_followers_count,
"tweet_user_friends_count" =>$tweet_user_friends_count
);
// print_str($aux_tweet, "aux_tweet");
$total_results[] = $aux_tweet;
}
}// END FOREACH
// Now we have the complete search space in $total_results, lets return it
return $total_results;
}
function _getRetweets($tweet_id, $draw_start_date, $draw_end_date, $organizer_username)
{
$users = array();
$users[] = array(
"username" => $organizer_username
);
$twitter = initTwitter($users);
$twitter_search_unprocessed = $twitter->getRetweets($tweet_id);
// Reduce data to only needed one
$total_results = array();
foreach($twitter_search_unprocessed as $tweet)
{
// print_str($tweet, "tweet");
$tweet_id = $tweet['id_str'];
$tweet_text = $tweet['text'];
// Now we need to get tweet date in timezone 00
$server_timezone_aux = (float)SERVER_TIMEZONE;
$server_timezone = $server_timezone_aux*(-1);
$tweet_created_date_aux = strtotime($tweet['created_at']);
$tweet_created_date = date("Y-m-d H:i:s", strtotime($server_timezone." hour", $tweet_created_date_aux));
$tweet_user_id = $tweet['user']['id_str'];
$tweet_user_username = $tweet['user']['screen_name'];
$tweet_user_name = $tweet['user']['name'];
$tweet_user_profile_image = $tweet['user']['profile_image_url'];
$tweet_user_followers_count = $tweet['user']['followers_count'];
$tweet_user_friends_count = $tweet['user']['friends_count'];
// print_str($tweet_created_date, "tweet_created_date");
// print_str($draw_end_date, "draw_end_date");
// print_str($draw_start_date, "draw_start_date");
if(($tweet_created_date<=$draw_end_date) && ($tweet_created_date>=$draw_start_date))
{
$aux_tweet = array(
"tweet_id" => $tweet_id,
"tweet_text" => $tweet_text,
"tweet_created_date" => $tweet_created_date,
"tweet_user_id" => $tweet_user_id,
"tweet_user_username" => $tweet_user_username,
"tweet_user_name" => $tweet_user_name,
"tweet_user_profile_image" => $tweet_user_profile_image,
"tweet_user_followers_count" =>$tweet_user_followers_count,
"tweet_user_friends_count" =>$tweet_user_friends_count
);
// print_str($aux_tweet, "aux_tweet");
$total_results[] = $aux_tweet;
}
}// END FOREACH
// Now we have the complete search space in $total_results, lets return it
return $total_results;
}
function _getTwitterSearch_RT($retweet_ids, $draw_start_date, $draw_end_date, $organizer_username)
{
$total_results = array();
$processed_results = array();
foreach($retweet_ids as $retweet_id)
{
/* START SEARCHING RTs IN TWEETS THAT CONTAIN THE WHOLE TEXT THROUGH TWITTER SEARCH */
// Get scheduled rt text from db
$scheduled_rt = getScheduledTweet($retweet_id);
if($scheduled_rt!=null)
{
$search_query = $scheduled_rt['text'];
$search_space = _getTwitterSearch($search_query, $draw_start_date, $draw_end_date, $organizer_username);
$search_space_count = count($search_space);
print_str($search_space_count, "WHOLE TWEET SEARCH RESULTS");
if($search_space_count>0)
{
// Check those mentions are RTs
foreach ($search_space as $tweet)
{
// print_str($tweet, "WHOLE TWEET");
// Check if we already proccess this tweet
$tweet_id = $tweet['tweet_id'];
$already_processed = in_array($tweet_id, $processed_results);
if(!$already_processed)
{
$tweet_text = $tweet['tweet_text'];
$rt_chars = substr($tweet_text, 0, 2);
$is_rt = ($rt_chars=="RT");
print_str($is_rt, "is_rt");
if($is_rt)
{
$total_results[] = $tweet;
}
// Add tweet to processed results array
$processed_results[] = $tweet_id;
}
}// END FOREACH
}
}
print_str($processed_results, "processed_results");
/* START SEARCHING RTs IN USER RTs */
print_str("STARTING_RT_CHECK");
$retweets = _getRetweets($retweet_id, $draw_start_date, $draw_end_date, $organizer_username);
$retweets_count = count($retweets);
print_str($retweets_count, "WHOLE RETWEETS RESULTS");
if($retweets_count>0)
{
foreach($retweets as $tweet)
{
// Check if we already proccess this tweet
$tweet_id = $tweet['tweet_id'];
$already_processed = in_array($tweet_id, $processed_results);
if(!$already_processed)
{
$total_results[] = $tweet;
// Add tweet to processed results array
$processed_results[] = $tweet_id;
}
}// END FOREACH
}
}// END FOREACH
print_str($processed_results, "processed_results_FINAL");
// Now we have the complete search space in $total_results, lets return it
return $total_results;
}
function _getTwitterSearch_RT_boost($retweet_ids, $draw_start_date, $draw_end_date, $organizer_username)
{
$total_results = array();
$processed_results = array();
foreach($retweet_ids as $retweet_id)
{
$retweets = _getRetweets($retweet_id, $draw_start_date, $draw_end_date, $organizer_username);
$retweets_count = count($retweets);
print_str($retweets_count, "WHOLE RETWEETS RESULTS");
if($retweets_count>0)
{
foreach($retweets as $tweet)
{
// Check if we already proccess this tweet
$tweet_id = $tweet['tweet_id'];
$already_processed = in_array($tweet_id, $processed_results);
if(!$already_processed)
{
$total_results[] = $tweet;
// Add tweet to processed results array
$processed_results[] = $tweet_id;
}
}// END FOREACH
}
// Update boost_count in schedlued tweets table
updateRetweetBoostCount($retweet_id);
}// END FOREACH
print_str($processed_results, "processed_results_FINAL");
// Now we have the complete search space in $total_results, lets return it
return $total_results;
}
// This function returns an array with the ids of the user followers
function _getTwitterFollowers($twitter_username, $organizer_username=null, $cursor=null)
{
$users = null;
if($organizer_username!=null)
{
$users = array();
$users[] = array(
"username" => $organizer_username
);
}
$twitter = initTwitter($users);
$twitter_followers_response = $twitter->getTwitterFollowers($twitter_username, $cursor);
$twitter_followers = $twitter_followers_response['twitter_followers'];
$last_cursor = $twitter_followers_response['last_cursor'];
// Get last cursor and store it
saveCursor($last_cursor,$twitter_username);
return $twitter_followers;
}
function _getFreshFollowers($organizer_username)
{
$users = array();
$users[] = array(
"username" => $organizer_username
);
$twitter = initTwitter($users);
$fresh_followers = $twitter->getLatestFollowers($organizer_username);
return $fresh_followers;
}
function _updateTwitterFollowers($organizer_username)
{
// Start time track
$time = microtime();
$time = explode(' ', $time);
$time = $time[1] + $time[0];
$begintime = $time;
$errors_trace = array();
// This function will get only the latest 5000 followers of certain user and update them in the db
// First we ask Twitter for fresh followers
$users = array();
$users[] = array(
"username" => $organizer_username
);
$twitter = initTwitter($users);
$fresh_followers = $twitter->getLatestFollowers($organizer_username);
// If we get something from twitter, then proceed
if(count($fresh_followers) > 0)
{
// Lets get stored followers in db
$db_tw_followers = getLastFollowersFromRecord($organizer_username);
if(count($db_tw_followers)>0)
{
// Now we need to get new followers for that organizer
$new_followers = array_diff($fresh_followers, $db_tw_followers);
// Now that we have new followers, we need to store them
processNewFollowers($new_followers, $organizer_username);
// Now we should merge fresh followers with db ones
$organizer_followers_aux = array_merge($fresh_followers, $db_tw_followers);
$organizer_followers = array_unique($organizer_followers_aux);
$organizer_followers_count = count($organizer_followers);
// Convert them to db storage format
$data_to_store1 = implode("|", $organizer_followers);
// We can compress the data or not, or find a better way
$data_to_store = gzcompress($data_to_store1, 8);
// End time track
$time = microtime();
$time = explode(" ", $time);
$time = $time[1] + $time[0];
$endtime = $time;
$totaltime = ($endtime - $begintime);
// Now lets update these followers in the db
// Connect to db
$conn = connect_db();
if($conn!=false)
{
// UPDATE followers record in db
$q_uof = "UPDATE users_followers
SET tw_followers = '".mysql_real_escape_string($data_to_store)."',
tw_followers_count = ".mysql_real_escape_string($organizer_followers_count).",
followers_update_date = '".date("Y-m-d H:i:s")."',
followers_update_time_elapsed_s = ".mysql_real_escape_string($totaltime).",
needs_check = 0
WHERE tw_account = '".$organizer_username."'";
// print_str($q_uof);
$r_uof = mysql_query($q_uof);
if($r_uof)// OK
{
print_str("OK", "UPDATE ".$organizer_username." FOLLOWERS");
}
else// Error
{
$errors_trace[] = "ERROR when UPDATING '".$organizer_username."' FOLLOWERS";
}
}
else
{
// Error in mysql connection
$errors_trace[] = "ERROR IN MySql CONN";
}
// Destroy db connection
disconnect_db($conn);
}// END DB FOLLOWERS > 0
}// END FRESH FOLLOWERS > 0
// Notify errors
if(count($errors_trace)>0)
{
$dev_team = array("agustinhaller@gmail.com", "brunobar79@gmail.com");
notifyError("updateTwitterFollowers", $errors_trace, $dev_team);
}
}
function _getFollowersCount($organizer_username)
{
$users = array();
$users[] = array(
"username" => $organizer_username
);
$twitter = initTwitter($users);
// Get organizer twitter data
$twitter_data = $twitter->getUserData($organizer_username);
$followers_count = 0;
if($twitter_data!=null)
{
$followers_count = $twitter_data['followers_count'];
}
return $followers_count;
}
// This function checks if $user_a follows back $user_b, where $user_a and $user_b may be twitter account id's or twitter screen names
function _userFollowsBackUser($user_a, $user_b)
{
$users = array();
$users[] = array(
"username" => $user_a
);
$users[] = array(
"username" => $user_b
);
$twitter = initTwitter($users);
$user_follows_back_user = $twitter->userFollowsBackUser($user_a, $user_b);
return $user_follows_back_user;
}
// This functions goes to twitter and brings back all the info of up to 100 users
function _getTwitterData($tw_100_account_ids)
{
$twitter_users_ret = array();
$twitter = initTwitter();
// Get bulk twitter data
$bulk_twitter_data_unprocessed = $twitter->getBulkUsersData($tw_100_account_ids);
foreach ($bulk_twitter_data_unprocessed as $tw_user)
{
$tw_username = $tw_user['screen_name'];
$tw_account_id = $tw_user['id_str'];
$tw_name = $tw_user['name'];
$tw_followers_qty = $tw_user['followers_count'];
$tw_following_qty = $tw_user['friends_count'];
$tw_profile_url = "http://twitter.com/".$tw_username;
$tw_image = $tw_user['profile_image_url'];
$aux_user = array(
"tw_username" => $tw_username,
"tw_account_id" => $tw_account_id,
"tw_name" => $tw_name,
"tw_followers_qty" => $tw_followers_qty,
"tw_following_qty" => $tw_following_qty,
"tw_profile_url" => $tw_profile_url,
"tw_image" => $tw_image
);
$twitter_users_ret[] = $aux_user;
}
return $twitter_users_ret;
}
function _followUser($user_id, $tw_username_to_follow)
{
$errors_trace = array();
$response_array = array();
// Get data from the user
$user_data = getParticipantTwitterData($user_id);
$users = array();
$users[] = array(
"username" => $user_data['username']
);
$twitter = initTwitter($users);
// Double check if we could login with the user credentials
$authenticated_user = $twitter->getAuthenticatedUser();
if($authenticated_user == $user_data['username'])
{
// Then we can do the follow
$follow_ok = $twitter->followUser($tw_username_to_follow);
if($follow_ok)
{
$response_array = array(
'response' => "OK",
'response_msg' => "Like a charm"
);
}
else
{
$response_array = array(
'response' => "ERROR",
'response_msg' => "Twitter call error"
);
$errors_trace[] = "ERROR when '".$user_data['username']."' tried to follow '".$tw_username_to_follow."'";
$errors_trace[] = "Twitter call error";
}
}
else
{
$response_array = array(
'response' => "ERROR",
'response_msg' => "Could not authenticate with user '".$user_data['username']."'"
);
$errors_trace[] = "Could not authenticate with user '".$user_data['username']."'";
}
// Notify errors
if(count($errors_trace)>0)
{
$dev_team = array("agustinhaller@gmail.com", "brunobar79@gmail.com");
notifyError("followUser", $errors_trace, $dev_team);
}
return $response_array;
}
function _tweetText($user_id, $text_to_tweet)
{
$errors_trace = array();
$response_array = array();
// Get data from the user
$user_data = getParticipantTwitterData($user_id);
$users = array();
$users[] = array(
"username" => $user_data['username']
);
$twitter = initTwitter($users);
// Double check if we could login with the user credentials
$authenticated_user = $twitter->getAuthenticatedUser();
if($authenticated_user == $user_data['username'])
{
// Then we can post the tweet
$tweet_response = $twitter->tweetText($text_to_tweet);
if($tweet_response!=null)
{
$tweet_ok = $tweet_response['tweet_ok'];
if($tweet_ok)
{
$tweet_object = $tweet_response['object_result'];
$tweet_id = $tweet_response['tweet_id'];
$response_array = array(
'response' => "OK",
'response_msg' => "Like a charm",
'tweet_id' => $tweet_id,
'object_result' => $tweet_object
);
}
else
{
$response_array = array(
'response' => "ERROR",
'response_msg' => "Twitter call error"
);
$errors_trace[] = "ERROR when '".$user_data['username']."' tried to tweet '".$text_to_tweet."'";
$errors_trace[] = "Twitter call error";
}
}
}
else
{
$response_array = array(
'response' => "ERROR",
'response_msg' => "Could not authenticate with user '".$user_data['username']."'"
);
$errors_trace[] = "Could not authenticate with user '".$user_data['username']."'";
}
// Notify errors
if(count($errors_trace)>0)
{
$dev_team = array("agustinhaller@gmail.com", "brunobar79@gmail.com");
notifyError("tweetText", $errors_trace, $dev_team);
}
return $response_array;
}
function _retweetTweet($user_id, $retweet_id)
{
$errors_trace = array();
$response_array = array();
// Get data from the user
$user_data = getParticipantTwitterData($user_id);
$users = array();
$users[] = array(
"username" => $user_data['username']
);
$twitter = initTwitter($users);
// Double check if we could login with the user credentials
$authenticated_user = $twitter->getAuthenticatedUser();
if($authenticated_user == $user_data['username'])
{
// Then we can retweet the tweet
$retweet_response = $twitter->retweetTweet($retweet_id);
if($retweet_response!=null)
{
$retweet_ok = $retweet_response['retweet_ok'];
if($retweet_ok)
{
$tweet_object = $retweet_response['object_result'];
$tweet_id = $retweet_response['tweet_id'];
$response_array = array(
'response' => "OK",
'response_msg' => "Like a charm",
'tweet_id' => $tweet_id,
'object_result' => $tweet_object
);
}
else
{
$response_array = array(
'response' => "ERROR",
'response_msg' => "Twitter call error"
);
$errors_trace[] = "ERROR when '".$user_data['username']."' tried to retweet with id '".$retweet_id."'";
$errors_trace[] = "Twitter call error";
}
}
}
else
{
$response_array = array(
'response' => "ERROR",
'response_msg' => "Could not authenticate with user '".$user_data['username']."'"
);
$errors_trace[] = "Could not authenticate with user '".$user_data['username']."'";
}
// Notify errors
if(count($errors_trace)>0)
{
$dev_team = array("agustinhaller@gmail.com", "brunobar79@gmail.com");
notifyError("retweetText", $errors_trace, $dev_team);
}
return $response_array;
}
// This function searches tweets that contain certain keyword and were tweeted by certain user
function _getOrganizerTweets($search_query, $draw_start_date, $draw_end_date, $organizer_username)
{
$users = array();
$users[] = array(
"username" => $organizer_username
);
$twitter = initTwitter($users);
$search_query_aux = $organizer_username." ".$search_query;
$twitter_search_unprocessed = $twitter->getTwitterSearch($search_query_aux);
// Reduce data to only needed one
$total_results = array();
foreach($twitter_search_unprocessed as $tweet)
{
$tweet_id = $tweet['id_str'];
$tweet_text = $tweet['text'];
// Now we need to get tweet date in timezone 00
$server_timezone_aux = (float)SERVER_TIMEZONE;
$server_timezone = $server_timezone_aux*(-1);
$tweet_created_date_aux = strtotime($tweet['created_at']);
$tweet_created_date = date("Y-m-d H:i:s", strtotime($server_timezone." hour", $tweet_created_date_aux));
$tweet_user_id = $tweet['user']['id_str'];
$tweet_user_username = $tweet['user']['screen_name'];
$tweet_user_name = $tweet['user']['name'];
$tweet_user_profile_image = $tweet['user']['profile_image_url'];
$tweet_user_followers_count = $tweet['user']['followers_count'];
$tweet_user_friends_count = $tweet['user']['friends_count'];
// print_str($tweet_created_date, "tweet_created_date");
// print_str($draw_end_date, "draw_end_date");
// print_str($draw_start_date, "draw_start_date");
if(($tweet_user_username==$organizer_username) && ($tweet_created_date<=$draw_end_date) && ($tweet_created_date>=$draw_start_date))
{
$aux_tweet = array(
"tweet_id" => $tweet_id,
"tweet_text" => $tweet_text,
"tweet_created_date" => $tweet_created_date,
"tweet_user_id" => $tweet_user_id,
"tweet_user_username" => $tweet_user_username,
"tweet_user_name" => $tweet_user_name,
"tweet_user_profile_image" => $tweet_user_profile_image,
"tweet_user_followers_count" =>$tweet_user_followers_count,
"tweet_user_friends_count" =>$tweet_user_friends_count
);
// print_str($aux_tweet, "aux_tweet");
$total_results[] = $aux_tweet;
}
}// END FOREACH
// Now we have the complete search space in $total_results, lets return it
return $total_results;
}
function _getUserMentions($twitter_username)
{
$twitter = initTwitter();
$search_query = "@".$twitter_username;
$twitter_search_unprocessed = $twitter->getTwitterSearch($search_query);
// Reduce data to only needed one
$total_results = array();
foreach($twitter_search_unprocessed as $tweet)
{
$tweet_id = $tweet['id_str'];
$tweet_text = $tweet['text'];
// Now we need to get tweet date in timezone 00
$server_timezone_aux = (float)SERVER_TIMEZONE;
$server_timezone = $server_timezone_aux*(-1);
$tweet_created_date_aux = strtotime($tweet['created_at']);
$tweet_created_date = date("Y-m-d H:i:s", strtotime($server_timezone." hour", $tweet_created_date_aux));
$tweet_user_id = $tweet['user']['id_str'];
$tweet_user_username = $tweet['user']['screen_name'];
$tweet_user_name = $tweet['user']['name'];
$tweet_user_profile_image = $tweet['user']['profile_image_url'];
$tweet_user_followers_count = $tweet['user']['followers_count'];
$tweet_user_friends_count = $tweet['user']['friends_count'];
// print_str($tweet_created_date, "tweet_created_date");
// print_str($draw_end_date, "draw_end_date");
// print_str($draw_start_date, "draw_start_date");
$aux_tweet = array(
"tweet_id" => $tweet_id,
"tweet_text" => $tweet_text,
"tweet_created_date" => $tweet_created_date,
"tweet_user_id" => $tweet_user_id,
"tweet_user_username" => $tweet_user_username,
"tweet_user_name" => $tweet_user_name,
"tweet_user_profile_image" => $tweet_user_profile_image,
"tweet_user_followers_count" =>$tweet_user_followers_count,
"tweet_user_friends_count" =>$tweet_user_friends_count
);
// print_str($aux_tweet, "aux_tweet");
$total_results[] = $aux_tweet;
}// END FOREACH
// Now we have the complete search space in $total_results, lets return it
return $total_results;
}
function _getTweetReplies($original_tweet_id)
{
$twitter = initTwitter();
$tweet_object = $twitter->tweetExists($original_tweet_id);
$tweet_username = $tweet_object['object']['tweet_user_username'];
$search_query = "@".$tweet_username;
$twitter_search_unprocessed = $twitter->getTwitterSearch($search_query);
// Reduce data to only needed one
$total_results = array();
foreach($twitter_search_unprocessed as $tweet)
{
$tweet_id = $tweet['id_str'];
$tweet_text = $tweet['text'];
// Now we need to get tweet date in timezone 00
$server_timezone_aux = (float)SERVER_TIMEZONE;
$server_timezone = $server_timezone_aux*(-1);
$tweet_created_date_aux = strtotime($tweet['created_at']);
$tweet_created_date = date("Y-m-d H:i:s", strtotime($server_timezone." hour", $tweet_created_date_aux));
$tweet_user_id = $tweet['user']['id_str'];
$tweet_user_username = $tweet['user']['screen_name'];
$tweet_user_name = $tweet['user']['name'];
$tweet_user_profile_image = $tweet['user']['profile_image_url'];
$tweet_user_followers_count = $tweet['user']['followers_count'];
$tweet_user_friends_count = $tweet['user']['friends_count'];
$in_reply_to_status_id = $tweet['in_reply_to_status_id'];
$original_tweet_id = (int)$original_tweet_id;
print_str($in_reply_to_status_id, "in_reply_to_status_id");
print_str($original_tweet_id, "original_tweet_id");
// print_str($tweet_created_date, "tweet_created_date");
// print_str($draw_end_date, "draw_end_date");
// print_str($draw_start_date, "draw_start_date");
if($in_reply_to_status_id!=null && $in_reply_to_status_id!="" && $in_reply_to_status_id == $original_tweet_id)
{
$aux_tweet = array(
"tweet_id" => $tweet_id,
"tweet_text" => $tweet_text,
"tweet_created_date" => $tweet_created_date,
"tweet_user_id" => $tweet_user_id,
"tweet_user_username" => $tweet_user_username,
"tweet_user_name" => $tweet_user_name,
"tweet_user_profile_image" => $tweet_user_profile_image,
"tweet_user_followers_count" =>$tweet_user_followers_count,
"tweet_user_friends_count" =>$tweet_user_friends_count
);
// print_str($aux_tweet, "aux_tweet");
$total_results[] = $aux_tweet;
}
}// END FOREACH
// Now we have the complete search space in $total_results, lets return it
return $total_results;
}
function _twitterLookup($all_participants_ids, $results_qty)
{
// Let's first sort the ids
sort($all_participants_ids);
$all_participants_qty = $results_qty;
$results_per_page = 100;
$all_participants = array();
if($all_participants_qty>0)
{
// We have to calculate how many pages are we going to bring (i mean how many calls to "getTwitterData" function do we need)
$complete_pages = floor($all_participants_qty/$results_per_page);
$pages_mod = $all_participants_qty % $results_per_page;
$max_page = ($pages_mod > 0) ? $complete_pages + 1 : $complete_pages;
$page_index = 1;
while($page_index <= $max_page)
{
$page_offset = ((($page_index-1)*$results_per_page)+1);
// Chop array to get 100's id to make the call to getTwitterData function
$page_ids = array_slice($all_participants_ids, ($page_offset-1), $results_per_page);
// Make call to getTwitterData function and append result to array
$page_participants = _getTwitterData($page_ids);
// Merge
$all_participants = array_merge($all_participants, $page_participants);
$page_index++;
}
// As the function getTwitterData retrives a maximum of 100 records, it may happend that if we have
// 610 ids and we request 550, we will get 600 instead (es multiplo de 100), so we need to slice the array in that case
if(count($all_participants)>$all_participants_qty)
{
$all_participants_aux = array_slice($all_participants, 0, $all_participants_qty);
$all_participants = $all_participants_aux;
}
}
return $all_participants;
}
function _tweetExists($tweet_id)
{
$twitter = initTwitter();
$tweet_object = $twitter->tweetExists($tweet_id);
return $tweet_object;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment