<?php /* -*- C++ -*- */

/**
 * YTMND API Example
 *
 * Parses a YTMND API PHPS response into a Timeplot (http://simile.mit.edu/timeplot/)
 * edible data format (CSV).
 *
 * This is purely an example of an API use and was coded in a way to make it easier to
 * understand, so it probably will use more memory and be slower than it should.
 *
 * @author max goldberg <nax@ytmnd.com>
 * $Id: views_data.php 1384 2010-02-23 16:06:17Z max $
 */

header('Content-type: text/plain');

if (!isset(
$_GET['site_id'])) {
  
$site_id 1;
}
else {
  
$site_id intval($_GET['site_id']);

  if (
$site_id == 0) {
    
$site_id 1;
  }
}

$api_url 'http://api.ytmnd.com/site/' $site_id '/views_by_day+votes_by_day+favorites+base?return=phps';

$serialized_data file_get_contents($api_url);

$data unserialize($serialized_data);

echo 
"##############################################\r\n";
echo 
"# timeplot csv format for the ytmnd api      #\r\n";
echo 
"#                                            #\r\n";
echo 
"# format is:                                 #\r\n";
echo 
"# date, views, votes, score, # of favorites  #\r\n";
echo 
"#                                            #\r\n";
echo 
"# line1 is site_id, domain, title            #\r\n";
echo 
"##############################################\r\n";



if (!isset(
$data['sites'][$site_id])) {
  die(
"invalid site\r\n");
}
else {

  
/**
   * We use the first line to send the page domain and title info, since it's a CSV we
   * temporarily replace commas with another string that we assume won't ever be used.
   *
   * As soon as this example is published, someone will make a site called #COMMA# just
   * to be an idiot.
   */

  
$js_title_hack str_replace(',''#COMMA#'$data['sites'][$site_id]['title']);
  echo 
"$site_id," $data['sites'][$site_id]['domain'] . ',' $js_title_hack "\r\n";
}

$views     =& $data['sites'][$site_id]['site_views'];
$score     =& $data['sites'][$site_id]['score'];
$favorites =& $data['sites'][$site_id]['favorites'];

// filter out irrelevant items that will get in the way of our loops later on.

unset($views['today'], $views['yesterday'], $views['week'], $views['month'], $views['all_time']);
unset(
$score['computed_score'], $score['total_votes'], $score['vote_sum'], $score['score_image']);
unset(
$favorites['total_favorites']);


/**
 * We go through each data type (votes/score, favorites, views) and
 * format data into an array of date => data format because we might have
 * some view data for one date but no vote info or vice versa.
 */

$csv_data = array();

/**
 * grab views
 */

foreach ($views as $view) {
  
$csv_data[$view['date']]['views'] = $view['view_count'];
}


/**
 * grab score and vote counts
 */

foreach ($score as $date => $row) {
  
$csv_data[$date]['votes'] = $row['one_votes']+$row['two_votes']+$row['three_votes']+$row['four_votes']+$row['five_votes'];
  
$csv_data[$date]['score'] = $row['current_score'];
}

/**
 * favorite counter (assumes favorites aren't in order)
 */

$total_favorites   0;

foreach (
$favorites as $favorite) {
  
$date date('Y-m-d'strtotime($favorite['date']));

  ++
$total_favorites;

  if (isset(
$csv_data[$date]['favorites'])) {
    ++
$csv_data[$date]['favorites'];
  }
  else {
    
$csv_data[$date]['favorites'] = intval($total_favorites);
  }
}

/*
 * sort the csv data into asscending order by date (the array key)
 */

ksort($csv_data);
unset(
$data$score$views$favorites);

/**
 * if you wanted to make sure there weren't any holes in your data, or you wanted to do a date range,
 * you could do something like:
 *
 * for ($date = $start_date; $date <= $end_date; $date = date('Y-m-d', strtotime("$date +1day"))) {
 *
 * but timeplot handles spotty data well so we don't need to worry about it.
 *
 * if we don't have information on favorite count or site score, we can assume they haven't changed since
 * the last date we processed, so keep track a note of them.
 */

$last_favorites 0;
$last_score     0;
$output_counter 0;

foreach (
$csv_data as $date => $site_stats) {

  if (!isset(
$site_stats['score'])) {
    
$score $last_score;
  }
  else {
    
$score $last_score $site_stats['score'];
  }

  if (!isset(
$site_stats['favorites'])) {
    
$favorites $last_favorites;
  }
  else {
    
$favorites $last_favorites $site_stats['favorites'];
  }

  if (!isset(
$site_stats['views'])) {
    
$views 0;
  }
  else {
    
$views $site_stats['views'];
  }

  if (!isset(
$site_stats['votes'])) {
    
$votes 0;
  }
  else {
    
$votes $site_stats['votes'];
  }

  echo 
"$date,$views,$votes,$score,$favorites\r\n";
  ++
$output_counter;
}

echo 
"\r\n";
echo 
"# returned $output_counter results.\r\n";