public void TestGetJson() { client = new GoogleSearchResultsClient(ht, apiKey); JObject data = client.GetJson(); JArray coffeeShops = (JArray)data["local_results"]["places"]; int counter = 0; foreach (JObject coffeeShop in coffeeShops) { Assert.IsNotNull(coffeeShop["title"]); counter++; } Assert.IsTrue(counter >= 1); coffeeShops = (JArray)data["organic_results"]; Assert.IsNotNull(coffeeShops); foreach (JObject coffeeShop in coffeeShops) { Console.WriteLine("Found: " + coffeeShop["title"]); Assert.IsNotNull(coffeeShop["title"]); } // Release socket connection client.Close(); }
public void TestGetHtml() { client = new GoogleSearchResultsClient(ht, apiKey); string htmlContent = client.GetHtml(); Assert.IsNotNull(htmlContent); //Console.WriteLine(htmlContent); Assert.IsTrue(htmlContent.Contains("</body>")); // Release socket connection client.Close(); }
public void TestGetArchive() { // Skip test on travis ci if (apiKey == null || apiKey == "demo") { return; } client = new GoogleSearchResultsClient(ht, apiKey); JObject data = client.GetJson(); string id = (string)((JObject)data["search_metadata"])["id"]; JObject archivedSearch = client.GetSearchArchiveJson(id); int expected = GetSize((JArray)data["organic_results"]); int actual = GetSize((JArray)archivedSearch["organic_results"]); Assert.IsTrue(expected == actual); }
public void SearchWithSerpApi_ReturnSearchResultsListWithTenElements_WithNotEmptySearchString() { // Arrange var searchString = "test"; var ht = new Hashtable { { "q", searchString }, { "num", 15 } }; var apiKey = "tempApiKey"; var client = new GoogleSearchResultsClient(ht, apiKey); // Act var result = SearchWithApi.SearchWithSerpApi(searchString, client); // Assert Assert.IsType <List <SearchResult> >(result); Assert.Equal(10, result.Count); }
public void TestGetLocation() { client = new GoogleSearchResultsClient(apiKey); JArray locations = client.GetLocation("Austin,TX", 3); int counter = 0; foreach (JObject location in locations) { counter++; Assert.IsNotNull(location); Assert.IsNotNull(location.GetValue("id")); Assert.IsNotNull(location.GetValue("name")); Assert.IsNotNull(location.GetValue("google_id")); Assert.IsNotNull(location.GetValue("gps")); // Console.WriteLine(location); } Assert.AreEqual(1, counter); }
private async Task <List <SearchResult> > GetSearchWithApiResults(string searchString) { var ht = new Hashtable { { "q", searchString }, { "num", 20 } }; var googleClient = new GoogleSearchResultsClient(ht, _apiKey); var bingClient = new BingSearchResultsClient(ht, _apiKey); var resultTasks = new List <Task <List <SearchResult> > >() { SearchWithApi.SearchInYandexAsync(searchString, _yandexSearchString), Task.Run(() => SearchWithApi.SearchWithSerpApi(searchString, googleClient)), Task.Run(() => SearchWithApi.SearchWithSerpApi(searchString, bingClient)) }; var results = await Task.WhenAny(resultTasks).Result; return(results); }
// public GoogleReviewController(ILogger<GoogleReviewController> logger) // { // _logger = logger; // } private async Task getRestaurantsAsync() { List <dynamic> restaurants = new List <dynamic>(); var db = new DataContext(); String apiKey = "764751f0e06a193f008c3b1efff06d386119170ab0713878223d13367ce00b4e"; Hashtable ht = new Hashtable(); ht.Add("engine", "google_maps"); ht.Add("q", "pizza"); ht.Add("google_domain", "google.com"); ht.Add("ll", "@-27.4580119,153.0500812,15z"); ht.Add("type", "search"); try { GoogleSearchResultsClient client = new GoogleSearchResultsClient(ht, apiKey); JObject data = client.GetJson(); JArray results = (JArray)data["local_results"]; foreach (JObject result in results) { RestaurantReview review = new RestaurantReview(); review.title = result["title"].ToString(); review.rating = result["rating"].ToString(); review.reviews = result["reviews"].ToString(); restaurants.Add(review); db.Reviews.Add(review); await db.SaveChangesAsync(); } } catch (Exception ex) { Console.WriteLine("Exception:"); Console.WriteLine(ex.ToString()); } }
static void Main(string[] args) { // secret api key from https://serpapi.com/dashboard String apiKey = Environment.GetEnvironmentVariable("API_KEY"); if (apiKey == null) { Console.WriteLine("API_KEY environment variable be set your secret api key visit: https://serpapi.com/dashboard"); Environment.Exit(1); } // Localized search for Coffee shop in Austin Texas Hashtable ht = new Hashtable(); ht.Add("location", "Austin, Texas, United States"); ht.Add("q", "Coffee"); ht.Add("hl", "en"); ht.Add("google_domain", "google.com"); try { GoogleSearchResultsClient client = new GoogleSearchResultsClient(ht, apiKey); Console.WriteLine("Get location matching: Austin"); JArray locations = client.GetLocation("Austin,TX", 3); foreach (JObject location in locations) { Console.WriteLine(location); } Console.WriteLine("Search coffee in Austin, Texas on Google [1 credit]"); JObject data = client.GetJson(); Console.WriteLine("local coffee shop"); JArray coffeeShops = (JArray)data["local_results"]; foreach (JObject coffeeShop in coffeeShops) { Console.WriteLine("Found: " + coffeeShop["title"]); } Console.WriteLine("organic result coffee shop"); coffeeShops = (JArray)data["organic_results"]; foreach (JObject coffeeShop in coffeeShops) { Console.WriteLine("Found: " + coffeeShop["title"]); } string id = (string)((JObject)data["search_metadata"])["id"]; Console.WriteLine("Search from the archive: " + id + ". [0 credit]"); JObject archivedSearch = client.GetSearchArchiveJson(id); foreach (JObject coffeeShop in (JArray)archivedSearch["organic_results"]) { Console.WriteLine("Found: " + coffeeShop["title"]); } // Get account information Console.WriteLine("Account information: [0 credit]"); JObject account = client.GetAccount(); Dictionary <string, string> dictObj = account.ToObject <Dictionary <string, string> >(); foreach (string key in dictObj.Keys) { Console.WriteLine(key + " = " + dictObj[key]); } // write: // account_id = xx // api_key = xx // account_email = [email protected] // plan_id = // plan_name = No Plan // searches_per_month = 0 // this_month_usage = 0 // this_hour_searches = 1 // last_hour_searches = 0 } catch (SerpApiClientException ex) { Console.WriteLine("Exception:"); Console.WriteLine(ex.ToString()); } }