| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -6,7 +6,6 @@ build/ | |||
| 6 | 6 | .project | |
| 7 | 7 | bin/ | |
| 8 | 8 | data.json | |
| 9 | - demo/gradle/ | ||
| 10 | 9 | demo/libs/*.jar | |
| 11 | 10 | derive.rb | |
| 12 | 11 | gradlew.bat | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -43,39 +43,39 @@ make all SERPAPI_KEY='<your private key>' | |||
| 43 | 43 | Use quotes if your key contains shell-special characters. You need a SerpApi account to obtain a key: https://serpapi.com/dashboard | |
| 44 | 44 | ||
| 45 | 45 | `demo/src/main/java/demo/App.java`: | |
| 46 | - ```java | ||
| 47 | - public class App { | ||
| 48 | - public static void main(String[] args) throws SerpApiException { | ||
| 49 | - if(args.length != 1) { | ||
| 50 | - System.out.println("Usage: app <secret api key>"); | ||
| 51 | - System.exit(1); | ||
| 52 | - } | ||
| 46 | + ```javapublic | ||
| 47 | + class App { | ||
| 48 | + public static void main(String[] args) { | ||
| 49 | + String apiKey = System.getenv("SERPAPI_KEY"); | ||
| 53 | 50 | ||
| 51 | + // set search location | ||
| 54 | 52 | String location = "Austin,Texas"; | |
| 55 | - System.out.println("find the first Coffee in " + location); | ||
| 53 | + String engine = "google"; | ||
| 54 | + System.out.println("find the first coffee shop in " + location + " using " + engine); | ||
| 56 | 55 | ||
| 57 | - // set api_key provided by the command line | ||
| 58 | 56 | Map<String, String> auth = new HashMap<>(); | |
| 59 | - auth.put("api_key", args[0]); | ||
| 57 | + auth.put("engine", engine); | ||
| 58 | + auth.put("api_key", apiKey); | ||
| 60 | 59 | ||
| 61 | - // Create search | ||
| 60 | + // create client | ||
| 62 | 61 | SerpApi serpapi= new SerpApi(auth); | |
| 63 | 62 | ||
| 63 | + // create search parameters | ||
| 64 | 64 | Map<String, String> parameter = new HashMap<>(); | |
| 65 | 65 | parameter.put("q", "Coffee"); | |
| 66 | 66 | parameter.put("location", location); | |
| 67 | 67 | ||
| 68 | + // perform search | ||
| 68 | 69 | try { | |
| 69 | - // Perform search | ||
| 70 | - JsonObject data = serpapi.search(parameter); | ||
| 71 | - | ||
| 72 | - // Decode response | ||
| 73 | - JsonArray results = data.get("local_results").getAsJsonObject().get("places").getAsJsonArray(); | ||
| 74 | - JsonObject first_result = results.get(0).getAsJsonObject(); | ||
| 75 | - System.out.println("first coffee shop: " + first_result.get("title").getAsString() + " found on Google in " + location); | ||
| 70 | + // get search results | ||
| 71 | + JsonObject data = serpapi.search(parameter); | ||
| 72 | + JsonArray organic = data.getAsJsonArray("organic_results"); | ||
| 73 | + JsonObject first = organic.get(0).getAsJsonObject(); | ||
| 74 | + System.out.println("First result: " + first.get("title").getAsString() + " (search near " + location + ")"); | ||
| 76 | 75 | } catch (SerpApiException e) { | |
| 77 | - System.out.println("oops exception detected!"); | ||
| 76 | + System.out.println("SerpApi request failed."); | ||
| 78 | 77 | e.printStackTrace(); | |
| 78 | + System.exit(1); | ||
| 79 | 79 | } | |
| 80 | 80 | } | |
| 81 | 81 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -61,39 +61,39 @@ make all SERPAPI_KEY='<your private key>' | |||
| 61 | 61 | Use quotes if your key contains shell-special characters. You need a SerpApi account to obtain a key: https://serpapi.com/dashboard | |
| 62 | 62 | ||
| 63 | 63 | `demo/src/main/java/demo/App.java`: | |
| 64 | - ```java | ||
| 65 | - public class App { | ||
| 66 | - public static void main(String[] args) throws SerpApiException { | ||
| 67 | - if(args.length != 1) { | ||
| 68 | - System.out.println("Usage: app <secret api key>"); | ||
| 69 | - System.exit(1); | ||
| 70 | - } | ||
| 64 | + ```javapublic | ||
| 65 | + class App { | ||
| 66 | + public static void main(String[] args) { | ||
| 67 | + String apiKey = System.getenv("SERPAPI_KEY"); | ||
| 71 | 68 | ||
| 69 | + // set search location | ||
| 72 | 70 | String location = "Austin,Texas"; | |
| 73 | - System.out.println("find the first Coffee in " + location); | ||
| 71 | + String engine = "google"; | ||
| 72 | + System.out.println("find the first coffee shop in " + location + " using " + engine); | ||
| 74 | 73 | ||
| 75 | - // set api_key provided by the command line | ||
| 76 | 74 | Map<String, String> auth = new HashMap<>(); | |
| 77 | - auth.put("api_key", args[0]); | ||
| 75 | + auth.put("engine", engine); | ||
| 76 | + auth.put("api_key", apiKey); | ||
| 78 | 77 | ||
| 79 | - // Create search | ||
| 78 | + // create client | ||
| 80 | 79 | SerpApi serpapi= new SerpApi(auth); | |
| 81 | 80 | ||
| 81 | + // create search parameters | ||
| 82 | 82 | Map<String, String> parameter = new HashMap<>(); | |
| 83 | 83 | parameter.put("q", "Coffee"); | |
| 84 | 84 | parameter.put("location", location); | |
| 85 | 85 | ||
| 86 | + // perform search | ||
| 86 | 87 | try { | |
| 87 | - // Perform search | ||
| 88 | - JsonObject data = serpapi.search(parameter); | ||
| 89 | - | ||
| 90 | - // Decode response | ||
| 91 | - JsonArray results = data.get("local_results").getAsJsonObject().get("places").getAsJsonArray(); | ||
| 92 | - JsonObject first_result = results.get(0).getAsJsonObject(); | ||
| 93 | - System.out.println("first coffee shop: " + first_result.get("title").getAsString() + " found on Google in " + location); | ||
| 88 | + // get search results | ||
| 89 | + JsonObject data = serpapi.search(parameter); | ||
| 90 | + JsonArray organic = data.getAsJsonArray("organic_results"); | ||
| 91 | + JsonObject first = organic.get(0).getAsJsonObject(); | ||
| 92 | + System.out.println("First result: " + first.get("title").getAsString() + " (search near " + location + ")"); | ||
| 94 | 93 | } catch (SerpApiException e) { | |
| 95 | - System.out.println("oops exception detected!"); | ||
| 94 | + System.out.println("SerpApi request failed."); | ||
| 96 | 95 | e.printStackTrace(); | |
| 96 | + System.exit(1); | ||
| 97 | 97 | } | |
| 98 | 98 | } | |
| 99 | 99 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -83,6 +83,6 @@ publishing { | |||
| 83 | 83 | } | |
| 84 | 84 | ||
| 85 | 85 | wrapper { | |
| 86 | - gradleVersion = "8.5" | ||
| 86 | + gradleVersion = "9.1.0" | ||
| 87 | 87 | distributionType = Wrapper.DistributionType.ALL | |
| 88 | 88 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,7 +1,8 @@ | |||
| 1 | 1 | # Ignore Gradle project-specific cache directory | |
| 2 | 2 | .gradle | |
| 3 | - gradle/ | ||
| 4 | - gradlew | ||
| 5 | 3 | ||
| 6 | 4 | # Ignore Gradle build output directory | |
| 7 | - build | ||
| 5 | + build/ | ||
| 6 | + | ||
| 7 | + # Local jars from `make dep` | ||
| 8 | + libs/ | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -6,10 +6,14 @@ else | |||
| 6 | 6 | $(info "found SERPAPI_KEY variable") | |
| 7 | 7 | endif | |
| 8 | 8 | ||
| 9 | + ifeq ($(strip $(SERPAPI_KEY)),) | ||
| 10 | + $(error "SERPAPI_KEY must be non-empty") | ||
| 11 | + endif | ||
| 12 | + | ||
| 9 | 13 | all: init clean build run | |
| 10 | 14 | ||
| 15 | + # Ensure wrapper script is executable (wrapper files should be committed under demo/) | ||
| 11 | 16 | init: | |
| 12 | - ../gradlew wrapper | ||
| 13 | 17 | chmod +x ./gradlew | |
| 14 | 18 | ||
| 15 | 19 | clean: | |
@@ -26,4 +30,4 @@ build: clean | |||
| 26 | 30 | @./gradlew build | |
| 27 | 31 | ||
| 28 | 32 | run: | |
| 29 | - @./gradlew run --args="$(SERPAPI_KEY)" | ||
| 33 | + @env SERPAPI_KEY="$(SERPAPI_KEY)" ./gradlew run | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -7,15 +7,18 @@ | |||
| 7 | 7 | */ | |
| 8 | 8 | ||
| 9 | 9 | plugins { | |
| 10 | - // Apply the java plugin to add support for Java | ||
| 11 | 10 | id 'java' | |
| 12 | - | ||
| 13 | - // Apply the application plugin to add support for building an application | ||
| 14 | 11 | id 'application' | |
| 15 | 12 | } | |
| 16 | 13 | ||
| 17 | - sourceCompatibility = 21 | ||
| 18 | - targetCompatibility = 21 | ||
| 14 | + java { | ||
| 15 | + sourceCompatibility = JavaVersion.VERSION_21 | ||
| 16 | + targetCompatibility = JavaVersion.VERSION_21 | ||
| 17 | + } | ||
| 18 | + | ||
| 19 | + application { | ||
| 20 | + mainClass = 'demo.App' | ||
| 21 | + } | ||
| 19 | 22 | ||
| 20 | 23 | repositories { | |
| 21 | 24 | mavenCentral() | |
@@ -24,13 +27,12 @@ repositories { | |||
| 24 | 27 | } | |
| 25 | 28 | ||
| 26 | 29 | dependencies { | |
| 27 | - // local jar file | ||
| 28 | - // implementation fileTree(dir: "./libs", includes: ['*.jar']) | ||
| 29 | - // download jar file from github over jitpack | ||
| 30 | - implementation 'com.github.serpapi:serpapi-java:1.0.0' | ||
| 31 | - // JSON dependencies | ||
| 30 | + def localJars = fileTree(dir: 'libs', include: '*.jar') | ||
| 31 | + if (!localJars.isEmpty()) { | ||
| 32 | + implementation localJars | ||
| 33 | + } else { | ||
| 34 | + implementation 'com.github.serpapi:serpapi-java:1.0.0' | ||
| 35 | + } | ||
| 32 | 36 | implementation group: 'com.google.code.gson', name: 'gson', version: '2.8.5' | |
| 33 | 37 | } | |
| 34 | 38 | ||
| 35 | - // Define the main class for the application | ||
| 36 | - mainClassName = 'demo.App' | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments