Skip to content

Commit

Permalink
[Naver] Fix metadata requests 403ing
Browse files Browse the repository at this point in the history
  • Loading branch information
sk-zk committed Sep 18, 2024
1 parent 0dab54c commit 137a1e7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
32 changes: 22 additions & 10 deletions streetlevel/naver/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

from ..util import get_json, get_json_async

headers = {"Referer": "https://map.naver.com"}


def build_find_panorama_request_url(lat: float, lon: float) -> str:
return f"https://map.naver.com/p/api/panorama/nearby/{lon}/{lat}"
Expand All @@ -25,40 +27,50 @@ def build_depth_request_url(panoid: str) -> str:


def find_panorama(lat: float, lon: float, session: Session = None) -> dict:
return get_json(build_find_panorama_request_url(lat, lon), session=session)
return get_json(build_find_panorama_request_url(lat, lon), session=session,
headers={"Referer": "https://map.naver.com"})


async def find_panorama_async(lat: float, lon: float, session: ClientSession) -> dict:
return await get_json_async(build_find_panorama_request_url(lat, lon), session)
return await get_json_async(build_find_panorama_request_url(lat, lon), session,
headers={"Referer": "https://map.naver.com"})


def find_panorama_by_id(panoid: str, language: str, session: Session = None) -> dict:
return get_json(build_find_panorama_by_id_request_url(panoid, language), session=session)
return get_json(build_find_panorama_by_id_request_url(panoid, language), session=session,
headers={"Referer": "https://map.naver.com"})


async def find_panorama_by_id_async(panoid: str, language: str, session: ClientSession) -> dict:
return await get_json_async(build_find_panorama_by_id_request_url(panoid, language), session)
return await get_json_async(build_find_panorama_by_id_request_url(panoid, language), session,
headers={"Referer": "https://map.naver.com"})


def get_historical(panoid: str, session: Session = None) -> dict:
return get_json(build_timeline_request_url(panoid), session=session)
return get_json(build_timeline_request_url(panoid), session=session,
headers={"Referer": "https://map.naver.com"})


async def get_historical_async(panoid: str, session: ClientSession) -> dict:
return await get_json_async(build_timeline_request_url(panoid), session)
return await get_json_async(build_timeline_request_url(panoid), session,
headers={"Referer": "https://map.naver.com"})


def get_neighbors(panoid: str, session: Session = None) -> dict:
return get_json(build_around_request_url(panoid), session=session)
return get_json(build_around_request_url(panoid), session=session,
headers={"Referer": "https://map.naver.com"})


async def get_neighbors_async(panoid: str, session: ClientSession) -> dict:
return await get_json_async(build_around_request_url(panoid), session)
return await get_json_async(build_around_request_url(panoid), session,
headers={"Referer": "https://map.naver.com"})


def get_depth(panoid: str, session: Session = None) -> dict:
return get_json(build_depth_request_url(panoid), session=session)
return get_json(build_depth_request_url(panoid), session=session,
headers={"Referer": "https://map.naver.com"})


async def get_depth_async(panoid: str, session: ClientSession) -> dict:
return await get_json_async(build_depth_request_url(panoid), session)
return await get_json_async(build_depth_request_url(panoid), session,
headers=headers)
13 changes: 9 additions & 4 deletions streetlevel/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,19 @@ async def get_image_async(url: str, session: ClientSession) -> Image.Image:
return Image.open(BytesIO(await response.read()))


def get_json(url: str, session: Session = None, preprocess_function: Callable = None) -> dict:
def get_json(url: str, session: Session = None, preprocess_function: Callable = None,
headers: dict = None) -> dict:
"""
Fetches JSON from a URL using requests.
:param url: The URL.
:param session: *(optional)* A requests session.
:param preprocess_function: *(optional)* A function to run on the text before passing it to the JSON parser.
:param headers: *(optional)* Request headers.
:return: The requested document as dictionary.
"""
requester = session if session else requests
response = requester.get(url)
response = requester.get(url, headers=headers)
if preprocess_function:
processed = preprocess_function(response.text)
return json.loads(processed)
Expand All @@ -84,17 +86,20 @@ def get_json(url: str, session: Session = None, preprocess_function: Callable =


async def get_json_async(url: str, session: ClientSession, json_function_parameters: dict = None,
preprocess_function: Callable = None) -> dict:
preprocess_function: Callable = None, headers: dict = None) -> dict:
"""
Fetches JSON from a URL using aiohttp.
:param url: The URL.
:param session: A ``ClientSession``.
:param json_function_parameters: *(optional)* Parameters to pass to the ``ClientResponse.json`` function.
:param preprocess_function: *(optional)* A function to run on the text before passing it to the JSON parser.
:param headers: *(optional)* Request headers.
:return: The requested document as dictionary.
"""
async with session.get(url) as response:
if headers is None:
headers = {}
async with session.get(url, headers=headers) as response:
if preprocess_function:
text = await response.text()
processed = preprocess_function(text)
Expand Down

0 comments on commit 137a1e7

Please sign in to comment.