-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapidae.rb
382 lines (340 loc) · 12.2 KB
/
apidae.rb
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# frozen_string_literal: true
# typed: true
require 'json'
require 'http'
require 'active_support/all'
require 'jsonpath'
require 'cgi'
require 'sorbet-runtime'
require_relative 'source'
class ApidaeSource < Source
class Settings < Source::SourceSettings
const :projet_id, String, name: 'projetId'
const :api_key, String, name: 'apiKey'
const :selection_id, String
const :website_details_url, T.nilable(String)
end
extend T::Generic
SettingsType = type_member{ { upper: Settings } } # Generic param
def jp(object, path)
JsonPath.on(object, "$.#{path}")
end
def self.build_url(path, query)
query = CGI.escape(query.to_json)
"https://api.apidae-tourisme.com/api/v002/#{path}/?query=#{query}"
end
def self.fetch(path, query)
url = build_url(path, query)
resp = HTTP.follow.get(url)
if !resp.status.success?
raise [url, resp].inspect
end
JSON.parse(resp.body)
end
def self.fetch_paged(path, query)
first = 0
count = 200 # Remote API max is 200
query = query.merge({ first: first, count: count })
next_url = T.let(
build_url(path, query),
T.nilable(String)
)
results = T.let([], T::Array[T.untyped])
while next_url
resp = HTTP.follow.get(next_url)
if !resp.status.success?
raise [next_url, resp].inspect
end
next_url = nil
json = JSON.parse(resp.body)
if json['objetsTouristiques']
results += json['objetsTouristiques']
first += count
query = query.merge({ first: first, count: count })
next_url = build_url(path, query)
end
end
results
end
def i18n_keys(object)
object&.transform_keys{ |key| key.gsub('libelle', '').downcase }
end
@@month = %w[Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec]
def self.date(date)
month = @@month[date[5..6].to_i - 1]
day = date[8..9]
[month, day].join(' ')
end
@@days = {
'LUNDI' => 'Mo',
'MARDI' => 'Tu',
'MERCREDI' => 'We',
'JEUDI' => 'Th',
'VENDREDI' => 'Fr',
'SAMEDI' => 'Sa',
'DIMANCHE' => 'Su',
}
@@day_month = {
'D_1ER' => 1,
'D_2EME' => 2,
'D_3EME' => 3,
'D_4EME' => 4,
'D_DERNIER' => -1,
}
def self.openning_days(ojs)
(ojs || []).collect{ |oj|
@@days[oj['jour']] + (oj['jourDuMois'].nil? ? '' : "[#{@@day_month[oj['jourDuMois']]}]")
}
end
@@exceptional_day = {
'PREMIER_JANVIER' => 'Jan 1',
'BERCHTOLDSTAG' => 'Jan 2', # Suisse
'SAINT_JOSEPH' => 'Mar 19', # Suisse
'VENDREDI_SAINT' => 'easter -2 day',
'LUNDI_PAQUES' => 'easter +1 day',
# 'ASCENSION' => '',
# 'LUNDI_PENTECOTE' => '',
'PREMIER_MAI' => 'May 1',
'HUIT_MAI' => 'May 8',
'QUATORZE_JUILLET' => 'Jul 14',
# 'FETE_DIEU' => '', # Suisse
'FETE_NATIONALE_SUISSE' => 'Aug 1', # Suisse
'QUINZE_AOUT' => 'Aug 15',
'LUNDI_DU_JEUNE_FEDERAL' => '', # Suisse
'PREMIER_NOVEMBRE' => 'Nov 1',
'ONZE_NOVEMBRE' => 'Nov 11',
'IMMACULEE_CONCEPTION' => 'Dec 8', # Suisse
'VINGT_CINQ_DECEMBRE' => 'Dec 25',
}
def self.exception_day(raw_date, day)
if raw_date.nil?
oed = @@exceptional_day[day]
if oed.nil?
logger.debug("Missing #{day}")
else
oed
end
else
date(raw_date)
end
end
def self.openning_hour(hour)
hour[0..4]
end
def self.openning(ouverture)
min_date_on = T.let(nil, T.nilable(String))
max_date_off = T.let(nil, T.nilable(String))
osm_openning_hours = (ouverture['periodesOuvertures'].collect { |po|
min_date_on = [min_date_on, po['dateDebut']].compact.min
max_date_off = po['tousLesAns'] ? nil : [max_date_off, po['dateFin']].compact.max
date_on = po['dateDebut'] && date(po['dateDebut'])
date_off = po['dateFin'] && date(po['dateFin'])
date_off = nil if date_on == date_off
date = [date_on, date_off].compact.join('-')
days = (
case po['type']
when 'OUVERTURE_TOUS_LES_JOURS' then nil
when 'OUVERTURE_SAUF' then (@@days.values - openning_days(po['ouverturesJournalieres'])).join(',')
when 'OUVERTURE_SEMAINE' then openning_days(po['ouverturesJournalieres']).join(',')
when 'OUVERTURE_MOIS' then openning_days(po['ouverturesJourDuMois']).join(',')
else raise po['type']
end
)
hour = (
if po['horaireOuverture']
openning_hour(po['horaireOuverture']) + (po['horaireFermeture'] ? "-#{openning_hour(po['horaireFermeture'])}" : '+')
elsif po['horaireFermeture']
"#{openning_hour(po['horaireFermeture'])}+"
end
)
[[date, days, hour].compact.join(' ')] +
(po['ouverturesExceptionnelles'] || {}).collect { |oe|
exception_day(oe['dateOuverture'], oe['dateSpeciale'])
}.compact.collect{ |day|
[day, hour].compact.join(' ')
}
} + (ouverture['fermeturesExceptionnelles'] || {}).collect { |fe|
exception_day(fe['dateFermeture'], fe['dateSpeciale'])
}.compact.collect{ |day|
"#{day} off"
}).join(';')
[min_date_on, max_date_off, osm_openning_hours]
end
# https://www.datatourisme.fr/ontology/core/#EntertainmentAndEvent
@@event_type = HashExcep[{
# SaleEvent
'Manifestations commerciales' => 'SaleEvent', # Generic
# '' => 'BricABrac',
# '' => 'FairOrShow',
# '' => 'Market',
# '' => 'OpenDay',
# '' => 'GarageSale',
# BusinessEvent
# '' => 'TrainingWorkshop',
# '' => 'ExecutiveBoardMeeting',
# '' => 'Congress',
# '' => 'BoardMeeting',
# '' => 'WorkMeeting',
# '' => 'Seminar',
# SocialEvent
'Distractions et loisirs' => 'SocialEvent', # Generic
# '' => 'LocalAnimation',
# '' => 'Carnival',
# '' => 'Parade',
'Traditions et folklore' => 'TraditionalCelebration',
# '' => 'PilgrimageAndProcession',
# '' => 'ReligiousEvent',
# CulturalEvent
'Culture' => 'CulturalEvent', # Generic
# '' => 'Concert',
# '' => 'Conference',
# '' => 'ArtistSigning',
# '' => 'ChildrensEvent',
# '' => 'Exhibition',
# '' => 'Festival',
# '' => 'Reading',
# '' => 'Opera',
# '' => 'TheaterEvent',
# '' => 'ScreeningEvent',
# '' => 'Recital',
# '' => 'VisualArtsEvent',
# '' => 'ShowEvent',
# '' => 'CircusEvent',
# '' => 'DanceEvent',
# '' => 'Harvest',
# SportsEvent
'Sports' => 'SportsEvent', # Generic
# '' => 'SportsCompetition',
# '' => 'SportsDemonstration',
# '' => 'Game',
# '' => 'Rally',
# '' => 'Rambling',
# Other. Not part of datatourisme ontology
'Nature et détente' => 'Other',
}]
def self.event(events)
events.collect{ |tm|
@@event_type[tm['libelleFr']]
}.compact
end
@@practice = HashExcep[{
# bicycle
'Sports cyclistes' => nil, # Generic for bicycle and mtb
'Itinéraire cyclo' => 'bicycle',
'Itinéraire de VTT à Assistance Électrique' => 'bicycle',
'Véloroute et voie verte' => 'bicycle',
'Itinéraire de Vélo à Assistance Electrique' => 'bicycle',
'Voie verte' => 'bicycle',
'Véloroute' => 'bicycle',
# mtb
'Itinéraire VTT' => 'mtb',
'Espace VTT' => 'mtb',
# horse
'Sports équestres' => 'horse', # Generic
'Itinéraire de randonnée équestre' => 'horse',
# hiking
'Sports pédestres' => 'hiking', # Generic
'Parcours d\'orientation' => 'hiking',
'Itinéraire de randonnée pédestre' => 'hiking',
'Parcours / sentier thématique' => 'hiking',
'Itinéraire de Trail' => 'hiking',
'Pôle trail' => 'hiking',
# other random data
'Sports d\'adresse' => nil,
'Paintball' => nil,
'Golf' => nil,
'Golf 18 trous' => nil,
'Sports divers' => nil,
}]
def self.practices(activites)
activites.collect{ |activite|
@@practice[activite['libelleFr']]
}.compact.uniq
end
def self.classs(clas)
clas = clas&.to_s
return unless clas != '9'
clas
end
def each
if ENV['NO_DATA']
[]
else
super(self.class.fetch_paged('recherche/list-objets-touristiques', {
projetId: @settings.projet_id,
apiKey: @settings.api_key,
selectionIds: [@settings.selection_id],
responseFields: ['@default', 'gestion', 'ouverture', 'multimedias'], # '@all' for debug with all fields
}))
end
end
def select(feat)
feat.dig('ouverture', 'fermeTemporairement') != 'FERME_TEMPORAIREMENT'
end
def map_id(feat)
feat['identifier']
end
def map_updated_at(feat)
feat.dig('gestion', 'dateModification')
end
def map_geometry(feat)
feat.dig('localisation', 'geolocalisation', 'geoJson')
end
def map_tags(feat)
r = feat
date_on, date_off, osm_openning_hours = r.dig('ouverture', 'periodesOuvertures').nil? ? [] : self.class.openning(r['ouverture'])
return nil if r['type'] == 'FETE_ET_MANIFESTATION' && date_on.nil? && date_off.nil?
practices = self.class.practices(jp(r, 'informationsEquipement.activites[*]')) if jp(r, 'informationsEquipement.itineraire')&.compact_blank.present?
{
name: i18n_keys(r['nom']),
description: i18n_keys(r.dig('presentation', 'descriptifCourt')),
website: jp(r, 'informations.moyensCommunication[*][?(@.type.libelleFr=="Site web (URL)")].coordonnees.fr'),
'website:details': { fr: @settings.website_details_url&.gsub('{{id}}', r['id'].to_s) }.compact_blank,
phone: jp(r, 'informations.moyensCommunication[*][?(@.type.libelleFr=="Téléphone")].coordonnees.fr'),
email: jp(r, 'informations.moyensCommunication[*][?(@.type.libelleFr=="Mél")].coordonnees.fr'),
facebook: jp(r, 'informations.moyensCommunication[*][?(@.type.libelleFr=="Page facebook")].coordonnees.fr').first,
image: jp(r, 'illustrations[*].traductionFichiers[0][?(@.locale=="fr")].urlDiaporama'),
addr: {
street: [
r.dig('localisation', 'adresse', 'adresse1'),
r.dig('localisation', 'adresse', 'adresse2'),
r.dig('localisation', 'adresse', 'adresse3'),
].compact_blank.join(', '),
postcode: r.dig('localisation', 'adresse', 'codePostal'),
city: r.dig('localisation', 'adresse', 'commune', 'nom'),
country: r.dig('localisation', 'adresse', 'commune', 'pays', 'libelleFr'),
}.compact_blank,
route: {
gpx_trace: jp(r, 'multimedias[*].traductionFichiers[*][?(@.extension=="gpx")].url').first,
pdf: practices.nil? ? nil : jp(r, 'multimedias[*].traductionFichiers[*][?(@.extension=="pdf")]').to_h{ |t| [t['locale'], t['url']] },
}.merge(practices.to_h{ |practice_slug|
[
practice_slug,
{
# "difficulty":
duration: jp(r, 'informationsEquipement.itineraire.dureeJournaliere').first,
length: jp(r, 'informationsEquipement.itineraire.distance').first,
}.compact_blank
]
} || {}).compact_blank,
'capacity:persons': (jp(r, 'informationsHebergementLocatif.capacite.capaciteHebergement').first || jp(r, 'informationsHebergementLocatif.capacite.capaciteMaximumPossible').first)&.nonzero?,
'capacity:rooms': (jp(r, 'informationsHebergementLocatif.capacite.nombreChambres').first || jp(r, 'informationsHotellerie.capacite.nombreChambresDeclareesHotelier').first)&.nonzero?,
'capacity:beds': [jp(r, 'informationsHebergementLocatif.capacite.nombreLitsSimples').first, jp(r, 'informationsHebergementLocatif.capacite.nombreLitsDoubles').first].compact_blank.presence&.sum&.nonzero?,
'capacity:pitches': jp(r, 'informationsHotelleriePleinAir.capacite.nombreEmplacementsClasses').first&.nonzero?,
opening_hours: osm_openning_hours,
start_date: r['type'] == 'FETE_ET_MANIFESTATION' ? date_on : nil,
end_date: r['type'] == 'FETE_ET_MANIFESTATION' ? date_off : nil,
stars: self.class.classs(
r.dig('informationsHotellerie', 'classement', 'ordre') ||
r.dig('informationsHebergementLocatif', 'classementPrefectoral', 'ordre') ||
r.dig('informationsHotelleriePleinAir', 'classement', 'ordre')
),
# event: r.dig('informationsFeteEtManifestation', 'typesManifestation').nil? ? nil : self.class.event(r.dig('informationsFeteEtManifestation', 'typesManifestation'))
}
end
def map_native_properties(feat, properties)
properties.transform_values{ |path|
jp(feat, path)
}.compact_blank
end
end