1- /*
2- * This Java source file was generated by the Gradle 'init' task.
3- */
41package demo ;
52
63import com .google .gson .JsonArray ;
4+ import com .google .gson .JsonElement ;
75import com .google .gson .JsonObject ;
86
97import java .util .Map ;
1210import serpapi .SerpApi ;
1311import serpapi .SerpApiException ;
1412
15- public class App {
16- public static void main (String [] args ) {
17- String apiKey = System .getenv ("SERPAPI_KEY" );
18- if (apiKey == null || apiKey .isEmpty ()) {
19- System .out .println ("Set environment variable SERPAPI_KEY or pass your key as the only argument." );
20- System .exit (1 );
21- }
13+ import java .util .regex .Matcher ;
14+ import java .util .regex .Pattern ;
2215
23- // set search location
24- String location = "Austin,Texas" ;
25- String engine = "google" ;
26- System .out .println ("find the first coffee shop in " + location + " using " + engine );
27-
28- Map <String , String > auth = new HashMap <>();
29- auth .put ("engine" , engine );
30- auth .put ("api_key" , apiKey );
31-
32- // create client
33- SerpApi serpapi = new SerpApi (auth );
34-
35- // create search parameters
36- Map <String , String > parameter = new HashMap <>();
37- parameter .put ("q" , "Coffee" );
38- parameter .put ("location" , location );
16+ class App {
17+ public static void main (String [] args ) {
18+ String apiKey = "token here" ;
19+ String engine = "google" ;
20+ System .out .println ("Using Offical Java Library" );
21+ Map <String , String > auth = new HashMap <>();
22+ auth .put ("engine" , engine );
23+ auth .put ("api_key" , apiKey );
24+
25+ SerpApi serpapi = new SerpApi (auth );
26+ Map <String , String > parameter = new HashMap <>();
27+ parameter .put ("q" , "grace hopper birthday" );
28+
29+ try {
30+ JsonObject data = serpapi .search (parameter );
31+ String foundText = findDateValueInJson (data );
32+
33+ if (foundText != null ) {
34+ // Apply regex pattern to capture strictly: Month DD, YYYY
35+ Pattern datePattern = Pattern .compile ("(January|February|March|April|May|June|July|August|September|October|November|December)\\ s+\\ d{1,2},\\ s+\\ d{4}" );
36+ Matcher matcher = datePattern .matcher (foundText );
37+
38+ if (matcher .find ()) {
39+ System .out .println ("Grace Hopper's Birthday: " + matcher .group (0 ));
40+ } else {
41+ System .out .println ("Grace Hopper's Birthday: " + foundText );
42+ }
43+ } else {
44+ System .out .println ("Could not extract any date from the response." );
45+ }
46+ } catch (SerpApiException e ) {
47+ System .out .println ("SerpApi request failed." );
48+ e .printStackTrace ();
49+ System .exit (1 );
50+ }
51+ }
3952
40- // perform search
41- try {
42- // get search results
43- JsonObject data = serpapi .search (parameter );
44- JsonArray organic = data .getAsJsonArray ("organic_results" );
45- JsonObject first = organic .get (0 ).getAsJsonObject ();
46- System .out .println ("First result: " + first .get ("title" ).getAsString () + " (search near " + location + ")" );
47- } catch (SerpApiException e ) {
48- System .out .println ("SerpApi request failed." );
49- e .printStackTrace ();
50- System .exit (1 );
53+ private static String findDateValueInJson (JsonElement element ) {
54+ if (element .isJsonObject ()) {
55+ JsonObject obj = element .getAsJsonObject ();
56+ if (obj .has ("born" )) return obj .get ("born" ).getAsString ();
57+ if (obj .has ("birthday" )) return obj .get ("birthday" ).getAsString ();
58+ if (obj .has ("date_of_birth" )) return obj .get ("date_of_birth" ).getAsString ();
59+
60+ for (Map .Entry <String , JsonElement > entry : obj .entrySet ()) {
61+ if (entry .getValue ().isJsonPrimitive ()) {
62+ String value = entry .getValue ().getAsString ();
63+ if (value .contains ("1906" ) || value .contains ("December" )) {
64+ return value ;
65+ }
66+ }
67+ String deepResult = findDateValueInJson (entry .getValue ());
68+ if (deepResult != null ) return deepResult ;
69+ }
70+ } else if (element .isJsonArray ()) {
71+ JsonArray arr = element .getAsJsonArray ();
72+ for (JsonElement item : arr ) {
73+ String deepResult = findDateValueInJson (item );
74+ if (deepResult != null ) return deepResult ;
75+ }
5176 }
77+ return null ;
5278 }
53- }
79+ }
0 commit comments