62 lines
2.0 KiB
Python
62 lines
2.0 KiB
Python
import psycopg2
|
|
import os
|
|
import shutil
|
|
from dotenv import load_dotenv
|
|
|
|
load_dotenv()
|
|
db_host = os.getenv('DB_HOST')
|
|
db_name = os.getenv('DB_NAME')
|
|
db_user = os.getenv('DB_USER')
|
|
db_password = os.getenv('DB_PASSWORD')
|
|
openai_api_key = os.getenv('OPENAI_API_KEY')
|
|
|
|
# Set up the database connection
|
|
conn = psycopg2.connect(
|
|
host=db_host,
|
|
database=db_name,
|
|
user=db_user,
|
|
password=db_password,
|
|
connect_timeout=120
|
|
)
|
|
|
|
# Create a cursor object to execute SQL queries
|
|
cursor = conn.cursor()
|
|
|
|
# Execute SQL query to retrieve table data
|
|
cursor.execute("SELECT DISTINCT dba, plevel, folder FROM rlarp.cust WHERE status = 'A' AND COALESCE(plevel,'') <> '' AND plevel !~ 'XXX'")
|
|
# cursor.execute("SELECT dba, plevel FROM rlarp.cust WHERE status = 'A' AND plevel IN ('C.EEX','C.XEX','U.AGB.DI','U.AGB.GM','U.BGC.DI')")
|
|
|
|
# Fetch all rows from the result set
|
|
rows = cursor.fetchall()
|
|
|
|
# Iterate through each row
|
|
for row in rows:
|
|
customer_name = row[0]
|
|
price_level = row[1]
|
|
folder = row[2]
|
|
|
|
print(customer_name)
|
|
print(price_level)
|
|
|
|
# Create a directory with the customer name
|
|
customer_directory = '/mnt/c/Users/ptrowbridge/Downloads/2024-04-09/'
|
|
#os.makedirs(customer_directory, exist_ok=True)
|
|
|
|
# Copy contents from the pre-existing folder based on price level
|
|
source_directory = '/mnt/c/Users/ptrowbridge/Downloads/2024-04-09/Levels/' + price_level
|
|
if os.path.exists(source_directory):
|
|
for file in os.listdir(source_directory):
|
|
# shutil.copy2(os.path.join(source_directory, file), customer_directory)
|
|
source_file = os.path.join(source_directory, file)
|
|
print("source file: " + source_file)
|
|
destination_file = os.path.join(customer_directory, customer_name + " - " + folder + " - " + file)
|
|
print("destination file : " + destination_file)
|
|
shutil.copy(source_file, destination_file)
|
|
else:
|
|
print("Source directory does not exist.")
|
|
|
|
# Close the cursor and connection
|
|
cursor.close()
|
|
conn.close()
|
|
|