-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathget_city_informations.py
146 lines (111 loc) · 3.77 KB
/
get_city_informations.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
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import re
import sys
import json
import shutil
import urllib.request
CITYLIST="https://raw.githubusercontent.com/jesuisundesdeux/vigilo-conf/main/main/citylist.json"
countries = {
"France": "fr",
"Belgique": "be"
}
REMOVE_SYMBOLS_ACCENTS = {
'a':[u'â',u'à'],
'c':[u'ç'],
'e':[u'è',u'ê',u'é',u'ë'],
'i':[u'ï',u'î'],
'o':[u'ö',u'ô'],
'u':[u'û',u'ü'],
'-': ['-','\'','"','/','.']
}
MAKEFILE_ACTION="make generate-cities-content"
def normalize(text):
text = text.lower()
# Remove accent
for c in iter(REMOVE_SYMBOLS_ACCENTS):
for r in REMOVE_SYMBOLS_ACCENTS[c]:
text = text.replace(r,c)
# Remove multiple spaces
text = "-".join(text.split())
text = re.sub(r'<BR.*?>', ' ', text,flags=re.MULTILINE)
text = text.replace('PAS DE SAISIE','')
text = text.strip()
return text
def protectEmail(email):
email = email.replace("@"," ✉ ")
email = email.replace("."," ⊙ ")
return email
def downloadFile(url):
try:
response = urllib.request.urlopen(url, timeout=10)
data = response.read()
text = data.decode('utf-8')
return text
except Exception as err:
print('skip')
return False;
def createContentForCity(key, city_info, scope_info):
foldername = normalize("%s-%s" % (scope_info['display_name'],countries[city_info['country']]))
version = 'Beta'
if city_info['prod']:
version = 'inconnue'
if 'backend_version' in scope_info:
version = scope_info['backend_version']
contact_info = ""
if 'contact_email' in scope_info and scope_info['contact_email']!='':
contact_info = "- **Contact:** %s" % protectEmail(scope_info['contact_email'])
carte_info = ""
if 'map_url' in scope_info and scope_info['map_url']!='':
carte_info = "- **Carte:** %s" % scope_info['map_url']
api_scope = "- **Scope info:** %s/get_scope.php?scope=%s" % (city_info['api_path'],city_info['scope'])
opendata = '- **Données ouvertes:** <a href="%s/get_issues.php?scope=%s&format=json">JSON</a> - <a href="%s/get_issues.php?scope=%s&format=geojson">GeoJSON</a> - <a href="%s/get_issues.php?scope=%s&format=csv">CSV</a>' % (city_info['api_path'],city_info['scope'],city_info['api_path'],city_info['scope'],city_info['api_path'],city_info['scope'])
content = """---
title: %s (%s)
---
{{%% vigilo-map-url "%s" %%}}
{{%% vigilo-stats "%s" %%}}
{{%% get_issues "%s" "&scope=%s" %%}}
## Informations complémentaires
%s
%s
%s
%s
""" % (
scope_info['display_name'],
version,
scope_info['map_url'],
key,
city_info['api_path'],
city_info['scope'],
contact_info,
carte_info,
api_scope,
opendata
)
# Write content
foldercontent = "content/villes/%s" % foldername
os.makedirs(foldercontent,exist_ok=True)
with open("%s/_index.fr.md" % foldercontent, 'w') as f:
f.write(content)
# Add folder to gitignore
with open(".gitignore", 'a+') as f:
gitignoreline = "# Add from %s\n%s\n\n" % (MAKEFILE_ACTION,foldercontent)
f.write(gitignoreline)
if __name__ == "__main__":
data = downloadFile(CITYLIST)
jcity = json.loads(data)
# Init .gitignore
shutil.copyfile(".gitignore.tpl", ".gitignore")
# Get cities informations
for k in jcity:
city_info = jcity[k]
city_info['api_path'] = city_info['api_path'].replace('%3A%2F%2F','://')
if city_info['prod']:
print(k)
# Get scope information
data = downloadFile("%s/get_scope.php?scope=%s" % (city_info['api_path'],city_info['scope']))
if data:
scope_info = json.loads(data)
createContentForCity(k, city_info,scope_info)