pamac-classic/src/aur.vala

146 lines
4.8 KiB
Vala
Raw Normal View History

2014-10-22 13:44:02 -03:00
/*
* pamac-vala
*
2015-03-04 11:55:36 -03:00
* Copyright (C) 2014-2015 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";
const string rpc_url = aur_url + "/rpc.php";
const string rpc_search = "?type=search&arg=";
const string rpc_info = "?type=info&arg=";
const string rpc_multiinfo = "?type=multiinfo";
const string rpc_multiinfo_arg = "&arg[]=";
const string aur_url_id = "/packages.php?setlang=en&ID=";
2014-11-21 11:19:46 -03:00
public Json.Array search (string[] needles) {
2014-12-03 12:02:14 -03:00
var prev_inter = new Json.Array ();
2014-11-21 11:19:46 -03:00
string uri = rpc_url + rpc_search + Uri.escape_string (needles[0]);
2014-10-22 13:44:02 -03:00
var session = new Soup.Session ();
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) {
print (e.message);
}
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") {
stderr.printf ("Failed to search %s from AUR\n", needles[0]);
} else {
prev_inter = root.get_object ().get_array_member ("results");
}
2015-01-28 12:40:05 -03:00
}
2014-11-21 11:19:46 -03:00
int length = needles.length;
2015-03-04 11:55:36 -03:00
if (length == 1) {
2014-11-21 11:19:46 -03:00
return prev_inter;
2015-03-04 11:55:36 -03:00
}
2014-11-21 11:19:46 -03:00
int i = 1;
2014-12-03 12:02:14 -03:00
var inter = new Json.Array ();
var found = new Json.Array ();
2014-11-21 11:19:46 -03:00
while (i < length) {
inter = new Json.Array ();
uri = rpc_url + rpc_search + Uri.escape_string (needles[i]);
message = new Soup.Message ("GET", uri);
session.send_message (message);
try {
parser.load_from_data ((string) message.response_body.flatten ().data, -1);
} catch (Error e) {
print (e.message);
}
2015-01-28 12:40:05 -03:00
root = parser.get_root ();
if (root != null) {
2015-03-04 11:55:36 -03:00
if (root.get_object ().get_string_member ("type") == "error") {
stderr.printf ("Failed to search %s from AUR\n", needles[i]);
} else {
found = root.get_object ().get_array_member ("results");
}
2015-01-28 12:40:05 -03:00
}
prev_inter.foreach_element ((prev_inter_array, prev_inter_index, prev_inter_node) => {
found.foreach_element ((found_array, found_index, found_node) => {
2014-11-21 11:19:46 -03:00
if (strcmp (prev_inter_node.get_object ().get_string_member ("Name"),
found_node.get_object ().get_string_member ("Name")) == 0) {
inter.add_element (prev_inter_node);
}
});
});
2015-01-28 12:40:05 -03:00
if (i != (length -1)) {
2014-11-21 11:19:46 -03:00
prev_inter = inter;
2015-01-28 12:40:05 -03:00
}
2014-11-21 11:19:46 -03:00
i += 1;
}
return inter;
2014-10-22 13:44:02 -03:00
}
public unowned Json.Object? info (string pkgname) {
2015-01-19 16:06:33 -03:00
string uri = rpc_url + rpc_info + Uri.escape_string (pkgname);
2014-10-22 13:44:02 -03:00
var session = new Soup.Session ();
var message = new Soup.Message ("GET", uri);
session.send_message (message);
2015-01-28 12:40:05 -03:00
var parser = new Json.Parser ();
2014-10-22 13:44:02 -03:00
try {
parser.load_from_data ((string) message.response_body.flatten ().data, -1);
} catch (Error e) {
stderr.printf ("Failed to get infos about %s from AUR\n", pkgname);
print (e.message);
}
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") {
stderr.printf ("Failed to get infos about %s from AUR\n", pkgname);
} else {
return root.get_object ().get_object_member ("results");
2015-03-04 11:55:36 -03:00
}
2015-01-28 12:40:05 -03:00
}
return null;
2014-10-22 13:44:02 -03:00
}
public Json.Array multiinfo (string[] pkgnames) {
Json.Array results = new Json.Array ();
if (pkgnames.length == 0) {
return results;
}
2014-10-22 13:44:02 -03:00
var builder = new StringBuilder ();
builder.append (rpc_url);
builder.append (rpc_multiinfo);
foreach (string pkgname in pkgnames) {
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
}
var session = new Soup.Session ();
var message = new Soup.Message ("GET", builder.str);
session.send_message (message);
2015-01-28 12:40:05 -03:00
var parser = new Json.Parser ();
2014-10-22 13:44:02 -03:00
try {
parser.load_from_data ((string) message.response_body.flatten ().data, -1);
} catch (Error e) {
print (e.message);
}
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") {
stderr.printf ("Failed to multiinfo %s from AUR\n", builder.str);
} else {
results = root.get_object ().get_array_member ("results");
}
2015-01-28 12:40:05 -03:00
}
2014-10-22 13:44:02 -03:00
return results;
}
}