-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfintel_get_text.py
More file actions
95 lines (76 loc) · 2.9 KB
/
fintel_get_text.py
File metadata and controls
95 lines (76 loc) · 2.9 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
#####################################
# Import Modules at the Top
#####################################
# Import from Python Standard Library
import pathlib
# Import from external packages
import requests
# Import from local project modules
from utils_logger import logger
#####################################
# Declare Global Variables
#####################################
fetched_folder_name = "iolp_buildings.txt"
#####################################
# Define Functions
#####################################
def fetch_txt_file(folder_name: str, filename: str, url: str) -> None:
"""
Fetch text data from the given URL and write it to a file.
Args:
folder_name (str): Name of the folder to save the file.
filename (str): Name of the output file.
url (str): URL of the text file to fetch.
Returns:
None
Example:
fetch_txt_file("data", "romeo.txt", "https://example.com/romeo.txt")
"""
if not url:
logger.error("The URL provided is empty. Please provide a valid URL.")
return
try:
logger.info(f"Fetching text data from {url}...")
response = requests.get(url)
response.raise_for_status()
write_txt_file(folder_name, filename, response.text)
logger.info(f"SUCCESS: Text file fetched and saved as {filename}")
except requests.exceptions.HTTPError as http_err:
logger.error(f"HTTP error occurred: {http_err}")
except requests.exceptions.RequestException as req_err:
logger.error(f"Request error occurred: {req_err}")
def write_txt_file(folder_name: str, filename: str, string_data: str) -> None:
"""
Write text data to a file.
Args:
folder_name (str): Name of the folder to save the file.
filename (str): Name of the output file.
string_data (str): Text content to write to the file.
Returns:
None
"""
file_path = pathlib.Path(folder_name).joinpath(filename)
try:
logger.info(f"Writing data to {file_path}...")
file_path.parent.mkdir(parents=True, exist_ok=True)
with file_path.open('w') as file:
file.write(string_data)
logger.info(f"SUCCESS: Data written to {file_path}")
except IOError as io_err:
logger.error(f"Error writing to file {file_path}: {io_err}")
#####################################
# Define main() function
#####################################
def main():
"""
Main function to demonstrate fetching text data.
"""
txt_url = 'https://github.com/dfintel25/Sample-Data/raw/refs/heads/main/2025-1-24-iolp-buildings.txt'
logger.info("Starting text fetch demonstration...")
fetch_txt_file(fetched_folder_name, "iolp_buildings.txt", txt_url)
#####################################
# Conditional Execution
#####################################
if __name__ == '__main__':
main()
# TODO: Run this as a script to test that all functions work as intended.