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...


In reply to Re^2: Downloading first X bytes of a file by pc0019
in thread Downloading first X bytes of a file by pc0019

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.