From 8744dee4f21263c4da72116aa3920e850abd0366 Mon Sep 17 00:00:00 2001 From: wjoe Date: Tue, 24 Sep 2013 15:16:24 +0100 Subject: Added options to get more results with -n --- blasearch.java | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/blasearch.java b/blasearch.java index 6ca2295..74ce079 100755 --- a/blasearch.java +++ b/blasearch.java @@ -32,7 +32,7 @@ public class blasearch { { String query = ""; - String version = "v1.2.1"; + String version = "v1.5.0"; /* Build query from query or arguments, depending on whether arguments were given. */ @@ -53,13 +53,37 @@ public class blasearch { if (query.equals("-v") || query.equals("--version")) { System.out.println(version); System.exit(0); + } + String[] qParts = query.split(" "); + int offset = 0; + int start = 0; + query = ""; + for (int i = 0; i < qParts.length; i++) { + if (qParts[i].equals("-n") && i < qParts.length-1) { + try { + if(Integer.parseInt(qParts[i+1]) > 0) { + offset = Integer.parseInt(qParts[i+1])-1; + start = (offset / 2)*4; + offset = (offset %2)*2; + i++; + } else { + start = 0; + offset = 0; + i++; + } + } catch (NumberFormatException e) { + start = 0; + offset = 0; + } + } else { + query += qParts[i]+"+"; + } } // Change all ' ' to '+' query = query.replaceAll(" ", "+"); - String[] parts = null; //URL which provides XML response - URL url = new URL("http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q="+ query); + URL url = new URL("http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q="+ query+"&start="+start); URLConnection urlc = url.openConnection(); @@ -98,12 +122,12 @@ public class blasearch { } // Print results if(results.size() > 0){ - for(int i=0; i<2; i++){ + for(int i=(offset); i<(offset+2); i++){ if (i != 0) { System.out.print(" | "); } int count = i + 1; - System.out.print("Result " + count + ": " + results.get(i)); + System.out.print("Result " + (count+start) + ": " + results.get(i)); } } else { System.out.print("Error: no results.\n"); -- cgit v1.2.3