<?php

//
require 'config.php';
require 'oauth_lib.php';
$connection = get_connection();
require 'db_lib.php';
$db = new db();
// Get all leaders that have had old search results collected
$query = "SELECT user_id, screen_name, search_since_id\r\n\tFROM leaders\r\n\tWHERE old_search_collected != '0000-00-00'";
$results = $db->select($query);
while ($row = mysqli_fetch_assoc($results)) {
    // Run the search on each user
    collect_tweets($row['user_id'], $row['screen_name'], $row['search_since_id']);
}
function collect_tweets($leader_user_id, $leader_screen_name, $since_id)
{
    global $db, $connection;
    while (true) {
        $connection->request('GET', $connection->url('1.1/search/tweets'), array('q' => $leader_screen_name, 'result_type' => 'recent', 'lang' => 'en', 'include_entities' => 'true', 'count' => 100, 'since_id' => $since_id));
        if ($connection->response['code'] != 200) {
            break;
        }
        $tweets_found = 0;
        $results = json_decode($connection->response['response']);
        $tweets = $results->statuses;
        foreach ($tweets as $tweet) {
            ++$tweets_found;
            $tweet_id = $tweet->id;
            $since_id = $tweet_id;
            if ($db->in_table('tweets', "tweet_id={$tweet_id}")) {
<?php

//
require 'config.php';
require 'oauth_lib.php';
$connection = get_connection();
require 'db_lib.php';
$db = new db();
// Get all the leader accounts that have had their old tweets collected
$query = "SELECT user_id \r\n\tFROM leaders\r\n\tWHERE old_timeline_collected != '0000-00-00'";
$results = $db->select($query);
while ($row = mysqli_fetch_assoc($results)) {
    $user_id = $row['user_id'];
    collect_tweets($user_id);
    $db->update('leaders', 'new_timeline_collected=now()', "user_id={$user_id}");
}
// Get all new tweets by this user
function collect_tweets($user_id)
{
    global $db, $connection;
    // Get the highest tweet_id (newest tweet) for this user
    $query = "SELECT max(tweet_id) AS since_id\r\n\t\tFROM tweets\r\n\t\tWHERE user_id = {$user_id}";
    $results = $db->select($query);
    $row = mysqli_fetch_assoc($results);
    $since_id = $row['since_id'];
    while (true) {
        $connection->request('GET', $connection->url('1.1/statuses/user_timeline'), array('user_id' => $user_id, 'include_entities' => 'true', 'include_rts' => 'true', 'exclude_replies' => 'false', 'trim_user' => 'true', 'count' => 100, 'since_id' => $since_id));
        if ($connection->response['response'] == '[]') {
            return;
        }
        if ($connection->response['code'] != 200) {