xeronuro
11/16/2012 - 1:36 AM

gistfile1.pl

#!/usr/bin/env perl
use common::sense;
use lib qw(../.. /tk/lib /tk/mojo2/lib);

use ojo;
use Util;
use Unicode::Normalize 'NFD';
use Data::Dumper;

my $conf = {
	app_id => '2824561',
	secret => 'aQMoqOe2mnCX5SwCC3fA',
	
	# code   => 'ae20c8c6a703ea34a7',
	# access_token => '5e7e507411329bfc5e7d51ef405e56489e55e7d5e7d51e79aa5ec9fc816bba1',
	code => 'fe07e16145a9e2dca6',
	access_token => 'b5bcb583fadb80a0b5944e5375b5bf5722bb594b5944e5bfed91e55c4b99b0b',
};

unless ($conf->{access_token}) {
	my $r = g("https://oauth.vkontakte.ru/access_token?client_id=$conf->{app_id}&client_secret=$conf->{secret}&code=$conf->{code}")
	  ->json
	;
	die Dumper $r;
}

#

my $DB = Util->db(require 'conf/mysql.conf');

my $i;
for (@{$DB->select('select * from playlist where status="wait" order by count')}) {
	warn "$_->{title}\n";
	$DB->query('update playlist set count=count+1 where id=?', $_->{id});
	
	my $t = NFD($_->{title});
	$t =~ s/\pM//og;
	
	my $q = b($t)->url_escape->to_string;
	my $r = g("https://api.vkontakte.ru/method/audio.search?q=$q&lyrics=1&count=1&access_token=$conf->{access_token}")
	  ->json
	;
	
	if (my $url = $r && $r->{response}->[1]->{url}) {
		$DB->query('update playlist set url=?, status="ready" where id=?', $url, $_->{id});
		$i++;
	}
	else {
		warn ' ERROR';
	}
}

say $i;