-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNeshanAPI.py
57 lines (50 loc) · 2.09 KB
/
NeshanAPI.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import requests
__author__='[email protected]'
__repository__='https://github.com/benyaminsalimi/Neshan-Python'
class NeshanAPI(object):
def __init__(self,key):
self.header = {}
self.header['Api-Key'] = key
self.header['user-agent'] = 'Neshan API Python lib'
self.header['Content-Type'] = 'application/x-www-form-urlencoded'
self.api_url = 'https://api.neshan.org/v1/'
def req(self,url,param=None):
url = self.api_url + url
req = requests.get(url=url, headers=self.header, params=param)
try:
result = req.json()
except:
result = {'status':'Api return empty value with 200 status code!'}
return result
def Location_Based(self, term, latitude, longitude):
url = 'search'
param = {}
param['term'] = term
param['lat'] = latitude
param['lng'] = longitude
return self.req(url=url,param=param)
def Reverse_Geocoding(self,latitude, longitude):
url = 'reverse'
param = {}
param['lat'] = latitude
param['lng'] = longitude
return self.req(url=url, param=param)
def Direction(self,origin_latitude,origin_longitude,destination_latitude,destination_longitude,alternative=None):
url = 'routing'
param = {}
if alternative is True:
param['alternative'] = 'true'
# latitude,longtitude
param['origin'] = origin_latitude+','+origin_longitude
param['destination'] = destination_latitude+','+destination_longitude
return self.req(url=url, param=param)
def Distance_Duration(self,origin_latitude,origin_longitude,destination_latitude,destination_longitude):
url = 'routing'
param = {}
# latitude,longtitude
param['origin'] = origin_latitude + ',' + origin_longitude
param['destination'] = destination_latitude + ',' + destination_longitude
r = self.req(url=url,param=param)
distance = r['routes'][0]['legs'][0]['distance']
duration = r['routes'][0]['legs'][0]['duration']
return distance, duration