services-posters/php/library.php

527 lines
12 KiB
PHP
Executable File

<?php
$id = $_GET['id'];
if (strlen($id) < 7)
$id = str_pad($id, 7, '0', STR_PAD_LEFT);
$size = $_GET['size'];
if ($size != 'large')
$size = 'small';
$imagetype = array('gif', 'jpeg', 'jpg', 'png');
function downloadPosters() {
global $imagetype;
set_time_limit(0);
echo '<tt>';
// getCriterionPosters();
$html = file_get_contents('html/0xdb.html');
$string = explode('id="iconPoster', $html);
for ($i = 1; $i < count($string); $i++) {
$substring = explode('"', $string[$i]);
$id = $substring[0];
if (strlen($id) == 7 || strlen($id) == 34) {
$file_exists = false;
foreach ($imagetype as $value) {
if (file_exists('posters/wikipedia.org/' . substr($id, 0, 4) . '/' . $id . '.' . $value)) {
$file_exists = true;
break;
}
if (file_exists('posters/wikipedia.org/' . substr($id, 0, 4) . '/_' . $id . '.' . $value)) {
$file_exists = true;
break;
}
}
if (!$file_exists) {
$file = getWikipedia($id);
if ($file)
echo 'writing ' . $id . '<br />';
}
else
echo 'reading ' . $id . '<br />' . str_repeat(' ', 10000);
$file_exists = false;
foreach ($imagetype as $value) {
if (file_exists('posters/imdb.com/' . substr($id, 0, 4) . '/' . $id . '.' . $value)) {
$file_exists = true;
break;
}
}
if (!$file_exists)
getImdb($id);
if (!file_exists('posters/0xdb.org/' . substr($id, 0, 4) . '/' . $id . '.jpg'))
get0xdb($id);
flush(); ob_flush();
}
}
// getImpawardsIndex();
}
function returnPoster($id, $size) {
if ($size == 'small')
$source = array('piratecinema', 'karagarga', 'criterion', 'wikipedia', 'impawards', 'imdb', '0xdb');
else
$source = array('piratecinema', 'karagarga', 'criterion', 'wikipedia', 'impawards');
foreach ($source as $value) {
$function = get . ucfirst($value);
$file[$value] = $function($id);
// echo $value . time() . ' ';
}
foreach ($source as $value) {
if ($file[$value] && substr($file[$value], 0, 1) != '_')
returnImage($id, $size, $file[$value]);
}
// returnImage($id, $size, 'png/poster' . ucfirst($size) . '.png');
if ($size == 'small')
returnImage($id, 'small', 'posters/0xdb.org/' . substr($id, 0, 4) . '/' . $id . '.png');
else
returnImage($id, 'large', 'png/posterLarge.png');
}
function getTitle($id) {
$html = getUrl('http://www.imdb.com/title/tt' . $id . '/');
$string = explode('<title>', $html);
$string = explode('</title>', $string[1]);
$string = explode(' (', $string[0]);
$title = html_entity_decode($string[0]);
return $title;
}
function getImdb($id) {
global $imagetype;
foreach ($imagetype as $value) {
$file = 'posters/imdb.com/' . substr($id, 0, 4) . '/' . $id . '.' . $value;
if (file_exists($file))
return $file;
}
$html = getUrl('http://www.imdb.com/title/tt' . $id . '/');
$string = explode('<a name="poster"', $html);
$string = explode('src="', $string[1]);
$string = explode('"', $string[1]);
$url = $string[0];
if ($url && $url != 'http://ia.imdb.com/media/imdb/01/I/44/47/29.gif') {
$data = getUrl($url);
$pathinfo = pathinfo($url);
$extension = strtolower($pathinfo['extension']);
$file = 'posters/imdb.com/' . substr($id, 0, 4) . '/' . $id . '.' . $extension;
// die($url . ' ' . $file);
writeFile($file, $data);
return $file;
}
}
function get0xdb($id) {
$file = 'posters/0xdb.org/' . substr($id, 0, 4) . '/' . $id . '.png';
/*
if (file_exists($file))
return $file;
*/
// the following is used to remove broken images from the db
if (file_exists($file)) {
$image = @imagecreatefrompng($file);
if ($image)
return $file;
else
unlink($file);
}
$data = file_get_contents('http://rlx:luxor@services.0xdb.org/posters/0xdbPoster.php?id=' . $id);
writeFile($file, $data);
$image = @imagecreatefrompng($file);
if ($image)
return $file;
else {
unlink($file);
return 'png/posterSmall.png';
}
}
function getWikipedia($id) {
global $imagetype;
foreach ($imagetype as $value) {
$file = 'posters/wikipedia.org/' . substr($id, 0, 4) . '/' . $id . '.' . $value;
if (file_exists($file))
return $file;
$file = 'posters/wikipedia.org/' . substr($id, 0, 4) . '/_' . $id . '.' . $value;
if (file_exists($file))
return false;
}
$title = getTitle($id);
$url = 'http://www.google.com/search?q=' . urlencode('site:en.wikipedia.org "' . $title . '"');
$html = getUrl($url);
$string = explode('" class=l>', $html);
for ($i = 0; $i < count($string) - 1; $i++) {
$substring = explode('<a href="', $string[$i]);
$url = $substring[count($substring) - 1];
$html = getUrl($url);
if (strstr($html, 'imdb.com/title/tt' . $id)) {
// echo "tt ok<br>";
if (strstr($html, '"/wiki/Image:')) {
// echo "image ok<br>";
$string = explode('"/wiki/Image:', $html);
$string = explode('"', $string[1]);
$url = 'http://en.wikipedia.org/wiki/Image:' . $string[0];
$html = getUrl($url);
$string = explode('src="http://upload.wikimedia.org/', $html);
$string = explode('"', $string[1]);
$url = 'http://upload.wikimedia.org/' . $string[0];
$pathinfo = pathinfo($url);
$extension = strtolower($pathinfo['extension']);
$imagesize = getimagesize($url);
if ($imagesize[0] > 128 || $imagesize[1] > 128) {
$file = 'posters/wikipedia.org/' . substr($id, 0, 4) . '/' . $id . '.' . $extension;
$data = getUrl($url);
writeFile($file, $data);
// echo "$file $title\n";
return $file;
}
}
break;
}
}
return false;
}
function getImpawards($id) {
global $imagetype;
foreach ($imagetype as $value) {
$file = 'posters/impawards.com/' . substr($id, 0, 4) . '/' . $id . '.' . $value;
if (file_exists($file))
return $file;
}
}
function getCriterion($id) {
global $imagetype;
foreach ($imagetype as $value) {
$file = 'posters/criterion.com/' . substr($id, 0, 4) . '/' . $id . '.' . $value;
if (file_exists($file))
return $file;
}
}
function getKaragarga($id) {
global $imagetype;
foreach ($imagetype as $value) {
$file = 'posters/karagarga.net/' . substr($id, 0, 4) . '/' . $id . '.' . $value;
if (file_exists($file))
return $file;
}
}
function getPiratecinema($id) {
global $imagetype;
foreach ($imagetype as $value) {
$file = 'posters/piratecinema.org/' . substr($id, 0, 4) . '/' . $id . '.' . $value;
if (file_exists($file))
return $file;
}
}
function returnImage($id, $size, $file) {
$pathinfo = pathinfo($file);
$extension = $pathinfo['extension'];
if ($extension == 'gif')
$source = imagecreatefromgif($file);
else if ($extension == 'png')
$source = imagecreatefrompng($file);
else
$source = imagecreatefromjpeg($file);
if ($size == 'small')
$size = 128;
else
$size = 512;
$imagesize = getimagesize($file);
if ($imagesize[0] > $imagesize[1]) {
$width = $size;
$height = round($width * $imagesize[1] / $imagesize[0]);
}
else {
$height = $size;
$width = round($height * $imagesize[0] / $imagesize[1]);
}
$target = imagecreatetruecolor($width, $height);
imagecopyresampled($target, $source, 0, 0, 0, 0, $width, $height, $imagesize[0], $imagesize[1]);
if (strstr($file, 'png/poster')) {
header('Content-Type: image/png');
header('Content-Disposition: inline; filename="' . $id . '.png"');
imagepng($target);
}
else {
header('Content-Type: image/jpeg');
header('Content-Disposition: inline; filename="' . $id . '.jpg"');
imagejpeg($target, NULL, 100);
}
exit;
}
function getUrl($url) {
$url = parse_url($url);
$host = $url['host'];
$file = $url['path'] . str_repeat('?', $url['query'] != '') . $url['query'];
// echo $host . ' ' . $file . '<br />';
$headers = "GET $file HTTP/1.1\r\n";
$headers .= "Accept: */*\r\n";
$headers .= "Accept-Language: en\r\n";
// $headers .= "Accept-Encoding: gzip, deflate\r\n";
$headers .= "User-Agent: Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/418 (KHTML, like Gecko) Safari/417.9.3\r\n";
$headers .= "Connection: close\r\n";
$headers .= "Host: $host\r\n\r\n";
$f = @fsockopen($host, 80, $errorNumber, $errorString, 60);
if ($f) {
fwrite($f, $headers);
while (!feof($f))
$data .= fread($f, 1024);
fclose($f);
$data = substr($data, strpos($data, "\r\n\r\n") + 4);
}
return $data;
}
function writeFile($file, $data) {
$pathinfo = pathinfo($file);
$dirname = $pathinfo['dirname'];
$filename = $dirname . '/' . $pathinfo['basename'];
$string = explode('/', $dirname);
for ($i = 0; $i < count($string); $i++) {
if ($i > 0)
$subdirname .= '/';
$subdirname .= $string[$i];
if (!file_exists($subdirname))
mkdir($subdirname, 0777);
}
@file_put_contents($filename, $data);
@chmod($filename, 0666);
}
function getCriterionPosters() {
$html = getUrl('http://www.criterion.com/asp/browse.asp?sort=spine');
$string = explode('height="20" valign="top" class="regtext"><a href="release.asp?id=', $html);
for ($i = 1; $i , count($string); $i++) {
$substring = explode('class="browse">', $string[$i]);
$substring = explode('<', $substring[1]);
$title = $substring[0];
$substring = explode('class="regtext">', $string[$i]);
$substring = explode('<', $substring[1]);
$director = $substring[0];
$substring = explode('class="regtext">', $string[$i]);
$substring = explode('<', $substring[4]);
$spine = $substring[0];
$url = 'http://www.google.com/search?q=' . urlencode('site:imdb.com "' . $title . '" ' . $director);
$html = getUrl($url);
$id = 0;
$string_ = explode('" class=l>', $html);
for ($i_ = 0; $i_ < count($string_) - 1; $i_++) {
$substring = explode('<a href="', $string_[$i_]);
$url = $substring[count($substring) - 1];
if (strstr($url, 'title/tt')) {
$substring = explode('title/tt', $url);
$id = substr($substring[1], 0, 7);
break;
}
}
if ($id) {
echo $title . ' (' . $director . ') = ' . getTitle($id) . '<br />' . str_repeat(' ', 10000);
$file = 'posters/criterion.com/' . substr($id, 0, 4) . '/' . $id . '.jpg';
if (!file_exists($file)) {
$data = getUrl('http://www.criterion.com/content/images/full_boxshot/' . $spine . '_box_348x490.jpg');
writeFile($file, $data);
}
}
}
}
function getImpawardsIndex($p = 1) {
global $url;
$html = getUrl('http://www.impawards.com/alpha' . $p . '.html');
// echo $html;
for ($y = 19; $y <= 20; $y++) {
$string = explode('href = ' . $y, $html);
for ($i = 1; $i < count($string); $i++) {
$substring = explode('>', $string[$i]);
if (strstr($substring[0], '_ver1') || !strstr($substring[0], '_ver')) {
$key = count($url);
$url[$key] = '/' . $y . $substring[0];
getImpawardsPage($url[$key]);
}
}
}
if (strstr($html, 'href = alpha' . ($p + 1)))
getImpawardsIndex($p + 1);
}
function getImpawardsPage($url) {
$html = getUrl('http://www.impawards.com' . $url);
$string = explode('imdb.com/title/tt', $html);
$string = explode(' ', $string[1]);
$id = str_pad($string[0], 7, '0', STR_PAD_LEFT);
if ($id > 0) {
$dirname = 'posters/impawards.com/' . substr($id, 0, 4);
$filename = $dirname . '/' . $id . '.jpg';
if (strstr($html, '_xlg.html'))
$poster = 'http://www.impawards.com' . substr($url, 0, 5) . '/posters' . substr($url, 5, -5) . '_xlg.jpg';
else
$poster = 'http://www.impawards.com' . substr($url, 0, 5) . '/posters' . substr($url, 5, -5) . '.jpg';
if (!file_exists($filename)) {
if (!file_exists($dirname))
mkdir($dirname, 0777);
$jpg = file_get_contents($poster);
$f = fopen($filename, 'w');
fwrite($f, $jpg);
fclose($f);
chmod($filename, 0666);
}
echo "$id $poster<br />\n";
}
}
function crawlPath($path) {
global $list;
if (substr($path, -1) == '/')
$path = substr($path, 0, -1);
$dir = dir($path);
while ($file = $dir->read()) {
if (substr($file, 0, 1) != '.') {
$file = $path . '/' . $file;
if (is_dir($file))
crawlPath($file);
else
$list[count($list)] = $file;
}
}
return $list;
}
?>