0

I have this problem in my script but I don't have ideas for resolve this !

when I launch the script :

boak@boak-LX:~/Documents$ perl dl-sound.pl --url http://soundcloud.com/alexorion/bigger-room-radio-015
Missing or empty input at dl-sound.pl line 100.

the script :

sub fetch_music_info {
  my ($self, $music_url) = @_;

  $music_url ||= $self->{url};

  my $page = $self->_get_content($music_url);
  my $jsmusic = $1 if ($page =~ m{window.SC.bufferTracks.push\((.*)}i);
  $jsmusic =~ s/;//g if defined($jsmusic);
  $jsmusic =~ s/\)//g if defined($jsmusic);

  my $music_info = JSON::Tiny::decode_json($jsmusic);

  return $music_info;
}
0

3 Answers 3

2

It is unlikely to he the cause of your problem, but you shouldn't make declarations conditional. The behaviour is undefined, and it can lead to all sorts of nonsense.

So this

my $jsmusic = $1 if $page =~ m{window.SC.bufferTracks.push\((.*)}i;

should be

my $jsmusic;
$jsmusic = $1 if $page =~ /window\.SC\.bufferTracks\.push\(([^)]*)/i;

Note also that I have changed the regex to escape the dots, and to capture only the characters up to the next closing parenthesis. That means the following substitutions shouldn't be necessary if I understand your data properly


Update

In fact, looking again, the whole purpose of your subroutine is invalidated if the pattern doesn't match, so you should write it more like

sub fetch_music_info {
  my ($self, $music_url) = @_;

  $music_url ||= $self->{url};

  my $page = $self->_get_content($music_url);
  if ($page =~ /window\.SC\.bufferTracks\.push\(([^)]*)/i) {
    return JSON::Tiny::decode_json($1);
  }
  else {
    die "Music Info not found";
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

this solution doesn't resolve because the script returns all the time "Music Info not found" thank's for your reactivity @Borodin
Well that just means that the regex pattern doesn't appear in the data. There's nothing much else the program can do! Perhaps your regex is wrong. Can you show a sample of the data you're trying to match?
1

I guess that $jsmusic is not defined here:

my $music_info = JSON::Tiny::decode_json($jsmusic);

Change to:

my $music_info;
$music_info = JSON::Tiny::decode_json($jsmusic) if defined $jsmusic;
return $music_info;

Comments

0

I agree with Borodin, but there's a more Perlish way to write this:

my ($jsmusic) = ($page =~ m{window.SC.bufferTracks.push\((.*)}i);

That's because the =~ operator returns a list of the matchvars ($1,$2, etc.).

In this case, $jsmusic will always be declared but it will be undefined if the regexp doesn't match. You need the parentheses around ($jsmusic) to force the operator to list context.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.