二手产品经理

二手产品经理

THIS IS RENO

Challenge - 99 Days - Learn Python Online in 100 Days

Record#

  1. Today's exercise is to fetch the latest content from replit and save it to the replit db.
  2. Set interest keywords and send an email if the content contains the keywords.
  3. Send the email every 6 hours, and the email content is the link to the fetched content.
  4. 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)

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.