-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.py
63 lines (53 loc) · 2.05 KB
/
script.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
import requests
import csv
from datetime import datetime, timedelta
import os
import sys
import shutil
def download_and_rename_file(url, original_filename, new_filename_prefix, destination_path):
# Send HTTP request to the specified URL and save the response from server in a response object called r
r = requests.get(url)
# Open a csv file in write mode
with open(original_filename, 'w') as f:
f.write(r.text)
# Open the csv file in read mode
with open(original_filename, 'r') as f:
reader = csv.reader(f)
# Get the first row
first_row = next(reader)
# Extract the date string
date_string = first_row[0].split(' ')[2]
date_object = datetime.strptime(date_string, '%Y-%m-%d')
# Get yesterday's date
yesterday = datetime.now() - timedelta(days=1)
if date_object.date() != yesterday.date():
# Remove the file
os.remove(original_filename)
# Exit the script
sys.exit("The date in the file is not yesterday's date.")
else:
# Define the new filename
new_filename = new_filename_prefix + yesterday.strftime('%m%d')
# Add the extension
new_filename += ".csv"
# Check if the new filename already exists
if os.path.exists(os.path.join(destination_path, new_filename)):
# If it exists, remove the original file and exit the script
os.remove(original_filename)
sys.exit(f"The file {new_filename} already exists.")
# Move and rename the file
shutil.move(original_filename, os.path.join(destination_path, new_filename))
# Download and rename the first file
download_and_rename_file(
'https://www.mimit.gov.it/images/exportCSV/prezzo_alle_8.csv',
'prezzo_alle_8.csv',
'price_',
'data/nuovi/prices/'
)
# Download and rename the second file
download_and_rename_file(
'https://www.mimit.gov.it/images/exportCSV/anagrafica_impianti_attivi.csv',
'anagrafica_impianti_attivi.csv',
'name_',
'data/nuovi/gas_station_info/'
)