This repository has been archived by the owner on Jan 29, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SearchInput.java
101 lines (87 loc) · 4 KB
/
SearchInput.java
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Assignment;
/**
*
* @author francisyzy
*/
public class SearchInput {
private boolean yahooSelected;
private boolean bingSelected;
private boolean googleSelected;
public SearchInput(boolean yahoo, boolean bing, boolean google){
this.yahooSelected = yahoo;
this.bingSelected = bing;
this.googleSelected = google;
}
public static Download dl = new Download();
public void startSearch(String input, int threadCount) throws InterruptedException{
String g_api_key = "";//get one from https://developers.google.com/custom-search/json-api/v1/overview
String yahoo = "https://sg.search.yahoo.com/search?p=";
String bing = "https://www.bing.com/search?q=";
//API KEY from https://developers.google.com/custom-search/json-api/v1/overview
//Reference https://developers.google.com/custom-search/json-api/v1/using_rest
String google = "https://www.googleapis.com/customsearch/v1?key="+g_api_key+"&cx=002976313549948865923:30uboh7bkhw&q=";
String yahooregex = "result\"><h3 class=\"title\"><a href=\"(.+?)\" class=\"\">";
String bingregex = "class=\"b_algo\"><h2><a href=\"(.+?)\" h=\"";
String googleregex = "\\\"link\\\": \\\"(http.*?)\\\"";
StringBuffer text = new StringBuffer(input);
String ysearch = yahoo + text.toString().replaceAll("%", "%25").replaceAll("\\+", "%2B").replaceAll(" ", "+");
String bsearch = bing + text.toString().replaceAll("%", "%25").replaceAll("\\+", "%2B").replaceAll(" ", "+");
String gsearch = google + text.toString().replaceAll("%", "%25").replaceAll("\\+", "%2B").replaceAll(" ", "+");
if((yahooSelected)&&!(bingSelected)&&!(googleSelected)){
Search yahooSearch = new Search(ysearch,dl,yahooregex);
yahooSearch.start();
yahooSearch.join();
}
else if(!(yahooSelected)&&(bingSelected)&&!(googleSelected)){
Search bingSearch = new Search(bsearch,dl,bingregex);
bingSearch.start();
bingSearch.join();
}
else if(!(yahooSelected)&&!(bingSelected)&&(googleSelected)){
Search googleSearch = new Search(gsearch,dl,googleregex);
googleSearch.start();
googleSearch.join();
}
else if((yahooSelected)&&(bingSelected)&&!(googleSelected)){
Search yahooSearch = new Search(ysearch,dl,yahooregex);
Search bingSearch = new Search(bsearch,dl,bingregex);
yahooSearch.start();
bingSearch.start();
yahooSearch.join();
bingSearch.join();
}
else if((yahooSelected)&&!(bingSelected)&&(googleSelected)){
Search yahooSearch = new Search(ysearch,dl,yahooregex);
Search googleSearch = new Search(gsearch,dl,googleregex);
yahooSearch.start();
googleSearch.start();
yahooSearch.join();
googleSearch.join();
}
else if(!(yahooSelected)&&(bingSelected)&&(googleSelected)){
Search bingSearch = new Search(bsearch,dl,bingregex);
Search googleSearch = new Search(gsearch,dl,googleregex);
bingSearch.start();
googleSearch.start();
bingSearch.join();
googleSearch.join();
}
else{
Search yahooSearch = new Search(ysearch,dl,yahooregex);
Search bingSearch = new Search(bsearch,dl,bingregex);
Search googleSearch = new Search(gsearch,dl,googleregex);
yahooSearch.start();
bingSearch.start();
googleSearch.start();
yahooSearch.join();
bingSearch.join();
googleSearch.join();
}
dl.startdownload(threadCount);
}
}