ChatGPT can be super helpful in helping you to create or modify code. For a fun exercise I recently asked it to write a "dead man's switch" style script in python. Specifically to check if there have been any new files written to a specified directory within the last 7 days. If there was nothing newer than 7 days then send an email using the sendgrid API.
Here's the full conversation - it's pretty interesting!
###########################
Me: Write me a script that can be used as a dead man's switch that will make me do something until I don't. Have it check for any file in /path/to/file/ directory. If there is not a file that is less than 7 days old then it will use sendgrid api to send an email to [email protected].
ChatGPT: Here's a script in Python that will check for files in a specific directory and send an email through the SendGrid API if no new file has been added within the past 7 days:
Me: Great - please add code comments to that finished script
ChatGPT: Sure! Here's the same script with added comments:
Me: Will this work with python3?
ChatGPT: Yes, this script should work with Python 3. The sendgrid library supports both Python 2 and 3, and the core Python libraries used in the script (os, datetime) are compatible with both versions as well.
Me: If i had this script in ~/synner/scripts/ how would you call this in cron to run every night at midnight?
ChatGPT: To run the script every night at midnight using cron, you can add the following line to your crontab file:
This line specifies that the script should be executed every day at midnight (the first 0 represents the minute, and the second 0 represents the hour). The full path to the python3 executable is used to ensure that the correct version of Python is used, and the full path to the script dead_man_switch.py is specified so that cron knows exactly which script to run.
To edit your crontab file, run the following command in your terminal:
This will open the crontab file for editing, and you can add the line above to the file and save it.
Note: The exact location of the python3 executable and the path to the script may be different on your system. Please make sure to use the correct paths for your setup.
#################################
This thing is pretty crazy. If you haven't tried it yet, go check it out here: https://chat.openai.com/chat
If you have played with it, what are you using it for?
Here's the full conversation - it's pretty interesting!
###########################
Me: Write me a script that can be used as a dead man's switch that will make me do something until I don't. Have it check for any file in /path/to/file/ directory. If there is not a file that is less than 7 days old then it will use sendgrid api to send an email to [email protected].
ChatGPT: Here's a script in Python that will check for files in a specific directory and send an email through the SendGrid API if no new file has been added within the past 7 days:
Code:
import os
import datetime
import sendgrid
from sendgrid.helpers.mail import Email, Content, Mail
path = "/path/to/file/"
now = datetime.datetime.now()
no_new_file = True
for filename in os.listdir(path):
file_path = os.path.join(path, filename)
if os.path.isfile(file_path):
file_time = datetime.datetime.fromtimestamp(os.path.getmtime(file_path))
if (now - file_time).days <= 7:
no_new_file = False
break
if no_new_file:
sg = sendgrid.SendGridAPIClient(api_key=os.environ.get("SENDGRID_API_KEY"))
from_email = Email("[email protected]")
to_email = Email("[email protected]")
subject = "No new file in the past week"
content = Content("text/plain", "This is a reminder that no new file has been added to the specified directory in the past week.")
mail = Mail(from_email, subject, to_email, content)
sg.client.mail.send.post(request_body=mail.get())
Me: Great - please add code comments to that finished script
ChatGPT: Sure! Here's the same script with added comments:
Code:
import os
import datetime
import sendgrid
from sendgrid.helpers.mail import Email, Content, Mail
# Define the path to the directory to be checked
path = "/path/to/file/"
# Get the current time
now = datetime.datetime.now()
# Assume no new file is present until proven otherwise
no_new_file = True
# Loop through the files in the directory
for filename in os.listdir(path):
file_path = os.path.join(path, filename)
# Check if the current item is a file
if os.path.isfile(file_path):
# Get the modification time of the file
file_time = datetime.datetime.fromtimestamp(os.path.getmtime(file_path))
# Check if the file was modified within the past 7 days
if (now - file_time).days <= 7:
# If a file was modified within the past 7 days, set no_new_file to False
no_new_file = False
# Stop looping through the files
break
# If no new file was found in the directory
if no_new_file:
# Create a SendGrid client object
sg = sendgrid.SendGridAPIClient(api_key=os.environ.get("SENDGRID_API_KEY"))
# Define the from email address
from_email = Email("[email protected]")
# Define the to email address
to_email = Email("[email protected]")
# Define the subject of the email
subject = "No new file in the past week"
# Define the content of the email
content = Content("text/plain", "This is a reminder that no new file has been added to the specified directory in the past week.")
# Create a Mail object and send the email
mail = Mail(from_email, subject, to_email, content)
sg.client.mail.send.post(request_body=mail.get())
Me: Will this work with python3?
ChatGPT: Yes, this script should work with Python 3. The sendgrid library supports both Python 2 and 3, and the core Python libraries used in the script (os, datetime) are compatible with both versions as well.
Me: If i had this script in ~/synner/scripts/ how would you call this in cron to run every night at midnight?
ChatGPT: To run the script every night at midnight using cron, you can add the following line to your crontab file:
0 0 * * * /usr/bin/python3 ~/synner/scripts/dead_man_switch.py
This line specifies that the script should be executed every day at midnight (the first 0 represents the minute, and the second 0 represents the hour). The full path to the python3 executable is used to ensure that the correct version of Python is used, and the full path to the script dead_man_switch.py is specified so that cron knows exactly which script to run.
To edit your crontab file, run the following command in your terminal:
crontab -e
This will open the crontab file for editing, and you can add the line above to the file and save it.
Note: The exact location of the python3 executable and the path to the script may be different on your system. Please make sure to use the correct paths for your setup.
#################################
This thing is pretty crazy. If you haven't tried it yet, go check it out here: https://chat.openai.com/chat
If you have played with it, what are you using it for?
Last edited: