Home > Linux, Programming, Useful commands > How to download archived videos from twitch.tv/justin.tv

How to download archived videos from twitch.tv/justin.tv

Working source here.

This php script will download all archived videos from twitch.tv (and probably from justin.tv – because there is same api as on twitch) using wget. All videos would be saved to download folder (create it first) with date as a name and ‘show title’. File name is limited to 50 chars.

Downloading is done by parsing json files from justin, more info: Channel/archives api wiki.

<?php
$url = "http://api.justin.tv/api/channel/archives/xxxx.json?limit=100"; //where xxxx is a channel id or login
$c = curl_init($url);
$options = array(
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => array('Content-type: application/json')
);
curl_setopt_array( $c, $options );
$json = curl_exec($c);
$json_a = json_decode($json,true);
$json_a = array_reverse($json_a);
 
foreach($json_a as $k => $v) {
    if(!history_check(format_date($v['created_on']))){
        exec('wget -c '.$v['video_file_url'].' -O "downloads/'.format_date($v['created_on']).' - '.format_title($v['title']).'.flv"', $array);
        history_add(format_date($v['created_on']));
        echo implode('"\n"', $array);
    }
}
 
function format_date($date){
    $out = preg_replace('/[^0-9\.]/', '', $date);
    return $out;
}
function format_title($title){ //format title and limit to 50 chars
    $out = preg_replace('/[^a-zA-Z \.]/','', $title);
    $out = substr($out, 0, 50);
    return $out;
}
function history_check($date){
    $data = file_get_contents('history');
 
    if(strpos($data, $date) !== FALSE)
    {
        $out = TRUE;
    }
    else
    {
        $out = FALSE;
    }
return $out;
}
 
function history_add($date){
    file_put_contents('history', $date."\n", FILE_APPEND);
}
?>

Usage:
Create download dir, save this source as get.php file and simply run it from command line (php-cli need to be installed):

php get.php

Take in mind that information about downloaded videos is stored in history file. This will avoid downloading videos which was downloaded before.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: