Reddit Analysis
FAQ
  • echo ''; function getposts($target, &$posts, $modhash = NULL, $after = NULL, $count = 0, $calls = 0) { // Create a stream $opts = array( 'http'=>array( 'method'=>"GET", 'header'=>"User-Agent: siegemedia redditbesttime tool", ) ); //append modhash to header if subsequent call, consider persisting through $_POST if ($modhash) { $opts['http']['header'] = $opts['http']['header'] . 'X-Modhash: .' . $modhash; } $context = stream_context_create($opts); $queryopts = array('t' => 'month', 'limit' => '100'); //append fullname of last post retrieved to get posts after if ($after) { $queryopts['after'] = $after; } //append updated count of posts already retrieved if ($count) { $queryopts['count'] = $count; } $query = http_build_query($queryopts); // initiate, if not array if (!is_array($posts)) $posts = array();// build subreddit URL from user inputted name $url = "http://www.reddit.com/r/".$target."/top.json?".$query; //get .json data, suppressing erros $json = @file_get_contents($url, false, $context); $calls++; if ($json === FALSE) { $emptyreturn = true; } else { //decode .json data $data = json_decode($json); //parse .json data $modhash = $data->data->modhash; if (empty($data->data->children)) { $emptyreturn = true; } else { $emptyreturn = false;foreach ($data->data->children as $postdata) { if ($postdata->kind == 't3') { #echo var_dump($postsdata); $after = $postdata->data->name; # push array of created_utc and score onto posts array $posts[]= [ $postdata->data->created_utc, $postdata->data->score, $after ]; } } } } if (empty($posts)) { echo "

    Subreddit $target not found :[

    "; } else { $count = count($posts); // return if no results, more than 5 calls, or 500+ posts retrieved if ($emptyreturn || $calls > 5 || $count >= 500) { echo ""; } else { echo ""; getposts($target, $posts, $modhash, $after, $count, $calls); } } } # end function getposts if(isset($_POST['subreddit'])) { $subreddit = htmlentities($_POST['subreddit']); getposts($subreddit, $formattedposts); } else { $subreddit = ''; }echo '
    '; echo "

    Choose a subreddit to see when the most successful posts are submitted.

    "; echo '
    '; echo '
    '; if (! empty($formattedposts)) { echo '

    NUMBER OF TOP POSTS BY DAY & HOUR (YOUR TIMEZONE)

    '; } echo '
    '; if (! empty($formattedposts)) { echo count($formattedposts) . " posts analyzed, minimum " . end(array_values($formattedposts))[1] . " votes"; } echo '
    ";
    • How does this work?

      We retrieve the top 500 posts for the current month, and group them by day and hour. Then we pick the three day and hour combinations with the most top posts as our recommendation. We do this as well for day without the hour and the hour without the day.