pamac-classic/src/aur.vala

90 lines
2.9 KiB
Vala
Raw Normal View History

2014-10-22 13:44:02 -03:00
/*
* pamac-vala
*
2017-01-03 05:43:19 -03:00
* Copyright (C) 2014-2017 Guillaume Benoit <guillaume@manjaro.org>
2014-10-22 13:44:02 -03:00
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a get of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace AUR {
// AUR urls
const string aur_url = "http://aur.archlinux.org";
2016-02-19 12:30:47 -03:00
const string rpc_url = aur_url + "/rpc/?v=5";
const string rpc_search = "&type=search&arg=";
const string rpc_multiinfo = "&type=info";
2014-10-22 13:44:02 -03:00
const string rpc_multiinfo_arg = "&arg[]=";
2016-04-14 13:19:20 -03:00
Json.Array rpc_query (string uri) {
var results = new Json.Array ();
2014-10-22 13:44:02 -03:00
var session = new Soup.Session ();
2016-04-14 13:19:20 -03:00
// set a 15 seconds timeout because it is also the dbus daemon timeout
session.timeout = 15;
2014-10-22 13:44:02 -03:00
var message = new Soup.Message ("GET", uri);
2014-11-21 11:19:46 -03:00
var parser = new Json.Parser ();
2014-10-22 13:44:02 -03:00
session.send_message (message);
try {
parser.load_from_data ((string) message.response_body.flatten ().data, -1);
} catch (Error e) {
2016-04-14 13:19:20 -03:00
critical (e.message);
2014-10-22 13:44:02 -03:00
}
2015-01-28 12:40:05 -03:00
unowned Json.Node? root = parser.get_root ();
if (root != null) {
2015-03-04 11:55:36 -03:00
if (root.get_object ().get_string_member ("type") == "error") {
2016-04-14 13:19:20 -03:00
critical ("Failed to query %s from AUR", uri);
2015-03-04 11:55:36 -03:00
} else {
2016-04-14 13:19:20 -03:00
results = root.get_object ().get_array_member ("results");
2015-03-04 11:55:36 -03:00
}
2015-01-28 12:40:05 -03:00
}
2016-04-14 13:19:20 -03:00
return results;
}
public async Json.Array search (string[] needles) {
if (needles.length == 0) {
return new Json.Array ();
} else {
2016-11-18 10:18:31 -03:00
var result = rpc_query (rpc_url + rpc_search + Uri.escape_string (needles[0]));
int i = 1;
while (i < needles.length) {
var inter = new Json.Array ();
var found = rpc_query (rpc_url + rpc_search + Uri.escape_string (needles[i]));
result.foreach_element ((result_array, result_index, result_node) => {
2016-04-14 13:19:20 -03:00
found.foreach_element ((found_array, found_index, found_node) => {
2016-11-18 10:18:31 -03:00
if (strcmp (result_node.get_object ().get_string_member ("Name"),
2016-04-14 13:19:20 -03:00
found_node.get_object ().get_string_member ("Name")) == 0) {
2016-11-18 10:18:31 -03:00
inter.add_element (result_node);
2016-02-19 12:30:47 -03:00
}
2016-04-14 13:19:20 -03:00
});
});
2016-11-18 10:18:31 -03:00
result = (owned) inter;
i++;
2015-01-28 12:40:05 -03:00
}
2016-11-18 10:18:31 -03:00
return result;
2014-11-21 11:19:46 -03:00
}
2014-10-22 13:44:02 -03:00
}
2016-04-14 13:19:20 -03:00
public async Json.Array multiinfo (string[] pkgnames) {
if (pkgnames.length == 0) {
2016-04-14 13:19:20 -03:00
return new Json.Array ();
}
2014-10-22 13:44:02 -03:00
var builder = new StringBuilder ();
builder.append (rpc_url);
builder.append (rpc_multiinfo);
2016-04-14 13:19:20 -03:00
foreach (unowned string pkgname in pkgnames) {
2014-10-22 13:44:02 -03:00
builder.append (rpc_multiinfo_arg);
2015-01-19 16:06:33 -03:00
builder.append (Uri.escape_string (pkgname));
2014-10-22 13:44:02 -03:00
}
2016-04-14 13:19:20 -03:00
return rpc_query (builder.str);
2014-10-22 13:44:02 -03:00
}
}