fix multiple words search in AUR

This commit is contained in:
guinux 2014-11-21 15:19:46 +01:00
parent 31c90c6419
commit db61ae3929
3 changed files with 39 additions and 93 deletions

View File

@ -17,8 +17,6 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
//~ const string srcpkgdir = "/tmp/pamac";
namespace AUR { namespace AUR {
// AUR urls // AUR urls
const string aur_url = "http://aur.archlinux.org"; const string aur_url = "http://aur.archlinux.org";
@ -29,33 +27,57 @@ namespace AUR {
const string rpc_multiinfo_arg = "&arg[]="; const string rpc_multiinfo_arg = "&arg[]=";
const string aur_url_id = "/packages.php?setlang=en&ID="; const string aur_url_id = "/packages.php?setlang=en&ID=";
public Json.Array search (string needle) { public Json.Array search (string[] needles) {
string uri = rpc_url + rpc_search + needle; Json.Array prev_inter = new Json.Array ();
string uri = rpc_url + rpc_search + Uri.escape_string (needles[0]);
var session = new Soup.Session (); var session = new Soup.Session ();
var message = new Soup.Message ("GET", uri); var message = new Soup.Message ("GET", uri);
session.send_message (message);
Json.Array results = new Json.Array ();
try {
var parser = new Json.Parser (); var parser = new Json.Parser ();
unowned Json.Object root_object;
session.send_message (message);
try {
parser.load_from_data ((string) message.response_body.flatten ().data, -1); parser.load_from_data ((string) message.response_body.flatten ().data, -1);
root_object = parser.get_root ().get_object ();
unowned Json.Object root_object = parser.get_root ().get_object (); prev_inter = root_object.get_array_member ("results");
results = root_object.get_array_member ("results");
//~ foreach (unowned Json.Node node in results.get_elements ()) {
//~ Json.Object pkg_info = node.get_object ();
//~ results.append (new Pamac.Package (null, pkg_info));
//~ }
} catch (Error e) { } catch (Error e) {
print (e.message); print (e.message);
} }
return results; int length = needles.length;
if (length == 1)
return prev_inter;
int i = 1;
Json.Array inter = new Json.Array ();
Json.Array found = new Json.Array ();
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);
root_object = parser.get_root ().get_object ();
found = root_object.get_array_member ("results");
} catch (Error e) {
print (e.message);
}
foreach (Json.Node prev_inter_node in prev_inter.get_elements ()) {
foreach (Json.Node found_node in found.get_elements ()) {
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);
}
}
}
if (i != (length -1))
prev_inter = inter;
i += 1;
}
return inter;
} }
public Json.Object? info (string pkgname) { public Json.Object? info (string pkgname) {
unowned Json.Object? pkg_info = null; unowned Json.Object? pkg_info = null;
string uri = rpc_url + rpc_info + pkgname; string uri = rpc_url + rpc_info + pkgname;
stdout.printf("get %s from AUR\n", pkgname);
var session = new Soup.Session (); var session = new Soup.Session ();
var message = new Soup.Message ("GET", uri); var message = new Soup.Message ("GET", uri);
session.send_message (message); session.send_message (message);
@ -90,10 +112,6 @@ namespace AUR {
unowned Json.Object root_object = parser.get_root ().get_object (); unowned Json.Object root_object = parser.get_root ().get_object ();
results = root_object.get_array_member ("results"); results = root_object.get_array_member ("results");
//~ foreach (var node in results.get_elements ()) {
//~ var pkg_info = node.get_object ();
//~ results.append (new Pamac.Package (null, pkg_info));
//~ }
} catch (Error e) { } catch (Error e) {
print (e.message); print (e.message);
} }

View File

@ -1,72 +0,0 @@
/*
* pamac-vala
*
* Copyright (C) 2014 Guillaume Benoit <guillaume@manjaro.org>
*
* 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/>.
*/
//~ const string srcpkgdir = "/tmp/pamac";
namespace AUR {
// AUR urls
const string aur_url = "http://aur3.org/";
public string[] search (string needle) {
string[] results = {};
try {
// Resolve hostname to IP address
var resolver = Resolver.get_default ();
var addresses = resolver.lookup_by_name (host, null);
var address = addresses.nth_data (0);
// Connect
var client = new SocketClient ();
var conn = client.connect (new InetSocketAddress (address, 1819));
// Send HTTP GET request
var message = @"nd $query";
conn.output_stream.write (message.data);
// Receive response
var response = new DataInputStream (conn.input_stream);
string line;
while ((line = response.read_line (null)) != null) {
results += line;
}
} catch (Error e) {
stderr.printf ("%s\n", e.message);
}
return results;
}
public void info (string pkgname) {
string uri = rpc_url + rpc_info + pkgname;
var session = new Soup.Session ();
var message = new Soup.Message ("GET", uri);
session.send_message (message);
try {
var parser = new Json.Parser ();
parser.load_from_data ((string) message.response_body.flatten ().data, -1);
var root_object = parser.get_root ().get_object ();
var pkg_info = root_object.get_object_member ("results");
AUR.Pkg aur_pkg = new AUR.Pkg (pkg_info);
stdout.printf ("got %s (%s)\n", aur_pkg.name, aur_pkg.license);
} catch (Error e) {
stderr.printf ("Failed to get infos about %s from AUR\n", pkgname);
print (e.message);
}
}
}

View File

@ -466,7 +466,7 @@ namespace Pamac {
if (aur_results.contains (search_string)) { if (aur_results.contains (search_string)) {
aur_pkgs = aur_results.get (search_string); aur_pkgs = aur_results.get (search_string);
} else { } else {
aur_pkgs = AUR.search (search_string); aur_pkgs = AUR.search (splitted);
aur_results.insert (search_string, aur_pkgs); aur_results.insert (search_string, aur_pkgs);
} }
} else { } else {