in reply to Re: Downloading first X bytes of a file
in thread Downloading first X bytes of a file

Thanks everyone for the replies.

CountZero that code is just what I was looking for!

I should have explained my self a bit more. I'm learning Perl and havn't been using it much, so at the moment I'm just playing around, seeing what it can do.

If I download the first part of an mp3 file chances are it will have the tag in (well, so far anyway) which I can read and see what the file contains - if the name isn't self-explanatory. So, an entirely useless script but I wanted to see if it could be done ;-)

Here the final script if anyones interested. I think it needs a lot of cleaning up >.<

Does the -w in the shebang line and the 'use warnings' mean the same thing? And what are things like the '-w' called (eg the -f or -d to specify file or directory)? Google doesn't like searching for one-letter things which makes it nearly impossible for me to find out!


#!/usr/bin/perl -w use strict; use warnings; use MP3::Tag; use LWP::UserAgent; my $dir = "./"; my $ua = LWP::UserAgent->new(); my $url = ""; ## <<< url to get files from my $buffer; my @files; my $response = $ua->get($url); if($response->is_success){ my $con = $response->content; while ($con =~ /<a href=\"(.*?)\.mp3\">/g){ push @files, "$1.mp3"; } } foreach my $file(@files) { print "Reading $file..."; $ua->get($url.$file, ':content_cb' => \&first_chunk, ':read_size_h +int' => 10240 ); sub first_chunk { my ($chunk, $response_ref, $protocol_ref) = @_; $buffer .= $chunk; die() if length($buffer) >= 10240; } ## Clean filename $file =~ tr/+/ /; $file =~ s/%([a-fA-F0-9]{2,2})/chr(hex($1))/eg; $file =~ s/<!--(.|\n)*-->//g; ## Save file open FILE, ">$file", or die $!; print FILE $buffer; close FILE; print " Done!\n"; ## Clear buffer $buffer = ""; ## Find tags my $filename = "$dir$file"; my $mp3 = MP3::Tag->new($filename); my ($song, $track, $artist, $album) = $mp3->autoinfo(); ## Print tag: Artist - Album (if any) - Song unless ($album) { if ($artist) { $album = " - "; } } else { $album = " - $album - "; } print "$artist$album$song\n"; }


edit: hmm I don't think I put my post in the right place...