Show DevBest Script to Download from Twitch.TV

Vinnie

Member
Dec 20, 2012
287
33
See it live here:

Code:
Example URL: http://nl.twitch.tv/jeffroiscool/b/299079587
<form action="twitch.php" method="POST">
Put twitch video URL here ->    <input type="textbox" name="url"> <br />
Click convert here ->    <input type="submit" name="submit" value="Convert">
</form>
<?php
if(isset($_POST['submit'])){
/**
* Takes an URL and splits it up into it's protocol, site, and resource parts
*
* Returns an associative array of protocol, port, site, and resource parts
* Note: the URL does not have to have a protocol specified
* example:
* <code>
*    $url = "http://www.foo.com/blog/search?q=bar";
*    $url_parts = getUrlParts($url);
*    // $url_parts will contain the following:
*    // Array
*    // (
*    //    [protocol] => http
*    //    [port] =>
*    //    [site] => www.foo.com
*    //    [resource] => blog/search?q=bar
*    // )
*   
*    $url = "www.foo.com:8080/blog/search?q=bar";
*    $url_parts = getUrlParts($url);
*    // $url_parts will contain the following:
*    // Array
*    // (
*    //    [protocol] =>
*    //    [port] => 8080
*    //    [site] => www.foo.com
*    //    [resource] => blog/search?q=bar
*    // )
* </code>
*
* @param string    The URL that you want to split up
* @return associative array    Array containing the split up parts of the URL
*/
function getUrlParts($url) {
    $result = array();
 
    // Get the protocol, site and resource parts of the URL
    // original url = http://example.com/blog/index?name=foo
    // protocol = http://
    // site = example.com/
    // resource = blog/index?name=foo
    $regex = '#^(.*?//)*([\w\.\d]*)(:(\d+))*(/*)(.*)$#';
    $matches = array();
    preg_match($regex, $url, $matches);
 
    // Assign the matched parts of url to the result array
    $result['protocol'] = $matches[1];
    $result['port'] = $matches[4];
    $result['site'] = $matches[2];
    $result['resource'] = $matches[6];
 
    // clean up the site portion by removing the trailing /
    $result['site'] = preg_replace('#/$#', '', $result['site']);
 
    // clean up the protocol portion by removing the trailing ://
    $result['protocol'] = preg_replace('#://$#', '', $result['protocol']);
 
    return $result;
}
 
$url = getUrlParts($_POST['url']);
$urlparts = $url['resource'];
$resourcearray = explode("/", $urlparts);
 
$videoid = $resourcearray[2];
 
$xmlapiurl = "http://api.justin.tv/api/clip/show/" . $videoid . ".xml";
$xmlcontent = file_get_contents($xmlapiurl);
$xml = simplexml_load_string($xmlcontent);
$json = json_encode($xml);
$xmlarray = json_decode($json,TRUE);
 
$videolink = $xml->xpath('//video_file_url');
$title = $xml->xpath('//title');
$embed = $xml->xpath('//embed_code');
echo "<a href='" . $videolink[0] . "'> Download: " . $title[0] . "</a> <br /><br />" . $embed[0] ;
}
?>
 

Users who are viewing this thread

Top