Record#
- Today's exercise is to fetch the latest content from replit and save it to the replit db.
- Set interest keywords and send an email if the content contains the keywords.
- Send the email every 6 hours, and the email content is the link to the fetched content.
- I don't know why there is an error when running in a new replit project, indicating that the schedule module is missing, but it can run normally in another project.
CODE#
main.py#
import schedule, time, os, smtplib, requests
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from bs4 import BeautifulSoup
password = os.environ['p']
username = os.environ['u']
url = "https://replit.com/community-hub"
keywords = "API"
def searchkey():
respone = requests.get(url)
html = respone.text
soup = BeautifulSoup(html, 'html.parser')
title = soup.find_all("a", {"class", "css-epm014"})
txt2=""
for txt in title:
if keywords in txt.text:
txt1 = f"""
<a href="{txt['href']}">{txt.text}</a>
"""
txt2 += txt1
return txt2
def sendMail():
email = searchkey()
server = "smtp.gmail.com"
port = 587
s = smtplib.SMTP(host = server, port = port)
s.starttls()
s.login(username, password)
msg = MIMEMultipart()
msg['To'] = "[email protected]"
msg['From'] = username
msg['Subject'] = "Take a BREAK"
msg.attach(MIMEText(email, 'html'))
s.send_message(msg)
del msg
def printMe():
print("⏰ Sending Reminder")
sendMail() # Moved the subroutine into printMe which is already scheduled
schedule.every(6).hours.do(printMe) # Changed the interval to every 1 hour
while True:
schedule.run_pending()
#print(searchkey())
time.sleep(1)