package VK::MP3; use strict; use warnings; use utf8; use LWP; use LWP::Protocol::https; use HTML::Entities; use URI::Escape; our $VERSION = 0.03; sub new { my ($class, %args) = @_; die 'USAGE: VK::MP3->new(login => ..., password => ...)' unless _valid_new_args(\%args); my $self = { ua => _create_ua(), login => $args{login}, password => $args{password}, }; bless $self, $class; die 'ERROR: login failed' unless($self->_login()); return $self; } sub search { my ($self, $query) = @_; my $res = $self->{ua}->get('http://vk.com/search?c[section]=audio&c[q]='.uri_escape_utf8($query)); die 'LWP: '.$res->status_line unless $res->is_success; my @matches = $res->decoded_content =~ m'(.+)}si; return undef unless $name; $name =~ s/<[^>]+>//g; $name =~ s/ ?\([^\(]*$//; $name = decode_entities($name); my ($duration) = $str =~ m{
(\d+:\d+)
}i; my ($link) = $str =~ m{value="(http://[^",]+\.mp3)}i; if($duration) { my ($min, $sec) = split /:/, $duration, 2; $duration = $min * 60 + $sec; } else { $duration = 0; } return { name => $name, duration => $duration, link => $link }; } sub _login { my $self = shift; my $res = $self->{ua}->post('https://login.vk.com/?act=login', { email => $self->{login}, pass => $self->{password}, }); return 0 unless $res->is_success; return $res->decoded_content =~ m#]+href="https://login\.vk\.com/\?act=logout&hash=#i; } sub _create_ua { my $ua = LWP::UserAgent->new(); push @{ $ua->requests_redirectable }, 'POST'; $ua->ssl_opts(verify_hostname => 0); $ua->cookie_jar( {} ); return $ua; } sub _valid_new_args { my $args = shift; return 0 unless ref($args) eq 'HASH'; for(qw/login password/) { return 0 unless defined($args->{$_}) && (ref($args->{$_}) eq ''); } return 1; } 1; __END__ =head1 NAME VK::MP3 - searches for mp3 on vkontakte.ru, also known as vk.com. =head1 SYNOPSIS use VK::MP3; my $vk = VK::MP3->new(login => 'user', password => 'secret'); my $rslt = $vk->search('Nightwish'); for (@{$rslt}) { # $_->{name}, $_->{duration}, $_->{link} } =head1 DESCRIPTION B helps you to find direct URL's of audio files on vk.com (via regular expressions and LWP). This package also includes B utility, that allows you to download found mp3. =head1 METHODS =head2 C my $vk = VK::MP3->new(login => $login, password => $password) Constructs a new C object and logs on vk.com. Throws exception in case of any error. =head2 C my $rslt = $vk->search($query) Results, found by $query. =head1 SUPPORT You can find documentation for this module with the perldoc command. perldoc VK::MP3 You can also look for information at: =over 3 =item * BitBucket L =item * AnnoCPAN: Annotated CPAN documentation L =item * CPAN Ratings L =back =head1 SEE ALSO L, L, L. =head1 AUTHOR Alexandr Alexeev, (L) =head1 COPYRIGHT Copyright 2011-2012 by Alexandr Alexeev This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =cut