Created
May 16, 2010 16:05
-
-
Save xaicron/402964 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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.']]; | |
| }; | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| my $data = +{ | |
| connect_info => [ | |
| 'dbi:SQLite:dbname=sqlite.db', | |
| '', | |
| '', | |
| +{ | |
| AutoCommit => 1, | |
| RaiseError => 1, | |
| PrintError => 0, | |
| }, | |
| ], | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | |
| )'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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; |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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)
)');