-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain3.py
More file actions
396 lines (294 loc) · 14.6 KB
/
Copy pathmain3.py
File metadata and controls
396 lines (294 loc) · 14.6 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
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
383
# -*- coding: utf-8 -*-
"""
Created on Thu Feb 25 23:48:17 2021
Test Bevouac
@author: MEHDI MAAREF
à l'attention de Mr FlorianBreton
"""
""""Module 1"""
#imports
import csv
import json
import tempfile
import numpy as np
import pandas as pd
# IMPORTING MODULES
import os
import zipfile
import tarfile
import gzip
import shutil
import requests
#extraction du fichier suivant l'url et téléchargement dans le fichier souhaité
# ARCHIVE EXTENSIONS
ZIP_EXTENSION = ".zip"
TAR_EXTENSION = ".tar"
TAR_GZ_EXTENSION = ".tar.gz"
TGZ_EXTENSION = ".tgz"
GZ_EXTENSION = ".gz"
EMPTY_URL_ERROR = "ERROR: URL should not be empty."
FILENAME_ERROR = "ERROR: Filename should not be empty."
UNKNOWN_FORMAT = "ERROR: Unknown file format. Can't extract."
with tempfile.TemporaryDirectory() as tmpdirname:
print('created temporary directory', tmpdirname)
#enregistrer le fichier souhaité dans le fichier concerné dans notre cas enregistré dans le fichier de téléchargement
def download_dataset(url, target_path="data/", keep_download=True, overwrite_download=False):
"""Downloads dataset from a url.
url: string, a dataset path
target_path: string, path where data will be downloaded
keep_download: boolean, keeps the original file after extraction
overwrite_download: boolean, stops download if dataset already exists
"""
if url == "" or url is None:
raise Exception(EMPTY_URL_ERROR)
filename = get_filename(url)
file_location = get_file_location(target_path, filename)
os.makedirs(tmpdirname, exist_ok=True) #your downloading target path
if os.path.exists(file_location) and not overwrite_download:
print(f"File already exists at {file_location}. Use: 'overwrite_download=True' to \
overwrite download")
extract_file(target_path, filename)
return
print(f"Downloading file from {url} to {file_location}.")
# Download
with open(file_location, 'wb') as f:
with requests.get(url, allow_redirects=True, stream=True) as resp:
for chunk in resp.iter_content(chunk_size = 512): #chunk_size in bytes
if chunk:
f.write(chunk)
print("Finished downloading.")
print("Extracting the file now ...")
extract_file(os.path.join(tmpdirname, '') , filename)
if not keep_download:
os.remove(file_location)
def extract_file(target_path, filename):
"""Extract file based on file extension
target_path: string, location where data will be extracted
filename: string, name of the file along with extension
"""
if filename == "" or filename is None:
raise Exception(FILENAME_ERROR)
file_location = get_file_location(target_path, filename)
if filename.endswith(ZIP_EXTENSION):
print("Extracting zip file...")
zipf = zipfile.ZipFile(file_location, 'r')
zipf.extractall(target_path)
zipf.close()
print(' E N D')
elif filename.endswith(TAR_EXTENSION) or \
filename.endswith(TAR_GZ_EXTENSION) or \
filename.endswith(TGZ_EXTENSION):
print("Extracting tar file")
tarf = tarfile.open(file_location, 'r')
tarf.extractall(target_path)
tarf.close()
print(' E N D')
elif filename.endswith(GZ_EXTENSION):
print("Extracting gz file")
out_file = file_location[:-3]
with open(file_location, "rb") as f_in:
with open(out_file, "wb") as f_out:
shutil.copyfileobj(f_in, f_out)
print(' E N D')
else:
print(UNKNOWN_FORMAT)
def get_filename(url):
"""Extract filename from file url"""
filename = os.path.basename(url)
return filename
def get_file_location(target_path, filename):
""" Concatenate download directory and filename"""
return target_path + filename
#download information and locations of the file (temporary directory)
print('name of file: ',get_filename("https://cadastre.data.gouv.fr/data/etalab-dvf/latest/csv/2020/full.csv.gz"))
download_dataset("https://cadastre.data.gouv.fr/data/etalab-dvf/latest/csv/2020/full.csv.gz",os.path.join(tmpdirname, ''),keep_download=True, overwrite_download=False)
pass #pass the ignoring error message: the python code does not recognize the path, because the file is temporary
#this normally is not recommended, but we use it to overcome the error messages and download the dataset
#properly
#Traitement data
file_location = get_file_location(os.path.join(tmpdirname, ''), 'full.csv.gz')#obtaining file location
#pandas support zip file reads
#reading file
data_splited = pd.read_csv(file_location)
data_splited #voici le fichier avec ses caractéristiques
print(data_splited.shape, 'shape of data')
#del(data_splited['my_name'])#adding column
data_splited
data_splited['NOM_CANDIDAT'] = 'MAAREF'#adding column with my name
data_splited = data_splited[['NOM_CANDIDAT','id_mutation', 'date_mutation', 'numero_disposition', 'nature_mutation',
'valeur_fonciere', 'adresse_numero', 'adresse_suffixe',
'adresse_nom_voie', 'adresse_code_voie', 'code_postal', 'code_commune',
'nom_commune', 'code_departement', 'ancien_code_commune',
'ancien_nom_commune', 'id_parcelle', 'ancien_id_parcelle',
'numero_volume', 'lot1_numero', 'lot1_surface_carrez', 'lot2_numero',
'lot2_surface_carrez', 'lot3_numero', 'lot3_surface_carrez',
'lot4_numero', 'lot4_surface_carrez', 'lot5_numero',
'lot5_surface_carrez', 'nombre_lots', 'code_type_local', 'type_local',
'surface_reelle_bati', 'nombre_pieces_principales',
'code_nature_culture', 'nature_culture', 'code_nature_culture_speciale',
'nature_culture_speciale', 'surface_terrain', 'longitude', 'latitude',
]]#new column NOM_CANDIDAT in the first position
#new data frame with column NOM_CANDIDAT added
adresse = pd.concat([data_splited["adresse_numero"], data_splited["adresse_nom_voie"], data_splited["nom_commune"],data_splited["code_postal"]], axis=1)
adresse["pays"] = 'FRANCE' #adding the column 'pays'
adresse['code_postal'] = adresse['code_postal'].values.astype(int) #converting postal code to int
#creating a dataframe adresse that we will use to merge the columns
data_splited['adresse_string'] = adresse[adresse.columns[0:]].apply(
lambda x: ' '.join(x.dropna().astype(str)),
axis=1) #merging different columns from the data frame adresse and creating the column 'adresse_string'
#data tranformed with the right form
#NUMERO_RUE NOM_RUE NOM_VILLE CODE_POSTAL PAYS
#delete Nan lines from the dataframe in the columns 'longitudes ' and 'latitudes'
data_splited.dropna(subset=["longitude"], axis=0, inplace=True)
data_splited.dropna(subset=["latitude"], axis=0, inplace=True)
data_splited['valeur_fonciere'] = data_splited['valeur_fonciere'].values.astype(int)
data_splited['longitude'] = data_splited['longitude'].values.astype(float)
data_splited['latitude'] = data_splited['latitude'].values.astype(float)
print(data_splited.shape, '--> new shape after dropping NaN values in columns longitude and latitude')
data_splited.reset_index(drop=True, inplace=True)#reset index with new number of lines
"""Module 2 - import de données"""
#data_splited['date_mutation'] = pd.to_datetime(data_splited['date_mutation'])
data_splited = data_splited.sort_values('date_mutation')
data_splited.reset_index(drop=True, inplace=True)#reset index with new number of lines
#data_splited.head(500)#We use head to obtain the 500 recent dates
"""API Module 2"""
with tempfile.TemporaryDirectory() as tmpdirname2:
data_splited.head(500).to_csv(os.path.join(tmpdirname2, '')+"Data_module2.csv")
f = open(os.path.join(tmpdirname2, '')+"Data_module2.csv")
print('make sure io.wrapper is convenient type', type(f))
post_url = "https://api.airtable.com/v0/appqmv1skloVlLyKV/DVF"
post_headers = {
"Authorization" : "Bearer key9SsqMTpaKecsn0",
"Content-Type": "application/json"
}
#f = open('C:\\Users\Lenovo\Desktop\Bevouac\Data_module2.csv')
csv_f = csv.DictReader(f, delimiter=',')
for row in csv_f:
data = {
"records": [
{
"fields": {
"NOM_CANDIDAT": row['NOM_CANDIDAT'],
"id_mutation": row['id_mutation'],
"date_mutation": row['date_mutation'],
"numero_disposition": int(row['numero_disposition']),
"nature_mutation": row['nature_mutation'],
"valeur_fonciere": int(row['valeur_fonciere']),
"adresse_numero": row['adresse_numero'],
"adresse_suffixe": row['adresse_suffixe'],
"adresse_nom_voie": row['adresse_nom_voie'],
"adresse_code_voie": row['adresse_code_voie'],
"code_postal": row['code_postal'],
"code_commune": row['code_commune'],
"nom_commune": row['nom_commune'],
"code_departement": row['code_departement'],
"ancien_code_commune": row['ancien_code_commune'],
"ancien_nom_commune": row['ancien_nom_commune'],
"id_parcelle": row['id_parcelle'],
"ancien_id_parcelle": row['ancien_id_parcelle'],
"numero_volume": row['numero_volume'],
"lot1_numero": row['lot1_numero'],
"lot1_surface_carrez": row['lot1_surface_carrez'],
"lot2_numero": row['lot2_numero'],
"lot2_surface_carrez": row['lot2_surface_carrez'],
"lot3_numero": row['lot3_numero'],
"lot3_surface_carrez": row['lot3_surface_carrez'],
"lot4_numero": row['lot4_numero'],
"lot4_surface_carrez": row['lot4_surface_carrez'],
"lot5_numero": row['lot5_numero'],
"lot5_surface_carrez": row['lot5_surface_carrez'],
"nombre_lots": row['nombre_lots'],
"code_type_local": row['code_type_local'],
"type_local": row['type_local'],
"surface_reelle_bati": row['surface_reelle_bati'],
"nombre_pieces_principales": row['nombre_pieces_principales'],
"code_nature_culture": row['code_nature_culture'],
"nature_culture": row['nature_culture'],
"code_nature_culture_speciale": row['code_nature_culture_speciale'],
"nature_culture_speciale": row['nature_culture_speciale'],
"surface_terrain": row['surface_terrain'],
"longitude": float(row['longitude']),
"latitude": float(row['latitude']),
"adresse_string": row['adresse_string']
}
},
],
}
print(post_url)
print(data)
post_airtable_request = requests.post(post_url, headers = post_headers, json = data)
print(post_airtable_request.status_code)
"""Module 3 : Flux de données"""
#code of module 3
#print 500 new lines for example
with open("Data_module2.csv", "r") as readfile:
lines = readfile.readlines()
list_num = len((lines))
with tempfile.TemporaryDirectory() as tmpdirname3:
new_data = data_splited.loc[list_num:list_num+500,:]#concaténation des lignes inédites + 500 nouvelles lignes
new_data.to_csv(os.path.join(tmpdirname3, '')+"new_data.csv")
f2 = open(os.path.join(tmpdirname3, '')+"new_data.csv")
print('make sure io.wrapper is convenient type', type(f2))
post_url = "https://api.airtable.com/v0/appqmv1skloVlLyKV/DVF"
post_headers = {
"Authorization" : "Bearer key9SsqMTpaKecsn0",
"Content-Type": "application/json"
}
#f = open('C:\\Users\Lenovo\Desktop\Bevouac\Data_module2.csv')
csv_f2 = csv.DictReader(f2, delimiter=',')
for row in csv_f2:
data = {
"records": [
{
"fields": {
"NOM_CANDIDAT": row['NOM_CANDIDAT'],
"id_mutation": row['id_mutation'],
"date_mutation": row['date_mutation'],
"numero_disposition": int(row['numero_disposition']),
"nature_mutation": row['nature_mutation'],
"valeur_fonciere": int(row['valeur_fonciere']),
"adresse_numero": row['adresse_numero'],
"adresse_suffixe": row['adresse_suffixe'],
"adresse_nom_voie": row['adresse_nom_voie'],
"adresse_code_voie": row['adresse_code_voie'],
"code_postal": row['code_postal'],
"code_commune": row['code_commune'],
"nom_commune": row['nom_commune'],
"code_departement": row['code_departement'],
"ancien_code_commune": row['ancien_code_commune'],
"ancien_nom_commune": row['ancien_nom_commune'],
"id_parcelle": row['id_parcelle'],
"ancien_id_parcelle": row['ancien_id_parcelle'],
"numero_volume": row['numero_volume'],
"lot1_numero": row['lot1_numero'],
"lot1_surface_carrez": row['lot1_surface_carrez'],
"lot2_numero": row['lot2_numero'],
"lot2_surface_carrez": row['lot2_surface_carrez'],
"lot3_numero": row['lot3_numero'],
"lot3_surface_carrez": row['lot3_surface_carrez'],
"lot4_numero": row['lot4_numero'],
"lot4_surface_carrez": row['lot4_surface_carrez'],
"lot5_numero": row['lot5_numero'],
"lot5_surface_carrez": row['lot5_surface_carrez'],
"nombre_lots": row['nombre_lots'],
"code_type_local": row['code_type_local'],
"type_local": row['type_local'],
"surface_reelle_bati": row['surface_reelle_bati'],
"nombre_pieces_principales": row['nombre_pieces_principales'],
"code_nature_culture": row['code_nature_culture'],
"nature_culture": row['nature_culture'],
"code_nature_culture_speciale": row['code_nature_culture_speciale'],
"nature_culture_speciale": row['nature_culture_speciale'],
"surface_terrain": row['surface_terrain'],
"longitude": float(row['longitude']),
"latitude": float(row['latitude']),
"adresse_string": row['adresse_string']
}
},
],
}
print(post_url)
print(data)
post_airtable_request = requests.post(post_url, headers = post_headers, json = data)
print(post_airtable_request.status_code)
print('Extraction completed in API --> 1000 lines')