Skip to content

Commit e88f11f

Browse files
query google_maps_directions (#29)
* query google_maps_directions * refactoring * added example * renamed
1 parent 157e85f commit e88f11f

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

examples/Google Directions.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Google Directions With Python
2+
3+
Returns directions between two points from Google Maps. [Outscraper API](https://app.outscraper.cloud/api-docs#tag/Google/paths/~1maps~1directions/get).
4+
5+
## Installation
6+
7+
Python 3+
8+
```bash
9+
pip install outscraper
10+
```
11+
12+
[Link to the Python package page](https://pypi.org/project/outscraper/)
13+
14+
## Initialization
15+
```python
16+
from outscraper import ApiClient
17+
18+
client = ApiClient(api_key='SECRET_API_KEY')
19+
```
20+
[Link to the profile page to create the API key](https://app.outscraper.com/profile)
21+
22+
## Usage
23+
24+
```python
25+
# Returns directions:
26+
results = client.google_maps_directions([
27+
['29.696596, 76.994928', '30.715966244353, 76.8053887016268'],
28+
['29.696596, 76.994928', '30.723065, 76.770169']
29+
])
30+
```

outscraper/api_client.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from time import sleep
33
from typing import Union, Tuple
44

5-
from .utils import as_list, parse_fields
5+
from .utils import as_list, parse_fields, format_direction_queries
66

77

88
class ApiClient(object):
@@ -323,7 +323,8 @@ def google_maps_directions(self, query: Union[list, str], departure_time: int =
323323
324324
See: https://app.outscraper.com/api-docs#tag/Google/paths/~1maps~1directions/get
325325
'''
326-
queries = as_list(query)
326+
327+
queries = format_direction_queries(query)
327328
wait_async = async_request or len(queries) > 10
328329

329330
response = requests.get(f'{self._api_url}/maps/directions', params={

outscraper/utils.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from typing import Union
22

3+
QUERY_DELIMITER = ' '
34

45
def as_list(value: Union[list, str]) -> list:
56
if isinstance(value, list):
@@ -13,3 +14,11 @@ def parse_fields(fields: Union[list, str]) -> str:
1314
return ','.join(fields)
1415
else:
1516
return fields
17+
18+
19+
def format_direction_queries(q: Union[list, str]) -> list[str]:
20+
if isinstance(q, list):
21+
if all(isinstance(i, list) for i in q):
22+
return [QUERY_DELIMITER.join(pair) for pair in q]
23+
return q
24+
return [q]

0 commit comments

Comments
 (0)