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