forked from zifnab06/JavaBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoogle.java
More file actions
57 lines (54 loc) · 1.93 KB
/
google.java
File metadata and controls
57 lines (54 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/**
* File: google.java
* Author: zifnab [at] zifnab06 [dot] net
* License: cc-by-nc
* usage: google.search("Search Terms");
* returns: URL in String format, or "No Results Found!"
* requires: org.json package.
*/
import org.json.JSONObject;
import org.json.JSONArray;
import java.net.*;
import java.io.*;
public class google{
public static String[] search(String terms){
String key = "ABQIAAAA6KnsrpTIyjEMJ1EqHjKG_xRBaorPdDj7IJd2xMtxtE9DwSoKhRQgazFCenlI-wnGV1574jW06163iw";
String ip = "173.255.208.207";
System.out.println("Google-ing: " + terms);
terms = terms.replace(' ', '+');
try {
//Opens URL
URL googlesearch = new URL("http://ajax.googleapis.com/ajax/services/search/web?v=1.0&" + "q=" + terms + "&key=" + key + "&userip=" + ip);
URLConnection connection = googlesearch.openConnection();
connection.addRequestProperty("Referer", "zifnab06.net");
//Read in JSON crap from URL.
String line;
StringBuilder results = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
while((line = reader.readLine()) != null) {
results.append(line + "\n");
}
//JSON Crap. Google needs to use XML....
JSONObject json = new JSONObject(results.toString());
JSONArray ja = json.getJSONObject("responseData").getJSONArray("results");
JSONObject j = ja.getJSONObject(0);
//Returns Title: URL
String[] ret = {j.toString(), j.getString("titleNoFormatting") + ": " + URLDecoder.decode(j.getString("url"),"UTF-8")};
return ret;
} catch (MalformedURLException e) {
//Shouldn't EVER hit this.
System.out.println(e);
String[] ret = {"error", e.toString()};
return(ret);
} catch (IOException e) {
//Shouldn't EVER hit this.
System.out.println(e);
String[] ret = {"error", e.toString()};
return(ret);
} catch (Exception e) {
System.out.println(e);
String[] ret = {"error","No results found!"};
return ret;
}
}
}