You're viewing a comment by Parth Patil and its responses.
You're viewing a comment by Parth Patil and its responses.
I am being sponsored by Syntress! They bought me an amazing dedicated server to run catonmat on. If you're looking web services, I highly recommend the Syntress guys!
I am being sponsored by A-Writer! If you ever need help with essay writing, look no further than A-Writer! They will help you with your writing in as quickly as 3 hours!
I love to read science books. They make my day and I get ideas for awesome blog posts, such as Busy Beaver, On Functors, Recursive Regular Expressions and many others.
Take a look at my
Amazon wish list, if you're curious about what I have planned reading next, and want to surprise me. :)
If you are interested in advertising on catonmat.net, contact me.
Free tools for coding on Vietstarsoft.com.
Programming homework help.


After reading the comment from Vinniemc I thought of taking a stab at showing some kind of progress indicator.
For showing progress indicator I had to find a mechanism where LWP::UserAgent would call my function after it received each chunk of file. I was delighted to find in LWP::UserAgent's perldoc that its possible to specify a call back method to LWP::UserAgent's get() method via the special field name ":content_cb". After trying unsuccessfully to use this call back functionality I went back to the LWP perldoc. On re-reading I found that its not possible to use the option ":content_file" & ":content_cb" at the same time !
After searching some more I found the lwp cookbook which has an example of manually processing http responses. So based on that I was able to hack up the progress indicator. Unfortunately it hardly qualifies as a one liner anymore! In my attempt to still make the script small it has become a little obfuscated and might be difficult to understand. So here is the code
perl -MLWP -e '$_ = shift; ($y, $i) = m#(http://www\.youtube\.com)/watch\?v=(.+)#; $m = LWP::UserAgent->new; ($t = $m->get("$y/v/$i")->request->uri) =~ s/.*&t=(.+)/$1/; open($fh,">$i.flv");binmode($fh);$t1=$t2=time;print "\n";$res = $m->request(HTTP::Request->new(GET => "$y/get_video?video_id=$i&t=$t"),sub { ($c,$res) = @_;$br += length($c);$t2 = time;if($t2 > $t1){if ($res->content_length) {printf STDERR "%d%% - ",100*$br/$res->content_length;$t1= $t2;}}print $fh $c;});close($fh);print "\n";' http://www.youtube.com/watch?v=l69Vi5IDc0gReply To This Comment