简体   繁体   中英

Why won't this APScheduler code work?

been looking for quite a while for an answer so I turned to here! It gives me the error, "Invalid Index" for the sched.start()!

import random
import datetime
from apscheduler.schedulers.blocking import BlockingScheduler
import smtplib
import email


def randEmail():
    #Gets randLine
    file_object = open("lyrics.txt", "r")
    randLine = random.randint(1, 10)
    for i, line in enumerate(file_object):
        if i == randLine:
            break
    #line = randomly generated line
    file_object.close()

    #Email
    emails = [ 'emails']


    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.starttls()
    server.login('login', 'password')
    server.sendmail('login',emails, line)
    server.quit()

    #Prints to notepad saying completed
    Date = datetime.datetime.now()


    with open("Server_Quit.txt", "r+") as ServerQuit:
        ServerQuit.write("Server has quit at " + str(Date))
    ServerQuit.close()


#Unsure whether working
#sched.Scheduler()
#sched.start()
#sched.add_interval_job(randEmail, hours=24, start_date='2016-10-10 18:30')

sched = BlockingScheduler()
@sched.randEmail('cron', day_of_week='mon-fri', hour=18, minutes=30)
sched.start()

I appreciate any help! I've tried my best to get this working on my own and have ironed through all the other problems myself, but can't get this working. Also, if I want this to run on my PC and do this everyday, can I just add it to startup processes, and when I start my PC the scheduler will start?

You can add a job to a scheduler by decorating a function with the scheduled_job decorator:

from apscheduler.schedulers.blocking import BlockingScheduler

sched = BlockingScheduler()

# minute=30 not minutes=30
@sched.scheduled_job('cron', day_of_week='mon-fri', hour=18, minute=30)
def randEmail():
    #Gets randLine
    with open("lyrics.txt", "r") as file_object:
        randLine = random.randint(1, 10)
        for i, line in enumerate(file_object):
            if i == randLine:
                break
    #line = randomly generated line

    #Email
    emails = [ 'emails']

    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.starttls()
    server.login('login', 'password')
    server.sendmail('login', emails, line)
    server.quit()

    #Prints to notepad saying completed
    Date = datetime.datetime.now()

    # You don't have to close the file if you use the with statement
    with open("Server_Quit.txt", "r+") as ServerQuit:
        ServerQuit.write("Server has quit at " + str(Date))

sched.start()

You can also use the add_job method:

sched.add_job(randEmail, 'cron', day_of_week='mon-fri', hour=18, minute=30)
sched.start()

I can't see any reason why it would not work as a startup process.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM