Example #1
0
function getTopFBPosts($account, $count = 10)
{
    $start = tryGET('start');
    $end = tryGet('end');
    $token = getFBToken();
    if (!isset($token[0])) {
        DoNotCache("Facebook: Authentication failed");
        return null;
    }
    //No token
    $url = "https://graph.facebook.com/{$account}/posts";
    $params = array($token[0] => $token[1], "limit" => 100, "fields" => "likes.limit(1).summary(true),shares,actions", "since" => $start, "until" => $end);
    $result = getAPI($url, $params);
    if (!isset($result->data)) {
        return array();
    }
    $rdata = $result->data;
    usort($rdata, 'fbSort');
    $posts = array();
    foreach ($rdata as $key => $p) {
        if ($key < $count) {
            array_push($posts, $p);
        } else {
            break;
        }
    }
    return $posts;
}
Example #2
0
function getTopIGMedia($userID, $count = 10)
{
    $start = tryGET('start');
    $end = tryGET('end');
    $url = "https://api.instagram.com/v1/users/{$userID}/media/recent";
    $params = array("min_timestamp" => $start, "max_timestamp" => $end, "count" => "200", "client_id" => getIGClientID());
    $res = getAPI($url, $params);
    if (!isset($res->data)) {
        return DoNotCache();
    }
    $media = $res->data;
    usort($media, "igSort");
    $result = array();
    foreach ($media as $key => $p) {
        if ($key < $count) {
            array_push($result, $p);
        } else {
            break;
        }
    }
    return $result;
}
Example #3
0
function getTweetsByDate($user, $start = 0, $end = 0)
{
    if (empty($user)) {
        return null;
    }
    $token = getTwitterToken();
    $headers = array("Authorization: Bearer " . $token);
    $getTweets = array();
    if ($start == 0) {
        $start = time();
    }
    if ($end == 0) {
        $end = time();
    }
    $max_id = null;
    //First get each tweet segment needed
    do {
        $params = array("count" => 200, "trim_user" => 1, "exclude_replies" => 1, "include_rts" => 0, "user_id" => $user, "max_id" => $max_id);
        $url = "https://api.twitter.com/1.1/statuses/user_timeline.json";
        $tweets = getAPI($url, $params, $headers);
        $lastDate = -1;
        if (isset($tweets->errors)) {
            DoNotCache();
            continue;
        }
        //Skip twitter calls that result in an error
        if (isset($tweets->curl_error)) {
            DoNotCache();
            continue;
        }
        //Skip for curl errors too
        $len = count($tweets);
        $last = $tweets[$len - 1];
        $lastDate = strtotime($last->created_at);
        $max_id = $last->id_str;
        $getTweets = array_merge($getTweets, $tweets);
    } while ($lastDate > $start);
    //Trim the rear
    foreach (array_reverse($getTweets, TRUE) as $k => $t) {
        if (strtotime($t->created_at) >= $start) {
            break;
        } else {
            unset($getTweets[$k]);
        }
    }
    //Trim the front
    foreach ($getTweets as $k => $t) {
        if (strtotime($t->created_at) <= $end) {
            break;
        } else {
            unset($getTweets[$k]);
        }
    }
    $getTweets = array_values($getTweets);
    return $getTweets;
}
Example #4
0
function getTopDeviations($account = null, $count = null)
{
    $pcount = $count;
    $qcount = tryGET('count');
    $vcount = 10;
    if (isset($pcount)) {
        $vcount = $pcount;
    } else {
        if (isset($qcount)) {
            $vcount = $qcount;
        }
    }
    if ($vcount > 100) {
        $vcount = 100;
    }
    $settings = getSettings();
    if (!isset($account)) {
        $account = $settings["Account"];
    }
    $start = tryGET('start');
    $end = tryGET('end');
    if (!isset($start) || !isset($end)) {
        return null;
    }
    $ndays = getDays($start, $end);
    $start = GoogleDate($start);
    $end = GoogleDate($end);
    $analytics = getAnalytics();
    $filter = "";
    //More than 1 pageview an hour to cut down on outliers and processing
    $dims = "ga:hostname,ga:pagePath,ga:date";
    $metric = "ga:pageviews";
    $sort = "-ga:pageviews";
    $count = 10000;
    //max
    $data = runQuery($analytics, $account, $start, $end, $metric, $dims, $sort, $count, $filter);
    if (isset($data->ga_error)) {
        return DoNotCache();
    }
    $data = $data->getRows();
    $values = array();
    $path = '';
    $tvals = array();
    foreach ($data as $key => $row) {
        if (!isset($values[$row[0] . $row[1]])) {
            $values[$row[0] . $row[1]] = array();
        }
        array_push($values[$row[0] . $row[1]], floatval($row[3]));
    }
    foreach ($values as $key => $val) {
        $rem = $ndays - count($val);
        for ($i = 0; $i < $rem; $i++) {
            array_push($val, 0);
        }
        $mean = mean($val);
        if ($mean < 1) {
            continue;
        }
        //Aviod super low page averages
        $sd = stdev($mean, $val);
        if ($sd == 0) {
            continue;
        }
        //Let's not deal with how this is even possible for right now
        $stdevs[$key] = array('mean' => $mean, 'stdev' => $sd, 'values' => $val);
    }
    $count = 100 * $vcount;
    $filter = "";
    $dims = "ga:date,ga:hour,ga:hostname,ga:pagePath,ga:pageTitle";
    $metric = "ga:pageviews";
    $sort = "-ga:pageviews";
    $data = runQuery($analytics, $account, $start, $end, $metric, $dims, $sort, $count, $filter);
    if (isset($data->ga_error)) {
        return DoNotCache();
    }
    $data = $data->getRows();
    $result = array();
    foreach ($data as $key => $row) {
        $path = $row[2] . $row[3];
        if (!isset($stdevs[$path])) {
            continue;
        }
        //if($sd['mean'] <= 0) continue;
        $sd = $stdevs[$path];
        $z = zscore($sd['stdev'], $sd['mean'], $row[5]);
        $y = substr($row[0], 0, 4);
        $m = substr($row[0], 4, 2);
        $d = substr($row[0], 6, 2);
        $time = "{$y}-{$m}-{$d} " . $row[1] . ":00";
        $ts = strtotime($time);
        $result[] = array('path' => $path, 'title' => $row[4], 'mean' => $sd['mean'], 'stdev' => $sd['stdev'], 'pageviews' => $row[5], 'z' => $z, 'timestamp' => $ts, "time" => $time, "values" => $sd['values']);
    }
    usort($result, "zsort");
    $ret = array_splice($result, 0, $vcount);
    return $ret;
}