Good day collective mind. Let me show you my ugly code for search and download mp3 from social network vk.com If some one could show me the better way it will be realy good.
#!/usr/bin/perl use warnings; use strict; use 5.018; use mp3vk_api; use utf8; my $vk = mp3vk_api->new( login => 'mail@mail.com', password => 'passwd', dir =>'Rhapsody', threads=>'10', ); my $out = $vk->search('Rhapsody'); my $a = $vk->vk_download($out);
and pm.
#!/usr/bin/perl package mp3vk_api; use strict; use warnings; use Env qw(HOME); use utf8; use Encode; use URI::Escape; use HTML::Entities; use LWP; use LWP::Protocol::https; use LWP::Simple; use Parallel::ForkManager; our $VERSION = 0.2; #constructor sub new { my ($class, %args) = @_; return ('Error in syntax usage') if(_arg_validation(\%args) != 1) +; my $self = { ua => _mk_ua(), login => $args{login}, password => $args{password}, dir =>$args{dir}, threads=>30, }; bless $self, $class; die ('ERROR WITH CONNECTION AUTH') if($self->_connection_try() != 1) +; return $self; } #Creation of user agent. Private method sub _mk_ua { my $ua = LWP::UserAgent->new(); push @{ $ua->requests_redirectable }, 'POST'; $ua->cookie_jar( {} ); return ($ua); } #public method. search via vk. sub search { my ($self, $query) = @_; my $res = $self->{ua}->get('http://vk.com/search?c[section]=audio&c[ +q]='.uri_escape_utf8($query)); eval{$res->is_success;} or return 'Error possible with LWP_ua see er +r msg -> '.$res->status_line; # my @findnings = $res->decoded_content =~ m'<input type="hi +dden" id="audio_info(.*?)</table>'sgi; my @findnings = $res->decoded_content =~ m'<div id="audio.* +?>(.*?)<div class="ai_controls">'sgi; my @dump; push @dump, $self->_cleanup_response($_) for(@ +findnings); @dump = grep { defined $_ } @dump; return (\@dump); } #parsing of vk response. private method sub _cleanup_response { my ($self, $str) = @_; my ($title) = $str =~ m{<span class="ai_title">(.*?)</span>}si; return undef unless $title; $title =~ s/(<.*?>|\(.+\)|\&\w+|[;'"])//g; $title =~ s/\s+/ /g; $title = decode_entities($title); my ($duration) = $str =~ m{<div class="ai_dur" data-dur="\d+" onclic +k="audioplayer.switchTimeFormat(this, event);">(\d+:\d+)</div>}i; my ($link) = $str =~ m{value="(http://[^'"`]+\.mp3)}i; $duration = 0 unless($duration); return { title => $title, duration => $duration, link => $link }; } #check posibility of connection sub _connection_try{ my $self = shift; my $res = $self->{ua}->post('https://login.vk.com/?act=login', { email => $self->{login}, pass => $self->{password}, }); if( $res->is_success && ($res->decoded_content =~ /var\s+vk\s*=\s*\{[^\{\}]*?id\s*\:\s*( +\d+)/i || $res->decoded_content =~ m#login\.vk\.com/\?act=logout#i ) ) + { $self->{id} = $1; return 1; } return 0; } #validation of login and passwd sub _arg_validation { my $args = shift; unless (defined $args->{dir}){ $args->{dir} = "$ENV{HOME}/vk_music/"; mkdir $args->{dir}; } else{ $args->{dir} = "$ENV{HOME}/vk_music/$args->{dir}/"; mkdir $args->{dir}; } unless(defined $args->{login} && defined $args->{password} && $args->{login} =~ /^.*?\@.*?$/ && $args->{ +password} =~ /\w{2,}/){ return 0; } else{ return 1; } } sub vk_download{ my $self = shift; my $ref_dump = shift; my $pm = Parallel::ForkManager->new($self->{threads}); foreach (@{$ref_dump}) { $pm->start and next +; # do the fork if (getstore($_->{ +link}, "$self->{dir}/$_->{title}.mp3") != RC_OK){ prin +t 'error occured '."\n"; } else{ print + $_->{title}.'.................. downloaded'."\n"; } $pm->finish; # do th +e exit in the child process } } 1;

In reply to mp3 search and download by builat

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.