-
Notifications
You must be signed in to change notification settings - Fork 53
/
google_places.py
81 lines (73 loc) · 2.35 KB
/
google_places.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import requests
import json
import config
from collections import namedtuple
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
stop_words = set(stopwords.words('english'))
def nearby_places(
api_key,
location,
query,
latitude,
longitude,
sort_by="prominence"):
url = "https://maps.googleapis.com/maps/api/place/textsearch/json?"
if "near" in query:
sort_by = "distance"
req = "{url}query='{query}'&location={latitude},{longitude}&rankby={rankby}&key={key}".format(
url=url,
query=query,
latitude=latitude,
longitude=longitude,
key=api_key,
rankby=sort_by
)
# print(req)
r = requests.get(req)
x = r.json()
y = x['results']
arr = []
count = 5
for i in range(min(count, len(y))):
arr.append(y[i]['name'])
return arr, y
def get_location():
# {u'city': u'Hyderabad', u'longitude':78.4744,
# u'latitude': 17.3753, u'state': u'Telangana', u'IPv4': u'157.48.48.45',
# u'country_code': u'IN', u'country_name': u'India', u'postal': u'500025'}
url = config.geolocation_api
response = requests.get(url)
data = response.json()
data_named = namedtuple("User", data.keys())(*data.values())
return data_named, data['longitude'], data['latitude']
def filter_sentence(text):
word_tokens = word_tokenize(text)
filtered_sentence = [w for w in word_tokens if w not in stop_words]
ans = ""
for x in filtered_sentence:
ans = ans + x + " "
return ans
def change_location_query(address, key):
url = "https://maps.googleapis.com/maps/api/geocode/json?address={}&key={}".format(
address, key)
r = requests.get(url)
x = r.json()
user = namedtuple('User', 'city longitude latitude place_id')
try:
x = x['results']
location = x[0]['address_components'][2]['long_name']
lattitude = x[0]['geometry']['location']['lat']
longitude = x[0]['geometry']['location']['lng']
return user(
city=location,
longitude=longitude,
latitude=lattitude,
place_id=x[0]['place_id'])
except Exception as e:
print(e)
return user(
city="Hyderabad",
longitude=17.44,
latitude=78.34,
place_id="ChIJK_h_8QMKzjsRlrTemJvoAp0")