[PHP] Getting the TV Show Banners from TheTVDB

Status
Not open for further replies.

Gaurav

Active Member
641
2010
104
0
Made this last night. Quite handy and can be integrated with a blog/forum. Postting the function here. Do update if you have an improved code :)

ATM, the function would search for the TV Show in the TheTVDB database, and if found will return the banner image link uploaded to lulzimg.com

PHP:
function getBanner($title){
    if(preg_match("/[a-zA-Z0-9.]+S[0-9]+E[0-9]+|[A-Za-z0-9.]+20[0-9.]+/", $title, $match)){
        $showname = trim(str_replace(".", " ", $match[0]));
        $showname = trim(preg_replace("/S[0-9]+E[0-9]+/", "", $showname));
        $url = "http://thetvdb.com/api/GetSeries.php?seriesname=".urlencode($showname);
        $page = postHost($url);
        if(preg_match("/graphical\/[a-z0-9A-Z.\-]+/", $page, $match)){
            $img = postHost("http://www.lulzimg.com/upload.php?submit=lulz&url=http://thetvdb.com/banners/$match[0]");
            //echo $img;
            preg_match_all("/http:\/\/lulzimg.com\/[a-z0-9]+\/[a-z0-9A-Z.]+/", $img, $matches);
            $imageUrl = $matches[0][1];
            return $imageUrl;
        }
    }
}
Please do note that the code above considers that the title of the show is in Scene Release format..

Also note, that the postHost function is a simple function which uses curl to post requests to TheTVDB. UTL and AutoUL users have that function in the script.

Ex:
The.Best.Thing.I.Ever.Ate.S03E03.Regional.Favorites.HDTV.XviD-MOMENTUM

Hope that helps.

Regards,
gunda316
 
19 comments
Here is the whole running PHP code. If u dont know how to make it work after seeing gundas post. I have just added the wrapper around the function (y) Gunda


PHP:
<?
function getBanner($title)
{
    if(preg_match("/[a-zA-Z0-9.]+S[0-9]+E[0-9]+|[A-Za-z0-9.]+20[0-9.]+/", $title, $match)){
        $showname = trim(str_replace(".", " ", $match[0]));
        $showname = trim(preg_replace("/S[0-9]+E[0-9]+/", "", $showname));
        $url = "http://thetvdb.com/api/GetSeries.php?seriesname=".urlencode($showname);
        $page = postHost($url, "", "");
        if(preg_match("/graphical\/[a-z0-9A-Z.\-]+/", $page, $match)){
            $img = postHost("http://www.lulzimg.com/upload.php?submit=lulz&url=http://thetvdb.com/banners/$match[0]", "", "");
            //echo $img;
            preg_match_all("/http:\/\/lulzimg.com\/[a-z0-9]+\/[a-z0-9A-Z.]+/", $img, $matches);
            $imageUrl = $matches[0][1];
            return $imageUrl;
        }
    }
} 
function postHost($url)
{
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_COOKIESESSION, 1);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
	curl_setopt($ch, CURLOPT_TIMEOUT, 60);
	curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)");
	$source_code = curl_exec($ch);
	curl_close($ch);
	return $source_code;
}
if (empty($_POST))
{
?>
<form method=post>
<input type="text" name="img" />
<input type="Submit" value="Submit" />
</form>
<?PHP
}
else
{
$img= getBanner($_POST['img']);
echo "<B>$img</B><BR/><BR/>";
echo "<img src=\"$img\" />";
}
?>

If you dont know how to run it than plz dont use it :|

Note: Not all the release name would work. Most of them work tho :)

Thanks Gunda u are charm <3
 
Here is the whole running PHP code. If u dont know how to make it work after seeing gundas post. I have just added the wrapper around the function (y) Gunda


PHP:
<?
function getBanner($title)
{
    if(preg_match("/[a-zA-Z0-9.]+S[0-9]+E[0-9]+|[A-Za-z0-9.]+20[0-9.]+/", $title, $match)){
        $showname = trim(str_replace(".", " ", $match[0]));
        $showname = trim(preg_replace("/S[0-9]+E[0-9]+/", "", $showname));
        $url = "http://thetvdb.com/api/GetSeries.php?seriesname=".urlencode($showname);
        $page = postHost($url, "", "");
        if(preg_match("/graphical\/[a-z0-9A-Z.\-]+/", $page, $match)){
            $img = postHost("http://www.lulzimg.com/upload.php?submit=lulz&url=http://thetvdb.com/banners/$match[0]", "", "");
            //echo $img;
            preg_match_all("/http:\/\/lulzimg.com\/[a-z0-9]+\/[a-z0-9A-Z.]+/", $img, $matches);
            $imageUrl = $matches[0][1];
            return $imageUrl;
        }
    }
} 
function postHost($url)
{
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_COOKIESESSION, 1);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
	curl_setopt($ch, CURLOPT_TIMEOUT, 60);
	curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)");
	$source_code = curl_exec($ch);
	curl_close($ch);
	return $source_code;
}
if (empty($_POST))
{
?>
<form method=post>
<input type="text" name="img" />
<input type="Submit" value="Submit" />
</form>
<?PHP
}
else
{
$img= getBanner($_POST['img']);
echo "<B>$img</B><BR/><BR/>";
echo "<img src=\"$img\" />";
}
?>

If you dont know how to run it than plz dont use it :|

Note: Not all the release name would work. Most of them work tho :)

Thanks Gunda u are charm <3
Thnx humour.. I was too lazy to add that xD
 
For uploading image to imgur use this replacement

PHP:
function getBanner($title){
    if(preg_match("/[a-zA-Z0-9.]+S[0-9]+E[0-9]+|[A-Za-z0-9.]+20[0-9.]+/", $title, $match)){
        $showname = trim(str_replace(".", " ", $match[0]));
        $showname = trim(preg_replace("/S[0-9]+E[0-9]+/", "", $showname));
        $url = "http://thetvdb.com/api/GetSeries.php?seriesname=".urlencode($showname);
        $page = postHost($url);
        if(preg_match("/graphical\/[a-z0-9A-Z.\-]+/", $page, $match)){
            $img = postHost("http://api.imgur.com/2/upload?url=http://thetvdb.com/banners/$match[0]");
            //echo $img;
            preg_match_all("/http:\/\/i.imgur.com\/[a-z0-9A-Z.]+/", $img, $matches);
            $imageUrl = $matches[0][1];
            return $imageUrl;
        }
    }
}
 
Here is the whole running PHP code. If u dont know how to make it work after seeing gundas post. I have just added the wrapper around the function (y) Gunda


PHP:
<?
function getBanner($title)
{
    if(preg_match("/[a-zA-Z0-9.]+S[0-9]+E[0-9]+|[A-Za-z0-9.]+20[0-9.]+/", $title, $match)){
        $showname = trim(str_replace(".", " ", $match[0]));
        $showname = trim(preg_replace("/S[0-9]+E[0-9]+/", "", $showname));
        $url = "http://thetvdb.com/api/GetSeries.php?seriesname=".urlencode($showname);
        $page = postHost($url, "", "");
        if(preg_match("/graphical\/[a-z0-9A-Z.\-]+/", $page, $match)){
            $img = postHost("http://www.lulzimg.com/upload.php?submit=lulz&url=http://thetvdb.com/banners/$match[0]", "", "");
            //echo $img;
            preg_match_all("/http:\/\/lulzimg.com\/[a-z0-9]+\/[a-z0-9A-Z.]+/", $img, $matches);
            $imageUrl = $matches[0][1];
            return $imageUrl;
        }
    }
} 
function postHost($url)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_COOKIESESSION, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
    curl_setopt($ch, CURLOPT_TIMEOUT, 60);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)");
    $source_code = curl_exec($ch);
    curl_close($ch);
    return $source_code;
}
if (empty($_POST))
{
?>
<form method=post>
<input type="text" name="img" />
<input type="Submit" value="Submit" />
</form>
<?PHP
}
else
{
$img= getBanner($_POST['img']);
echo "<B>$img</B><BR/><BR/>";
echo "<img src=\"$img\" />";
}
?>
If you dont know how to run it than plz dont use it :|

Note: Not all the release name would work. Most of them work tho :)

Thanks Gunda u are charm <3

Any idea on how to make this script work on cli?
 
PHP:
function getBanner($title){
    if(preg_match("/[a-zA-Z0-9.]+S[0-9]+E[0-9]+|[A-Za-z0-9.]+20[0-9.]+/", $title, $match)){
        $showname = trim(str_replace(".", " ", $match[0]));
        $showname = trim(preg_replace("/S[0-9]+E[0-9]+/", "", $showname));
        $url = "http://thetvdb.com/api/GetSeries.php?seriesname=".urlencode($showname);
        $page = postHostBan($url);
        //echo("<br />".$page."<br /><br />");
        if(preg_match("/graphical\/[a-z0-9A-Z.\-]+/", $page, $match)){
            $pic = $match[0];
            $img =  postHost("http://imgclub.org/?url=http://cache.thetvdb.com/banners/$pic", "", "");
            preg_match("/http:\/\/imgclub.org\/images\/[A-Za-z0-9.]+.jpg/", $img, $match);
            $imageUrl = $match[0];
            return $imageUrl;
        }
    }
}  

function postHostBan($url)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_COOKIESESSION, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
    curl_setopt($ch, CURLOPT_TIMEOUT, 60);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)");
    $source_code = curl_exec($ch);
    curl_close($ch);
    return $source_code;
}

Uploads to IMGClub and fixed it a bit. :)
 
Status
Not open for further replies.
Back
Top