-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequests_Addedtimestamp.py
More file actions
74 lines (58 loc) · 2.42 KB
/
requests_Addedtimestamp.py
File metadata and controls
74 lines (58 loc) · 2.42 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
import requests
import base64
import os
import glob
from datetime import datetime
# API endpoint URL with query string parameter
url = 'https://api.<xxx>'
# Directory containing the CSV files
csv_directory = '<xxxCsv>'
# Function to fetch CSV files from the given directory
def fetch_csv_files(directory):
csv_files = glob.glob(os.path.join(directory, "*.csv"))
return csv_files
# Fetch the CSV files
csv_files = fetch_csv_files(csv_directory)
# Check if CSV files are found
if csv_files:
print(f"Found CSV files: {csv_files}")
# Process each CSV file
for csv_path in csv_files:
print(f"Processing file: {csv_path}")
# Verify the file exists in the expected location
if os.path.isfile(csv_path):
print(f"File found: {csv_path}")
# Generate new filename
current_time = datetime.now().strftime("%Y%m%d%H%M%S")
new_filename = f"ContractOpportunities_{current_time}.csv"
new_csv_path = os.path.join(os.path.dirname(csv_path), new_filename)
# Rename the file
os.rename(csv_path, new_csv_path)
print(f"Renamed file to: {new_csv_path}")
# Encode your CSV file to base64
try:
with open(new_csv_path, 'rb') as file:
encoded_csv = base64.b64encode(file.read()).decode('utf-8')
# Define the data to be sent
data = {
'name': new_filename, # Use the new filename
'type': 'text/csv',
'base64Data': encoded_csv, # Add your base64 encoded data here
'overwrite': True # Indicate that the file should be overwritten
}
# Make the API call
response = requests.post(url, json=data)
# Check the response status
if response.status_code == 200:
print("API call successful!")
else:
print("API call failed with status code:", response.status_code)
print("Error message:", response.text)
except FileNotFoundError:
print(f"File not found: {new_csv_path}")
except Exception as e:
print(f"An error occurred: {e}")
else:
print(f"File not found: {csv_path}")
else:
print(f"No CSV files found in the directory: {csv_directory}")