diff --git a/PlayBot/sites.pm b/PlayBot/sites.pm index 9c2fa4b8e3c6609282b0bfc38bcf6ed2def6357c..9da0405088eee5478c95a0f1a96ff7baca886b2f 100644 --- a/PlayBot/sites.pm +++ b/PlayBot/sites.pm @@ -89,14 +89,15 @@ sub insert_content # insertion de la vidéo dans la bdd eval { my $sth = $dbh->prepare(' - INSERT INTO playbot (type, url, sender, title, duration, playlist) - VALUES (?,?,?,?,?,?) + INSERT INTO playbot (type, url, external_id, sender, title, duration, playlist) + VALUES (?,?,?,?,?,?,?) '); $log->error("Couldn't prepare querie; aborting") unless (defined $sth); $sth->execute( $content->{site}, $content->{url}, + $content->{external_id}, $content->{author}, $content->{title}, $content->{duration}, diff --git a/PlayBot/sites/soundcloud.pm b/PlayBot/sites/soundcloud.pm index 81b59cfa246ee10878bddf4f7bdabd45966e5eae..f802fcfb50b8452655cc4a7248215080a964cde4 100644 --- a/PlayBot/sites/soundcloud.pm +++ b/PlayBot/sites/soundcloud.pm @@ -5,16 +5,63 @@ use warnings; use LWP::UserAgent; use JSON; -use URI::Find; use Scalar::Util qw(looks_like_number); my $root = 'http://api.soundcloud.com'; my $clientId = 'f4956716fe1a9dc9c3725af822963365'; +sub regex_playlist +{ + return qr#(?:^|[^!])https?://(?:www\.)?soundcloud.com/([a-zA-Z0-9_-]+/sets/[a-zA-Z0-9_-]+)(?:\?.+)?(?: |$)#; +} + + sub regex { - return qr#(?:^|[^!])https?://(?:www\.)?soundcloud.com/([a-zA-Z0-9_-]+/[a-zA-Z0-9_-]+)(?:\?.+)?#; + return qr#(?:^|[^!])https?://(?:www\.)?soundcloud.com/([a-zA-Z0-9_-]+/[a-zA-Z0-9_-]+)(?:\?.+)?(?: |$)#; +} + + +sub get_playlist { + shift; + my $id = shift; + + my $ua = LWP::UserAgent->new( + timeout => 30, + env_proxy => 1, + ); + + my $response; + if (looks_like_number($id)) + { + $response = $ua->get($root.'/playlists/'.$id.'?client_id='.$clientId); + } + else + { + my $url = 'https://www.soundcloud.com/'.$id; + $response = $ua->get($root.'/resolve.json?url='.$url.'&client_id='.$clientId); + } + die($response->status_line) unless ($response->is_success); + + my $content = decode_json($response->decoded_content); + my $infos = { + author => $content->{'user'}->{'username'}, + external_id => $content->{'id'}, + duration => $content->{'track_count'}, + site => 'soundcloud', + title => $content->{'title'}, + url => $content->{permalink_url}, + }; + + my @urls; + foreach my $item (@{ $content->{'tracks'} }) + { + push @urls, $item->{'permalink_url'}; + } + + $infos->{'urls'} = \@urls; + return %$infos; } @@ -53,7 +100,7 @@ sub get { $infos->{'ddl'} = $content->{'download_url'}; } - return %{ $infos }; + return %$infos; } 1;