Created
September 24, 2012 15:03
-
-
Save agustinhaller/3776419 to your computer and use it in GitHub Desktop.
max_id stuff
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
function getOrganizerTweets($search_query, $draw_start_date, $draw_end_date, $organizer_username) | |
{ | |
$search_results_aux = array(); | |
$tmhOAuth = initTmhOAuth($organizer_username); | |
// print_str($tmhOAuth, $organizer_username." CREDENTIALS"); | |
// Now perform the search | |
$last_page = 1; | |
$keep_searching = true; | |
$total_results_count = 0; | |
$total_results = array(); | |
$max_id = null; | |
while($last_page <= 20 && $keep_searching) | |
{ | |
$search_args = array(); | |
$search_args['q'] = $organizer_username." ".$search_query;// include user name in order to filter search results | |
$search_args['page'] = $last_page; | |
$search_args['rpp'] = 100; | |
if($max_id != null) | |
{ | |
$search_args['max_id'] = $max_id; | |
} | |
print_str($search_args, "search_args"); | |
$tmhOAuth->request('GET', 'http://search.twitter.com/search.json', $search_args); | |
if($tmhOAuth->response['code'] == 200) | |
{ | |
$data = json_decode($tmhOAuth->response['response'], true); | |
// print_str($data, "search_data1"); | |
// print_str($last_page, "last_page"); | |
// print_str($keep_searching, "keep_searching"); | |
$page_results = $data['results']; | |
$page_results_count = count($page_results); | |
// print_str($page_results_count, "page_results_count"); | |
if($page_results_count>0) | |
{ | |
$total_results_count += $page_results_count; | |
// Reduce data to only needed one | |
$page_results_reduced = array(); | |
foreach($page_results 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['from_user_id_str']; | |
// New data | |
$tweet_user_username = $tweet['from_user']; | |
$tweet_user_name = $tweet['from_user_name']; | |
$tweet_user_profile_image = $tweet['profile_image_url']; | |
// 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)) | |
{ | |
print_str($tweet, "tweet"); | |
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 | |
); | |
// print_str($aux_tweet, "aux_tweet"); | |
$page_results_reduced[] = $aux_tweet; | |
} | |
} | |
// Merge page results with total results | |
$total_results = array_merge($total_results, $page_results_reduced); | |
$last_page++; | |
$keep_searching = true; | |
$max_id = $page_results[$page_results_count-1]['id_str']; | |
} | |
else | |
{ | |
$keep_searching = false; | |
} | |
} | |
else// Twitter call error | |
{ | |
$tw_response = $tmhOAuth->response['response']; | |
$log_text = "Twitter call error when getting users data in function 'getTwitterData' for user"; | |
$log_data = array("case" =>"TWITTER CALL ERROR", | |
"exception"=>"", | |
"response"=>$log_text.$tw_response); | |
// logError($log_data); | |
} | |
$tw_manager = TwitterAPICallManager::getManager(); | |
$tw_manager->newCall(); | |
} | |
// Now we have the complete search space in $total_results, lets return it | |
return $total_results; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment