Skip to content

Instantly share code, notes, and snippets.

@xaicron
Created May 16, 2010 16:05
Show Gist options
  • Select an option

  • Save xaicron/402964 to your computer and use it in GitHub Desktop.

Select an option

Save xaicron/402964 to your computer and use it in GitHub Desktop.
use strict;
use warnings;
use Plack::Builder;
use Plack::Response;
use Plack::Request;
use HTTP::Cookies;
use LWP::UserAgent;
use DBI;
my $conf = do 'conf.pl';
my $ua = LWP::UserAgent->new;
my $dbh = DBI->connect(@{$conf->{connect_info}});
my $app = builder {
sub {
my $req = Plack::Request->new(shift);
if ($req->request_uri =~ m|http://www.youtube.com/watch| && $req->param('v')) {
eval { $dbh->do('INSERT INTO queue (finished, video_id) VALUES(?, ?)', undef, 0, $req->param('v')) };
$ua->agent($req->user_agent);
my $cookies = $req->cookies;
my $cookie_jar = HTTP::Cookies->new;
for my $key (keys %$cookies) {
$cookie_jar->set_cookie(0, $key, $cookies->{$key}, '/', 'www.youtube.com');
}
$ua->cookie_jar($cookie_jar);
my $get_res = $ua->get($req->request_uri);
my $res = Plack::Response->new($get_res->code);
$res->headers($get_res->headers);
$res->body($get_res->content);
return $res->finalize;
}
return [400, ['Content-Type' => 'text/plain'], ['Proxy settings are wrong.']];
};
};
my $data = +{
connect_info => [
'dbi:SQLite:dbname=sqlite.db',
'',
'',
+{
AutoCommit => 1,
RaiseError => 1,
PrintError => 0,
},
],
};
use strict;
use warnings;
use DBI;
my $conf = do 'conf.pl';
my $dbh = DBI->connect(@{$conf->{connect_info}});
$dbh->do('CREATE TABLE queue (
id INTEGER PRIMARY KEY,
finished INT,
video_id VERCHAR,
UNIQUE (video_id)
)');
function FindProxyForURL(url, host) {
var YOUTUBE_PATTERN = new RegExp('^http://www.youtube.com/watch\\?v=[^&]+');
if (YOUTUBE_PATTERN.test(url)) {
return "PROXY localhost:5000";
}
return "DIRECT";
}
#!/usr/bin/env perl
use strict;
use warnings;
use DBI;
use AnyEvent::Impl::Perl;
use AnyEvent;
use Coro::Handle;
use Encode qw/find_encoding decode_utf8/;
use Term::Encoding qw/term_encoding/;
use WWW::YouTube::Download;
my $enc = find_encoding term_encoding;
my $out = unblock \*STDOUT;
$out->syswrite("Start YouTube Downloader\n");
my $conf = do 'conf.pl';
my $dbh = DBI->connect(@{$conf->{connect_info}});
chdir 'videos' or die "videos $!";
my $working = 0;
my $cv = AnyEvent->condvar;
my $timer; $timer = $timer = AnyEvent->timer(
after => 0,
interval => 10,
cb => sub {
$out->syswrite(AnyEvent->time, "\n");
my $res = $dbh->selectall_arrayref('SELECT * FROM queue WHERE finished = 0 LIMIT 3', +{Slice => +{}});
for my $row (@$res) {
my $client = WWW::YouTube::Download->new;
my $param = $client->prepare_download($row->{video_id});
$out->syswrite($enc->encode(decode_utf8(sprintf "Downloading (fmt=[%d]) -> %s\n", $param->{fmt}, $param->{title}), sub {"U+%04X", shift}));
$client->download($row->{video_id});
$dbh->do('UPDATE queue SET finished = 1 WHERE id = ?', undef, $row->{id});
}
}
);
$cv->recv;
@xaicron

xaicron commented May 16, 2010

Copy link
Copy Markdown
Author

use strict;
use warnings;
use DBI;

my $conf = do 'conf.pl';

my $dbh = DBI->connect(@{$conf->{connect_info}});
$dbh->do('CREATE TABLE queue (
id INTEGER PRIMARY KEY,
finished INT,
video_id VERCHAR,
UNIQUE (video_id)
)');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment