Exemple #1
0
 def test_get_dict(self):
     client = GoogleSearchResults({
         "q": "Coffee",
         "location": "Austin,Texas"
     })
     data = client.get_dict()
     self.assertIsNotNone(data.get('local_results'))
Exemple #2
0
    def test_async(self):
        # store searches
        search_queue = Queue()

        # Serp API search
        search = GoogleSearchResults({
            "location": "Austin,Texas",
            "async": True
        })

        # loop through companies
        for company in ['amd', 'nvidia', 'intel']:
            print("execute async search: q = " + company)
            search.params_dict["q"] = company
            data = search.get_dict()
            print("add search to the queue where id: " +
                  data['search_metadata']['id'])
            # add search to the search_queue
            search_queue.put(data)

        print("wait until all search statuses are cached or success")

        # Create regular search
        search = GoogleSearchResults({"async": True})
        while not search_queue.empty():
            data = search_queue.get()
            search_id = data['search_metadata']['id']

            # retrieve search from the archive - blocker
            print(search_id + ": get search from archive")
            search_archived = search.get_search_archive(search_id)
            print(search_id + ": status = " +
                  search_archived['search_metadata']['status'])

            # check status
            if re.search('Cached|Success',
                         search_archived['search_metadata']['status']):
                print(search_id + ": search done with q = " +
                      search_archived['search_parameters']['q'])
            else:
                # requeue search_queue
                print(search_id + ": requeue search")
                search_queue.put(search)
                # wait 1s
                time.sleep(1)
        # search is over.
        print('all searches completed')
Exemple #3
0
import sys
import os
from serpapi import GoogleSearchResults

# Run Out Of Box testing
#  Load package
#  Run simple query
#
import pprint
print("initialize serpapi search")
search = GoogleSearchResults({
    "q": "coffee",
    "location": "Austin,Texas",
    "api_key": os.getenv("API_KEY", "demo")
})
print("execute search")
result = search.get_dict()
print("display result")
pp = pprint.PrettyPrinter(indent=2)
pp.pprint(result)
print("------")
if len(result) > 0:
    print("OK: Out out box testing is passing")
    sys.exit(0)

print("FAIL: Out box testing is failing: no result")
sys.exit(1)