Skip to content

Commit

Permalink
added search maps
Browse files Browse the repository at this point in the history
  • Loading branch information
googio committed Nov 18, 2023
1 parent ec95097 commit bfbaf23
Show file tree
Hide file tree
Showing 2 changed files with 187 additions and 0 deletions.
76 changes: 76 additions & 0 deletions serply/serply.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ def __generate_url__(
return f"{self.base_url}{self.api_version}/job/search/{urlencode(params)}"
elif endpoint == "news":
return f"{self.base_url}{self.api_version}/news/{urlencode(params)}"
elif endpoint == "maps":
return f"{self.base_url}{self.api_version}/maps/{urlencode(params)}"
elif endpoint == "product":
# product doesn't take num
if "num" in params:
Expand Down Expand Up @@ -650,3 +652,77 @@ async def serp_async(

self.logger.debug(f"Performing async serp with {locals()}")
return await self.__make_request_async__(url=url)


def maps(
self,
keyword: str,
num: int = 10,
hl="lang_en",
gl="us",
lr="lang_en",
engine="google",
*args,
**kwargs,
) -> dict:
"""
search for jobs
NOTE: right now job only supports the English interface has to be in US or Canada
:param keyword: str: keywords to search for
:param num: int: number of results to return (max 100, defaults to 10)
:param engine: str: search engine to use (defaults to google) [google, bing]
:param start: int: start index for results (defaults to 0)
:return: dict: response from API
"""
results = {}
url = self.__generate_url__(
keyword=keyword,
num=num,
engine=engine,
hl=hl,
gl=gl,
lr=lr,
endpoint="maps",
*args,
**kwargs,
)

self.logger.debug(f"Performing job search with {locals()}")
return self.__make_request__(url=url)

async def maps_async(
self,
keyword: str,
num: int = 10,
hl="lang_en",
gl="us",
lr="lang_en",
engine="google",
*args,
**kwargs,
) -> dict:
"""
search for jobs
NOTE: right now job only supports the English interface has to be in US or Canada
:param keyword: str: keywords to search for
:param num: int: number of results to return (max 100, defaults to 10)
:param engine: str: search engine to use (defaults to google) [google, bing]
:param start: int: start index for results (defaults to 0)
:return: dict: response from API
"""
results = {}

url = self.__generate_url__(
keyword=keyword,
num=num,
engine=engine,
hl=hl,
gl=gl,
lr=lr,
endpoint="maps",
*args,
**kwargs,
)

self.logger.debug(f"Performing job async search with {locals()}")
return await self.__make_request_async__(url=url)
111 changes: 111 additions & 0 deletions tests/test_serply_maps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import os
import unittest
import pytest
import asyncio
import langid
from serply.serply import Serply

API_KEY = os.getenv("API_KEY", None)

serply = Serply(api_key=API_KEY)


def test_generate_simple_search_maps_url():
url = serply.__generate_url__(keyword="lawyers in new york city", endpoint="maps")
assert url == "https://api.serply.io/v1/maps/q=lawyers+in+new+york+city&num=10"


def test_generate_simple_search_maps_url_num_100():
url = serply.__generate_url__(keyword="apple store in dallas", endpoint="maps", num=100)
assert url == "https://api.serply.io/v1/maps/q=apple+store+in+dallas&num=100"


def test_generate_search_maps_url_start():
url = serply.__generate_url__(keyword="coffee shops", endpoint="maps", start=33)
assert url == "https://api.serply.io/v1/maps/q=coffee+shops&num=10&start=33"


def test_generate_search_maps_url_start_gl():
url = serply.__generate_url__(keyword="iphone", endpoint="maps", start=33, gl="de")
assert url == "https://api.serply.io/v1/maps/q=iphone&num=10&start=33&gl=de"


def test_simple_search_maps_default():
results = serply.maps(keyword="apple store")
assert results
assert "places" in results
assert len(results["places"]) > 0


def test_simple_search_maps_in_spanish():
results = serply.maps(keyword="iphone", lr="lang_es")
assert results
assert "places" in results
assert len(results["places"]) > 0

# expect one link to use https://www.apple.com/es/iphone/
for place in results["places"]:
if description := place["description"]:
detected_language, confidence = langid.classify(
" ".join(description).encode("utf-8")
)
if detected_language == "es":
assert True
return


def test_simple_search_maps_in_interface_german():
results = serply.maps(keyword="iphone", hl="lang_de", gl="de")
assert results
assert "places" in results
assert len(results["places"]) > 0

for place in results["places"]:
if description := place["description"]:
detected_language, confidence = langid.classify(
" ".join(description).encode("utf-8")
)
if detected_language == "de":
assert True
return



# test async versions
def test_simple_search_maps_default_async():
results = asyncio.run(serply.maps_async(keyword="iphone"))
assert results
assert "places" in results
assert len(results["places"]) > 0


def test_simple_search_maps_in_spanish_async():
results = asyncio.run(serply.maps_async(keyword="iphone", lr="lang_es"))
assert results
assert "places" in results
assert len(results["places"]) > 0

for place in results["places"]:
if description := place["description"]:
detected_language, confidence = langid.classify(
" ".join(description).encode("utf-8")
)
if detected_language == "es":
assert True
return


def test_simple_search_maps_in_interface_german_async():
results = asyncio.run(serply.maps_async(keyword="iphone", hl="lang_de", gl="de"))
assert results
assert "places" in results
assert len(results["places"]) > 0

for place in results["places"]:
if description := place["description"]:
detected_language, confidence = langid.classify(
" ".join(description).encode("utf-8")
)
if detected_language == "de":
assert True
return

0 comments on commit bfbaf23

Please sign in to comment.