Written October 17, 2008 in Note-To-Self

To track cheating on the NapierLife Challenge, I wrote this script to take an hourly “snapshot” of the view-counts of all a users’ videos:

Of course, you need the Zend GData API

[code language=php]
#!/usr/bin/php

// load required libraries
require_once 'Zend/Loader.php'; // the Zend dir must be in your include_path
require_once "DB.php";

Zend_Loader::loadClass('Zend_Gdata_YouTube');
$yt = new Zend_Gdata_YouTube();

$dsn = "mysql://kiwification:kiwification@localhost/kiwification_cheatbuster";
$conn =& DB::connect ($dsn);
if (DB::isError ($conn)) die ("Cannot connect: " . $conn->getMessage () . "\n");

$username='NapierLife';
$debug = false;
$daily_timestamp = date("Y-m-d 00:00:00");
$hourly_timestamp = date("Y-m-d H:00:00");

// purge any old records - we'll replace them
$daily_query = "DELETE FROM log_daily WHERE log_daily_timestamp = '$daily_timestamp'";
$hourly_query = "DELETE FROM log_hourly WHERE log_hourly_timestamp = '$hourly_timestamp'";
$result =& $conn->query($daily_query);
$result =& $conn->query($hourly_query);

// all the videos start with the same text, we'll strip it out
$text_to_strip = 'This is My Life, Join Me! - ';

// construct a video query
$query = $yt->newVideoQuery();
$query->setOrderBy('viewCount');
$query->SetAuthor($username);
$query->setMaxResults(50);
$query->setStartIndex(0);

$videos_to_watch = $yt->getVideoFeed($query);

foreach ($videos_to_watch as $video) {
$title = substr($video->getVideoTitle(),strlen($text_to_strip));
$views = $video->getVideoViewCount();
$id = $video->getVideoId();
if ($debug) print "$id ($title) has $views views\n";

// daily log
$query = "INSERT into log_daily VALUES ('$daily_timestamp','$id','$title','$views')";
$result =& $conn->query($query);
if (DB::isError ($result)) die ("INSERT failed ($query): " . $result->getMessage () . "\n");

// hourly log
$query = "INSERT into log_hourly VALUES ('$hourly_timestamp','$id','$title','$views')";
$result =& $conn->query($query);
if (DB::isError ($result)) die ("INSERT failed ($query): " . $result->getMessage () . "\n");

}

// construct a second video query
$query = $yt->newVideoQuery();
$query->setOrderBy('viewCount');
$query->SetAuthor($username);
$query->setMaxResults(50);
$query->setStartIndex(49);

$videos_to_watch = $yt->getVideoFeed($query);

foreach ($videos_to_watch as $video) {
$title = substr($video->getVideoTitle(),strlen($text_to_strip));
$views = $video->getVideoViewCount();
$id = $video->getVideoId();
if ($debug) print "$id ($title) has $views views\n";

// daily log
$query = "INSERT into log_daily VALUES ('$daily_timestamp','$id','$title','$views')";
$result =& $conn->query($query);
if (DB::isError ($result)) die ("INSERT failed ($query): " . $result->getMessage () . "\n");

// hourly log
$query = "INSERT into log_hourly VALUES ('$hourly_timestamp','$id','$title','$views')";
$result =& $conn->query($query);
if (DB::isError ($result)) die ("INSERT failed ($query): " . $result->getMessage () . "\n");
}

?>
[/code]

No related posts.

No Comments

(Required)
(Required, will not be published)
CommentLuv Enabled