-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBingImageSearchv7Quickstart.java
More file actions
132 lines (112 loc) · 5.5 KB
/
BingImageSearchv7Quickstart.java
File metadata and controls
132 lines (112 loc) · 5.5 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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*Copyright (c) Microsoft Corporation. All rights reserved.
Licensed under the MIT License.*/
import java.net.*;
import java.util.*;
import java.io.*;
import javax.net.ssl.HttpsURLConnection;
/*
* Gson: https://github.com/google/gson
* Maven info:
* groupId: com.google.code.gson
* artifactId: gson
* version: 2.8.1
*
* Once you have compiled or downloaded gson-2.8.1.jar, assuming you have placed it in the
* same folder as this file (BingImageSearch.java), you can compile and run this program with the following steps:
* 1. create three directories, named "bin", "src", and "lib"
* 2. place this .java file in "src"
* 3. download the latest gson .jar file online, and place it in the "lib" folder
* 4. replace the subscriptionKey value with your valid Cognitive services subscription key
* 5. run the following commands, replacing "lib/gson-2.8.5.jar" with your .jar file:
* javac -d bin -sourcepath src -cp lib/gson-2.8.5.jar src/BingImageSearchv7Quickstart.java
* java -cp bin;lib/gson-2.8.5.jar BingImageSearchv7Quickstart
*/
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonArray;
public class BingImageSearchv7Quickstart {
// ***********************************************
// *** Update or verify the following values. ***
// **********************************************
// Replace the subscriptionKey string value with your valid subscription key.
static String subscriptionKey = "enter key here";
// Verify the endpoint URI. At this writing, only one endpoint is used for Bing
// search APIs. In the future, regional endpoints may be available. If you
// encounter unexpected authorization errors, double-check this value against
// the endpoint for your Bing Web search instance in your Azure dashboard.
static String host = "https://api.cognitive.microsoft.com";
static String path = "/bing/v7.0/images/search";
static String searchTerm = "tropical ocean";
public static SearchResults SearchImages (String searchQuery) throws Exception {
// construct URL of search request (endpoint + query string)
URL url = new URL(host + path + "?q=" + URLEncoder.encode(searchQuery, "UTF-8"));
HttpsURLConnection connection = (HttpsURLConnection)url.openConnection();
connection.setRequestProperty("Ocp-Apim-Subscription-Key", subscriptionKey);
// receive JSON body
InputStream stream = connection.getInputStream();
String response = new Scanner(stream).useDelimiter("\\A").next();
// construct result object for return
SearchResults results = new SearchResults(new HashMap<String, String>(), response);
// extract Bing-related HTTP headers
Map<String, List<String>> headers = connection.getHeaderFields();
for (String header : headers.keySet()) {
if (header == null) continue; // may have null key
if (header.startsWith("BingAPIs-") || header.startsWith("X-MSEdge-")) {
results.relevantHeaders.put(header, headers.get(header).get(0));
}
}
stream.close();
return results;
}
// pretty-printer for JSON; uses GSON parser to parse and re-serialize
public static String prettify(String json_text) {
JsonParser parser = new JsonParser();
JsonObject json = parser.parse(json_text).getAsJsonObject();
Gson gson = new GsonBuilder().setPrettyPrinting().create();
return gson.toJson(json);
}
public static void main (String[] args) {
if (subscriptionKey.length() != 32) {
System.out.println("Invalid Bing Search API subscription key!");
System.out.println("Please paste yours into the source code.");
System.exit(1);
}
try {
System.out.println("Searching the Web for: " + searchTerm);
SearchResults result = SearchImages(searchTerm);
//uncomment this section to see the HTTP headers
/*
System.out.println("\nRelevant HTTP Headers:\n");
for (String header : result.relevantHeaders.keySet())
System.out.println(header + ": " + result.relevantHeaders.get(header));
*/
System.out.println("\nJSON Response:\n");
//parse the jseon
JsonParser parser = new JsonParser();
JsonObject json = parser.parse(result.jsonResponse).getAsJsonObject();
//get the first image result from the JSON object, along with the total
//number of images returned by the Bing Image Search API.
String total = json.get("totalEstimatedMatches").getAsString();
JsonArray results = json.getAsJsonArray("value");
JsonObject first_result = (JsonObject)results.get(0);
String resultURL = first_result.get("thumbnailUrl").getAsString();
System.out.println("\nThe total number of images found: "+ total +"\n");
System.out.println("\nThe thumbnail URL to the first image search URL: "+ resultURL +"\n");
}
catch (Exception e) {
e.printStackTrace(System.out);
System.exit(1);
}
}
}
// Container class for search results encapsulates relevant headers and JSON data
class SearchResults{
HashMap<String, String> relevantHeaders;
String jsonResponse;
SearchResults(HashMap<String, String> headers, String json) {
relevantHeaders = headers;
jsonResponse = json;
}
}