Skip to content

Instantly share code, notes, and snippets.

@garvinhicking
Created December 6, 2013 12:57
Show Gist options
  • Save garvinhicking/7823333 to your computer and use it in GitHub Desktop.
Save garvinhicking/7823333 to your computer and use it in GitHub Desktop.
Quick and dirty IMDB comparison
<?php
$myself = 'garvin';
$compare = 'andih';
# Usage:
# - wget a URL like this:
# http://www.imdb.com/list/export?list_id=ratings&author_id=ur0555872 (garvin)
# - save it as imdb_ratings_XXX.csv (see below when it gets called)
# - do the same with the CSV of the second user you want to compare with
# - enter the proper user names/file names at the bottom of this file
# - point the $compare variable to the XXX username (filename)
# - run this php script
# - read :)
# License: use this code as you like.
error_reporting(E_ALL & ~E_NOTICE);
echo "<html><body>";
function differ($array, $diffcounter, $data) {
$i = 0;
foreach($array AS $key => $sub_array) {
$i++;
if ($i > $diffcounter) {
break;
}
echo "<h2>Difference of " . $key . " pts</h2>\n";
echo "<ul>\n";
ksort($sub_array);
foreach($sub_array AS $myrating => $list) {
$movies = explode(',', $list);
foreach($movies AS $movie) {
if (empty($movie)) continue;
echo "<li>" . $data[$movie][9] . ' VS personal ' . $data[$movie][8] . ": <a href='" . $data[$movie][15] . "'>" . $data[$movie][5] . "</a></li>\n";
}
}
echo "</ul>\n";
}
}
function csv($file) {
$fp = fopen(dirname(__FILE__) . '/' . $file, 'rb');
$data = array();
while($row = fgetcsv($fp)) {
/*
"position","const","created","modified","description","Title","Title type","Directors","You rated","IMDb Rating","Runtime (mins)","Year","Genres","Num. Votes","Release Date (month/day/year)","URL"
[0] => 1568 [position]
[1] => tt0120689 [id]
[2] => Thu Jan 1 00:00:00 1970
[3] =>
[4] =>
[5] => The Green Mile [title]
[6] => Feature Film
[7] => Frank Darabont
[8] => 8 [my rating]
[9] => 8.5 [imdb rating]
[10] => 189
[11] => 1999
[12] => crime, drama, fantasy, mystery
[13] => 362491
[14] => 1999-12-06
[15] => http://www.imdb.com/title/tt0120689/
)
*/
@$data['year'][$row[11]][$row[8]] .= $row[1] . ',';
$data['rating'][$row[1]] = $row[8];
$diff = $row[9] - $row[8];
if ($row[9] > 0) {
@$data['ratingdiff'][$diff][$row[8]] .= $row[1] . ',';
}
$data['movies'][$row[1]] = $row;
}
return $data;
}
function statout($data, $who, $year) {
echo "<h1>Ratings for $who, top of $year:</h1>\n";
echo "<ol>\n";
krsort($data[$who]['year'][$year]);
$i = 0;
$total = 0;
foreach($data[$who]['year'][$year] AS $rating => $movies) {
$i++;
echo "<li>Rating: " . $rating . ":\n";
$movies = explode(',', $movies);
echo "<ul>\n";
foreach($movies AS $movie) {
if (empty($movie)) continue;
$mdat = $data[$who]['movies'][$movie];
echo "<li><a href='" . $mdat[15] . "'>" . $mdat[5] . "</a> (Deviation: " . ($mdat[9] - $mdat[8]) . ")</li>\n";
$total++;
}
echo "</ul></li>\n";
}
echo "</ol>\n";
echo "<p>End: Total of " . $total . " movies rated.</p>\n";
echo "\n";
$max = $min = $data[$who]['ratingdiff'];
ksort($max);
krsort($min);
echo "<h1>Highest differences with IMDB (all-time, $who liked it better than IMDB)</h1>\n";
differ($max, 3, $data[$who]['movies']);
echo "\n";
echo "<h1>Highest differences with IMDB (all-time, $who liked it less than IMDB)</h1>\n";
differ($min, 3, $data[$who]['movies']);
}
function data_compare($mdata, $user1, $user2) {
$data1 = $mdata[$user1];
$data2 = $mdata[$user2];
foreach($data1['movies'] AS $movie => $data) {
if (!isset($data2['movies'][$movie])) {
unset($data1['movies'][$movie]);
if ($data[6] == "TV Episode") {
continue;
}
if ($data[6] == "TV Series") {
echo "$user2 hat TV-Serie <a href='" . $data[15] . "'>" . $data[5] . "</a> nicht gesehen.<br />\n";
} else {
echo "$user2 hat <strong>Film <a href='" . $data[15] . "'>" . $data[5] . "</a></strong> nicht gesehen.<br />\n";
}
}
}
echo "<hr />\n";
foreach($data2['movies'] AS $movie => $data) {
if (!isset($data1['movies'][$movie])) {
unset($data2['movies'][$movie]);
if ($data[6] == "TV Episode") {
continue;
}
if ($data[6] == "TV Series") {
echo "$user1 hat TV-Serie <a href='" . $data[15] . "'>" . $data[5] . "</a> nicht gesehen.<br />\n";
} else {
echo "$user1 hat <strong>Film <a href='" . $data[15] . "'>" . $data[5] . "</a></strong> nicht gesehen.<br />\n";
}
}
}
$out = array();
foreach($data1['movies'] AS $movie => $data) {
$d = $data[8] - $data2['movies'][$movie][8];
$out['diff'][$d] .= $movie . ',';
$out['alltime'][$data[8] + $data2['movies'][$movie][8]] .= $movie . ',';
}
ksort($out['diff']);
krsort($out['alltime']);
echo "<h1>Deviation between $user1 and $user2:</h1>\n";
echo "<ul>\n";
foreach($out['diff'] AS $p => $movies) {
echo "<li>" . $p . " points difference:\n";
echo "<ol>\n";
$movies = explode(',', $movies);
foreach($movies AS $movie) {
if (empty($movie)) continue;
echo "<li>$user1 " . $data1['movies'][$movie][8] . " vs. $user2 " . $data2['movies'][$movie][8] . ": <a href='" . $data1['movies'][$movie][15] . "'>" . $data1['movies'][$movie][5] . "</a></li>\n";
}
echo "</ol></li>\n";
}
echo "</ul>\n";
echo "<h1>Added ratings of $user1 and $user2</h1>\n";
echo "<ul>\n";
foreach($out['alltime'] AS $p => $movies) {
echo "<li>" . $p . " points combined:\n";
echo "<ol>\n";
$movies = explode(',', $movies);
foreach($movies AS $movie) {
if (empty($movie)) continue;
echo "<li>$user1 " . $data1['movies'][$movie][8] . " vs. $user2 " . $data2['movies'][$movie][8] . ": <a href='" . $data1['movies'][$movie][15] . "'>" . $data1['movies'][$movie][5] . "</a></li>\n";
}
echo "</ol></li>\n";
}
echo "</ul>\n";
}
$data[$myself'] = csv('imdb_ratings_' . $myself . '.csv');
statout($data, $myself, 2013);
$data[$compare] = csv('imdb_ratings_' . $compare . '.csv');
statout($data, $compare, 2013);
data_compare($data, $myself, $compare);
echo "</body></html>";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment