-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnas_stock.py
70 lines (57 loc) · 2.5 KB
/
nas_stock.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
64
65
66
67
68
69
70
from ftplib import FTP
from datetime import datetime
import os
import csv
import sys
def log_message_csv(log_file, message):
"""Helper function to log messages to a CSV file with SQL-compatible datetime."""
script_name = os.path.basename(sys.argv[0])
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
with open(log_file, "a", newline="") as csv_file:
writer = csv.writer(csv_file)
writer.writerow([timestamp, script_name, message])
def download_selected_files():
# FTP server details
ftp_host = "ftp.nasdaqtrader.com"
ftp_user = "anonymous"
ftp_pass = "guest"
remote_dir = "/symboldirectory"
local_dir = "./downloads" # Local directory to save the files
log_file = "./log.csv" # Log file in CSV format
# List of files to download
target_files = {"nasdaqlisted.txt", "nasdaqtraded.txt", "mfundslist.txt"}
log_message_csv(log_file, "Starting download process...")
log_message_csv(log_file, "Connecting to the FTP server...")
# Connect to the FTP server
ftp = FTP(ftp_host)
ftp.login(user=ftp_user, passwd=ftp_pass)
log_message_csv(log_file, "Connected successfully.")
# Navigate to the desired directory
ftp.cwd(remote_dir)
log_message_csv(log_file, f"Navigated to directory: {remote_dir}")
# List all files in the directory
files = ftp.nlst()
log_message_csv(log_file, f"Found {len(files)} files in {remote_dir}")
# Ensure the local directory exists
if not os.path.exists(local_dir):
os.makedirs(local_dir)
log_message_csv(log_file, f"Created local directory: {local_dir}")
# Get today's date in YYYYMMDD format
current_date = datetime.now().strftime("%Y%m%d")
# Download only the target files
for file in files:
if file in target_files:
# Prepend the current date to the file name
new_file_name = f"{current_date}_{file}"
local_file_path = os.path.join(local_dir, new_file_name)
# Download the file
log_message_csv(log_file, f"Downloading: {file}")
with open(local_file_path, "wb") as local_file:
ftp.retrbinary(f"RETR {file}", local_file.write)
log_message_csv(log_file, f"Downloaded and saved as: {new_file_name}")
# Close the FTP connection
ftp.quit()
log_message_csv(log_file, "Disconnected from the FTP server.")
log_message_csv(log_file, "Selected files have been downloaded and renamed.")
if __name__ == "__main__":
download_selected_files()