6
6
import csv
7
7
import gzip
8
8
import logging
9
- import os
10
9
from datetime import datetime , timedelta
11
10
from io import StringIO
12
11
from pathlib import Path
@@ -34,8 +33,8 @@ def __init__(self, error_mode=ErrorMode.TruncTrace):
34
33
self .error_mode = error_mode
35
34
self .cachedir = self .CACHEDIR
36
35
self .backup_cachedir = self .BACKUPCACHEDIR
37
- self .epss_path = str ( Path ( self .cachedir ) / "epss" )
38
- self .file_name = os . path . join ( self .epss_path , "epss_scores-current.csv" )
36
+ self .epss_path = self .cachedir / "epss"
37
+ self .file_name = self .epss_path / "epss_scores-current.csv"
39
38
self .source_name = self .SOURCE
40
39
41
40
async def update_epss (self ):
@@ -58,11 +57,11 @@ async def download_epss_data(self):
58
57
"""Downloads the EPSS CSV file and saves it to the local filesystem.
59
58
The download is only performed if the file is older than 24 hours.
60
59
"""
61
- os . makedirs ( self .epss_path , exist_ok = True )
60
+ self .epss_path . mkdir ( parents = True , exist_ok = True )
62
61
# Check if the file exists
63
- if os . path .exists (self . file_name ):
62
+ if self . file_name .exists ():
64
63
# Get the modification time of the file
65
- modified_time = os . path . getmtime ( self .file_name )
64
+ modified_time = self .file_name . stat (). st_mtime
66
65
last_modified = datetime .fromtimestamp (modified_time )
67
66
68
67
# Calculate the time difference between now and the last modified time
@@ -80,8 +79,7 @@ async def download_epss_data(self):
80
79
decompressed_data = gzip .decompress (await response .read ())
81
80
82
81
# Save the downloaded data to the file
83
- with open (self .file_name , "wb" ) as file :
84
- file .write (decompressed_data )
82
+ self .file_name .write_bytes (decompressed_data )
85
83
86
84
except aiohttp .ClientError as e :
87
85
self .LOGGER .error (f"An error occurred during updating epss { e } " )
@@ -102,8 +100,7 @@ async def download_epss_data(self):
102
100
decompressed_data = gzip .decompress (await response .read ())
103
101
104
102
# Save the downloaded data to the file
105
- with open (self .file_name , "wb" ) as file :
106
- file .write (decompressed_data )
103
+ self .file_name .write_bytes (decompressed_data )
107
104
108
105
except aiohttp .ClientError as e :
109
106
self .LOGGER .error (f"An error occurred during downloading epss { e } " )
@@ -114,9 +111,8 @@ def parse_epss_data(self, file_path=None):
114
111
if file_path is None :
115
112
file_path = self .file_name
116
113
117
- with open (file_path ) as file :
118
- # Read the content of the CSV file
119
- decoded_data = file .read ()
114
+ # Read the content of the CSV file
115
+ decoded_data = Path (file_path ).read_text ()
120
116
121
117
# Create a CSV reader to read the data from the decoded CSV content
122
118
reader = csv .reader (StringIO (decoded_data ), delimiter = "," )
0 commit comments