David Bushong
Résumé
Picture Gallery
Software Tidbits
Links
Contact Me

Software Tidbits

#!/usr/bin/perl
#
# asciitube -- given a youtube url, play it in the current terminal
#
# 2008 - David Bushong <david@bushong.net>
# 

use strict;
use warnings;
use CGI '-oldstyle_urls';
use JSON;
use LWP::UserAgent;
use Getopt::Std;

my %opt;
getopts('hc', \%opt);

die <<EOF if @ARGV != 1 && $ARGV[0] !~ m{^http://www\.youtube\.com/} || $opt{h};
usage: $0 [-c] http://www.youtube.com/...
       -c: color (use cacalib instead of aalib)
EOF

my $url  = $ARGV[0];
my $ua   = LWP::UserAgent->new(max_redirect => 0);

my $res  = $ua->get($url);
die $res->status_line unless $res->is_success;
my $html = $res->content;
die "failed to find swfArgs\n" unless $html =~ /swfArgs = (\{[^}]+\})/;
my $json = $1;
my $args = eval { jsonToObj($json) } || die "couldn't parse json: $json\n";
my $url2 = 'http://www.youtube.com/get_video?' . CGI->new({
  video_id           => $args->{video_id},
  t                  => $args->{t},
  use_get_video_info => 1,
  load_modules       => 1,
})->query_string;

$res = $ua->get($url2);
die "failed to get 303\n" unless $res->code eq '303';
my $flv_url = $res->header('Location') || die "couldn't find Location header\n";

delete $ENV{DISPLAY};
my $out = $opt{c} ? 'caca' : 'aa';
exec('mplayer', '-quiet', '-vo', $out, $flv_url);