| 1 | package VK::MP3; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use utf8; | ||
| 6 | |||
| 7 | use LWP; | ||
| 8 | use LWP::Protocol::https; | ||
| 9 | use HTML::Entities; | ||
| 10 | use URI::Escape; | ||
| 11 | use JSON::XS qw/decode_json/; | ||
| 12 | use Encode; | ||
| 13 | |||
| 14 | our $VERSION = 0.11; | ||
| 15 | |||
| 16 | sub new { | ||
| 17 | my ($class, %args) = @_; | ||
| 18 | die 'USAGE: VK::MP3->new(login => ..., password => ...)' | ||
| 19 | unless _valid_new_args(\%args); | ||
| 20 | |||
| 21 | my $self = { | ||
| 22 | ua => _create_ua(), | ||
| 23 | login => $args{login}, | ||
| 24 | password => $args{password}, | ||
| 25 | }; | ||
| 26 | bless $self, $class; | ||
| 27 | |||
| 28 | die 'ERROR: login failed' unless($self->_login()); | ||
| 29 | |||
| 30 | return $self; | ||
| 31 | } | ||
| 32 | |||
| 33 | sub search { | ||
| 34 | my ($self, $query) = @_; | ||
| 35 | |||
| 36 | my $res = $self->{ua}->get('https://vk.com/search?c[section]=audio&c[q]='.uri_escape_utf8($query)); | ||
| 37 | die 'lwp: '.$res->status_line unless $res->is_success; | ||
| 38 | |||
| 39 | my @matches = $res->decoded_content =~ m'<input type="hidden" id="audio_info(.*?)</tbody></table>'sgi; | ||
| 40 | |||
| 41 | my @rslt; | ||
| 42 | push @rslt, $self->_parse_found_item($_) for(@matches); | ||
| 43 | @rslt = grep { defined $_ } @rslt; | ||
| 44 | |||
| 45 | return \@rslt; | ||
| 46 | } | ||
| 47 | |||
| 48 | sub get_playlist { | ||
| 49 | my ($self) = @_; | ||
| 50 | my $res; | ||
| 51 | |||
| 52 | $res = $self->{ua}->post('https://vk.com/audio', { | ||
| 53 | act => 'load_audios_silent', | ||
| 54 | al => 1, | ||
| 55 | gid => 0, | ||
| 56 | id => $self->{id}, | ||
| 57 | please_dont_ddos => '2', | ||
| 58 | }, | ||
| 59 | ); | ||
| 60 | die 'LWP: '.$res->status_line unless $res->is_success; | ||
| 61 | |||
| 62 | my $json_str = (split /<!>/, $res->decoded_content)[5]; | ||
| 63 | $json_str =~ s/'/"/gs; | ||
| 64 | $json_str = Encode::encode('utf-8', $json_str); | ||
| 65 | my $json = decode_json($json_str); | ||
| 66 | return 'Invalid response' unless defined $json->{all} && ref($json->{all}) eq 'ARRAY'; | ||
| 67 | |||
| 68 | my @rslt; | ||
| 69 | for my $item(@{$json->{all}}) { | ||
| 70 | next unless ref $item eq 'ARRAY' && scalar @{$item} > 7; | ||
| 71 | my $name = decode_entities($item->[5].' – '.$item->[6]); | ||
| 72 | $name =~ s/(^\s+|\s+$)//g; | ||
| 73 | my $rslt_item = { | ||
| 74 | name => $name, | ||
| 75 | duration => $item->[3], | ||
| 76 | link => $item->[2], | ||
| 77 | }; | ||
| 78 | push @rslt, $rslt_item; | ||
| 79 | } | ||
| 80 | return \@rslt; | ||
| 81 | } | ||
| 82 | |||
| 83 | sub _parse_found_item { | ||
| 84 | my ($self, $str) = @_; | ||
| 85 | my ($name) = $str =~ m{<div class="title_wrap fl_l".*?>(.*?)</div>}si; | ||
| 86 | return undef unless $name; | ||
| 87 | |||
| 88 | $name =~ s/<[^>]+>//g; | ||
| 89 | $name =~ s/ ?\([^\(]*$//; | ||
| 90 | $name = decode_entities($name); | ||
| 91 | |||
| 92 | my ($duration) = $str =~ m{<div class="duration fl_r".*?>(\d+:\d+)</div>}i; | ||
| 93 | my ($link) = $str =~ m{value="(https?://[^",]+\.mp3)}i; | ||
| 94 | |||
| 95 | if($duration) { | ||
| 96 | my ($min, $sec) = split /:/, $duration, 2; | ||
| 97 | $duration = $min * 60 + $sec; | ||
| 98 | } else { | ||
| 99 | $duration = 0; | ||
| 100 | } | ||
| 101 | |||
| 102 | return { name => $name, duration => $duration, link => $link }; | ||
| 103 | } | ||
| 104 | |||
| 105 | sub _login { | ||
| 106 | my $self = shift; | ||
| 107 | |||
| 108 | my $res = $self->{ua}->get('https://vk.com/'); | ||
| 109 | die 'lwp: '.$res->status_line unless $res->is_success; | ||
| 110 | |||
| 111 | my ($ip_h) = $res->decoded_content =~ m'<input type="hidden" name="ip_h" value="([^"]+)" />'sgi; | ||
| 112 | my ($lg_h) = $res->decoded_content =~ m'<input type="hidden" name="lg_h" value="([^"]+)" />'sgi; | ||
| 113 | |||
| 114 | return 1 unless $ip_h && $lg_h; | ||
| 115 | |||
| 116 | $res = $self->{ua}->post('https://login.vk.com/?act=login', { | ||
| 117 | email => $self->{login}, | ||
| 118 | pass => $self->{password}, | ||
| 119 | act => "login", | ||
| 120 | role => "al_frame", | ||
| 121 | expire => "", | ||
| 122 | captcha_sid => "", | ||
| 123 | captcha_key => "", | ||
| 124 | _origin => "https://vk.com", | ||
| 125 | ip_h => $ip_h, | ||
| 126 | lg_h => $lg_h, | ||
| 127 | }); | ||
| 128 | |||
| 129 | if( $res->is_success && | ||
| 130 | ($res->decoded_content =~ /parent\.onLoginDone/i) && | ||
| 131 | ($res->decoded_content =~ /"uid":"(\d+)"/i)) { | ||
| 132 | $self->{id} = $1; | ||
| 133 | return 1; | ||
| 134 | } | ||
| 135 | |||
| 136 | return 0; | ||
| 137 | } | ||
| 138 | |||
| 139 | sub _create_ua { | ||
| 140 | my $ua = LWP::UserAgent->new(agent => "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/47.0.2526.106 Chrome/47.0.2526.106 Safari/537.36"); | ||
| 141 | |||
| 142 | push @{ $ua->requests_redirectable }, 'POST'; | ||
| 143 | $ua->cookie_jar( {} ); | ||
| 144 | |||
| 145 | return $ua; | ||
| 146 | } | ||
| 147 | |||
| 148 | sub _valid_new_args { | ||
| 149 | my $args = shift; | ||
| 150 | return 0 unless ref($args) eq 'HASH'; | ||
| 151 | for(qw/login password/) { | ||
| 152 | return 0 unless defined($args->{$_}) && (ref($args->{$_}) eq ''); | ||
| 153 | } | ||
| 154 | return 1; | ||
| 155 | } | ||
| 156 | |||
| 157 | 1; | ||
| 158 | |||
| 159 | __END__ |