PageRenderTime 546ms CodeModel.GetById 344ms app.highlight 10ms RepoModel.GetById 87ms app.codeStats 103ms

/lib/Teto/Track/SoundCloud.pm

http://github.com/motemen/Teto
Perl | 62 lines | 47 code | 14 blank | 1 comment | 4 complexity | e365972ad20ff5409c2b9a668918b8d1 MD5 | raw file
 1package Teto::Track::SoundCloud;
 2use Mouse;
 3
 4extends 'Teto::Track';
 5
 6my $NON_USER = qr/^(tags|tracks|people|groups|tour|you|settings|pages|developers|premium|login|followings)$/;
 7my $NON_MIX  = qr/^(following|groups|follow|tracks|comments|favorites|sets)$/;
 8
 9override buildargs_from_url => sub {
10    my ($class, $url) = @_;
11    my ($user, $mix) = $url =~ m<^http://soundcloud\.com/([\w-]+)/([\w-]+)$> or return undef;
12    return undef if $user =~ $NON_USER;
13    return undef if $mix =~ $NON_MIX;
14    return {};
15};
16
17override _play => sub {
18    my $self = shift;
19    my $media_url = $self->media_url or return;
20    # $self->send_url_to_buffer($media_url);
21    my $fh = $self->url_to_fh($media_url);
22    $self->send_file_to_buffer($fh);
23};
24
25sub _build_media_url {
26    my $self = shift;
27
28    my $res = $self->user_agent->get($self->url);
29    unless ($res->is_success) {
30        $self->add_error($res->message);
31        return;
32    }
33
34    if (defined (my $title = $self->extract_title_from_res($res))) {
35        $self->title($title);
36    }
37
38    if (defined (my $image = $self->extract_image_from_res($res))) {
39        $self->image($image);
40    }
41
42    my ($media_url) = $res->decoded_content =~ m<"streamUrl":"([^"]+)"> or return;
43    return $media_url;
44}
45
46sub extract_title_from_res {
47    my ($self, $res) = @_;
48    my ($title) = $res->decoded_content =~ m#<h1[^>]*><em>\s*(.+)\s*</em></h1>#;
49    return $title;
50}
51
52sub extract_image_from_res {
53    my ($self, $res) = @_;
54    my ($image) = $res->decoded_content =~ m#<meta content="([^"]+)" property="og:image" />#;
55    return $image;
56}
57
58__PACKAGE__->meta->make_immutable;
59
60no Mouse;
61
621;