-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathSnapPlacesProvider.ts
More file actions
228 lines (213 loc) · 6.77 KB
/
Copy pathSnapPlacesProvider.ts
File metadata and controls
228 lines (213 loc) · 6.77 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// Import module
const placesModule = require("./Snapchat Places API Module")
import {getPhysicalDistanceBetweenLocations} from "./MapUtils"
export type Address = {
street_address: string
locality: string
region: string
postal_code: string
country: string
country_code: string
}
export type time = {
hour: number
minute: number
}
export type timeInterval = {
start_hour: time
end_hour: time
}
export type dayHours = {
day: string
hours: timeInterval[]
}
export type openingHours = {
dayHours: dayHours[]
time_zone: string
}
export type PlaceInfo = {
placeId: string
category: string
name: string
phone_number: string
address: Address
opening_hours: openingHours
centroid: GeoPosition
}
@component
export class SnapPlacesProvider extends BaseScriptComponent {
@input
private remoteServiceModule: RemoteServiceModule
private apiModule: any
private locationToPlaces: Map<GeoPosition, PlaceInfo[]> = new Map<GeoPosition, PlaceInfo[]>()
onAwake() {
this.createEvent("OnStartEvent").bind(() => {
this.apiModule = new placesModule.ApiModule(this.remoteServiceModule)
})
}
getNearbyPlacesInfo(
location: GeoPosition,
numberNearbyPlaces: number,
nearbyDistanceThreshold: number,
filter: string[] = null
): Promise<PlaceInfo[]> {
if (location.latitude === 0 && location.longitude === 0) {
return new Promise((resolve) => {
resolve([])
})
}
const nearbyPlaces = this.getNearbyPlacesFromCache(location, nearbyDistanceThreshold)
if (nearbyPlaces !== null) {
return new Promise((resolve) => {
resolve(nearbyPlaces)
})
} else {
return new Promise((resolve, reject) => {
this.getNearbyPlaces(location, numberNearbyPlaces, filter)
.then((places) => {
this.getPlacesInfo(places)
.then((places) => {
this.locationToPlaces.set(location, places)
resolve(places)
})
.catch((error) => {
reject(`Error getting places info: ${error}`)
})
})
.catch((error) => {
reject(`Error getting nearby places: ${error}`)
})
})
}
}
getNearbyPlaces(location: GeoPosition, numberNearbyPlaces: number, filter: string[] = null): Promise<any[]> {
return new Promise((resolve, reject) => {
this.apiModule
.get_nearby_places({
parameters: {
lat: location.latitude.toString(),
lng: location.longitude.toString(),
gps_accuracy_m: "100",
places_limit: numberNearbyPlaces.toString()
}
})
.then((response) => {
const results = response.bodyAsJson()
if (filter !== null) {
const places: any[] = []
;(results.nearbyPlaces as any[]).forEach((place) => {
const categoryName = place.categoryName as string
for (let i = 0; i < filter.length; i++) {
if (categoryName.includes(filter[i])) {
places.push(place)
break
}
}
})
resolve(places)
} else {
resolve(results.nearbyPlaces)
}
})
.catch((error) => {
reject(`Error retrieving nearby places: ${error}`)
})
})
}
getPlacesInfo(places: any[]): Promise<PlaceInfo[]> {
return new Promise((resolve, reject) => {
const promises: Promise<PlaceInfo>[] = []
places.forEach((place) => {
if (place.placeTypeEnum && place.placeTypeEnum === "VENUE") {
const getPlacePromise = new Promise<PlaceInfo>((resolve, reject) => {
this.apiModule
.get_place({
parameters: {
place_id: place.placeId
}
})
.then((response) => {
try {
const placeInfo = this.parsePlace(response.bodyAsString(), place.categoryName)
resolve(placeInfo)
} catch (error) {
reject(error)
}
})
.catch((error) => {
reject(error)
})
})
promises.push(getPlacePromise)
}
})
Promise.all(promises).then((places) => {
resolve(places)
})
})
}
private parsePlace(jsonString: string, categoryName: string): PlaceInfo {
const placeObject: any = JSON.parse(jsonString).place
const longlat = GeoPosition.create()
longlat.latitude = placeObject.geometry.centroid.lat
longlat.longitude = placeObject.geometry.centroid.lng
const place: PlaceInfo = {
placeId: placeObject.id,
category: categoryName,
name: placeObject.name,
phone_number: placeObject.contactInfo?.phoneNumber?.phoneNumber ?? "",
address: {
street_address: placeObject.address.address1,
locality: placeObject.address.locality,
region: placeObject.address.region,
postal_code: placeObject.address.postalCode,
country: placeObject.address.country,
country_code: placeObject.countryCode
},
opening_hours: placeObject.openingHours
? {
dayHours: placeObject.openingHours.dayHours
? placeObject.openingHours.dayHours.map((dayHour) => {
return {
day: dayHour.day,
hours: dayHour.hours.map((hour) => {
return {
start_hour: {
hour: hour.start?.hour ?? 0,
minute: hour.start?.minute ?? 0
},
end_hour: {
hour: hour.end?.hour ?? 0,
minute: hour.end?.minute ?? 0
}
}
})
}
})
: {},
time_zone: placeObject.openingHours.timeZone ? placeObject.openingHours.timeZone : ""
}
: {
dayHours: [],
time_zone: ""
},
centroid: longlat
}
return place
}
private getNearbyPlacesFromCache(
location: GeoPosition,
nearbyPlacesRefreshMinimumDistanceThreshold: number
): PlaceInfo[] | null {
let nearestDistance = Number.MAX_VALUE
let cachedNearbyPlaces: PlaceInfo[] | null = null
for (const cachedLocation of this.locationToPlaces.keys()) {
const distance = getPhysicalDistanceBetweenLocations(location, cachedLocation)
if (distance < nearestDistance) {
cachedNearbyPlaces = this.locationToPlaces.get(location)
nearestDistance = distance
}
}
return nearestDistance <= nearbyPlacesRefreshMinimumDistanceThreshold ? cachedNearbyPlaces : null
}
}