Hello I am very new to programming and i'm trying to make an auto-posting bot for my subreddit.I'm using praw and I need to run this script at certain times and have it input and work
import praw
r = praw.Reddit(user_agent="UA")
r.login("username", "password")
sub = r.get_subreddit("Sub")
sub.submit("Title", text="Post text")
I'm running windows and someone said to use the task scheduler but I haven't been able to figure it out. Any help would be great. Thank You.
I would suggest to look into sched, a general purpose event scheduler. It is described, with appropriate examples to start you, in the Python's documentation.
Sample:
import time
import sched
scheduler = sched.scheduler(time.time, time.sleep)
def reddit():
<your code>
def scheduler_reddit():
scheduler.enter(0, 1, reddit, ())
scheduler.run()
time.sleep(3600)
for i in range(100):
scheduler_reddit()
Change 3600 with the desired time in seconds.
Related
I am a complete novice, and have had a script written for me that scrapes football stats data.
I would like to automate this process nightly at 8 pm, however, have not been able to crack it. I assume it is not overly complex - however it has been for my inexperience.
My "googling" has led me to "pip install schedule" ... and I have followed directions to create ...
# au_john_finalsiren_V9.py
import schedule
import time
def au_john_finalsiren_V9.py()
schedule.every().day.at(20:00:00").do(au_john_finalsiren_V9.py)
while True:
schedule.run_pending()
time.sleep(1)
But I am missing something!!
All help is appreciated!!
JR
The issue is in your code and function calling. Try to modify it in the following way:
# au_john_finalsiren_V9.py
import schedule
import time
def au_john_finalsiren_V9():
print('This is a function that is called every day at 20:00')
schedule.every().day.at("20:00").do(au_john_finalsiren_V9)
while True:
schedule.run_pending()
time.sleep(1)
In the function au_john_finalsiren_V9 write the code that should be executed.
so I have a python script that make a POST call with some arguments (ID and Date), that means that at a specific date time makes a POST passing and ID.
Now what I want to achieve is having a script that puts jobs in the queue and another one that execute those script at that specific date time.
The script stays the same every time but it's date and ID that changes.
I can of course run multiple times the same script with different arguments and using time.sleep wait until it gets executed but I am trying to find a cleaner way...
You can try to use sched module.
Here is an example that I made for myself in the past to deal with the discord:
import sched
import time
import sh_executor
import sh_logger as log
import sv_discord
s = None
def init():
log.info("Initializing Scheduler...")
global s
s = sched.scheduler(time.time, time.sleep)
sh_executor.submit_task(_start)
def stop():
s.cancel(_restart_discord)
def _start():
if sv_discord.DiscordSettings.IS_BOT_ENABLED:
s.enter(30, 1, _restart_discord)
s.run()
def _restart_discord():
log.info("Scheduler: restarting Discord...")
sv_discord.reconnect()
s.enter(3600, 1, _restart_discord)
Also: Take a look here (python cron-like jobs)
I want to execute a file at a specific time as I won't be around at that time to execute it.
I was thinking if there is a way in which I can write some code in another file, that will start that code, and leave it running so that it can start that code at that specific time - Maybe using os and time, or command line.
How about something like:
import time, subprocess, sys
time.sleep(3600) # wait 1 hour
subprocess.call([sys.executable, 'another-script.py'])
You can use Python's builtin sched module:
import sched, time
def action():
print("hello world")
# Set up scheduler
s = sched.scheduler(time.localtime, time.sleep)
# Schedule when you want the action to occur
s.enterabs(time.strptime('Tue March 15 15:55:17 2020'), 0, action)
# Block until the action has been run
s.run()
So this is something I have been wondering for a while, and while I don't know if there is a correct answer there is probably a better option.
So which of the options below is best to schedule a python script to run at a specific time? Let me know what you prefer, or if you have an alternative.
1) Take a python file script.py, write a ".bat" file to run the code in command prompt, and then use windows native task scheduler to launch the file at a specific time each day.
BAT Example:
cd C:\Users\Administrator\Documents\Scripts
python script.py
This is some code for a BAT file that will run your python script.
2) Use python to create a timed task within your file like some of the examples below:
import schedule
import time
def job():
print("I'm working...")
schedule.every(10).minutes.do(job)
schedule.every().hour.do(job)
schedule.every().day.at("10:30").do(job)
while 1:
schedule.run_pending()
time.sleep(1)
or
from datetime import datetime
from threading import Timer
x=datetime.today()
y=x.replace(day=x.day+1, hour=1, minute=0, second=0, microsecond=0)
delta_t=y-x
secs=delta_t.seconds+1
def hello_world():
print "hello world"
#...
t = Timer(secs, hello_world)
t.start()
or
from datetime import date
from apscheduler.scheduler import Scheduler
# Start the scheduler
sched = Scheduler()
sched.start()
# Define the function that is to be executed
def my_job(text):
print text
# The job will be executed on November 6th, 2009
exec_date = date(2009, 11, 6)
# Store the job in a variable in case we want to cancel it
job = sched.add_date_job(my_job, exec_date, ['text'])
# The job will be executed on November 6th, 2009 at 16:30:05
job = sched.add_date_job(my_job, datetime(2009, 11, 6, 16, 30, 5), ['text'])
When it comes to option 2 there are plenty of examples I could include but I just want to know whats better in your opinion.
Does one of the options use more processing power? Is one of the options more reliable? etc.
I will choose option 1. If you choose option 2, your code will not run in several circumstances, such as your machine restart or your python IDE crashes.
Option 1 will run your code as long as your machine is running.
I have never used Option 2 personally.
In general, if you just have one or two servers. Task Scheduler (Windows) or cron (Linux) would be the way to go.
There are also tools like AutoSys that are built for scheduling batch jobs.
I am learning Python and was tinkering with Advanced scheduler. I am not able to gt it working though.
import time
from datetime import datetime
from apscheduler.scheduler import Scheduler
sched = Scheduler(standalone=True)
sched.start()
##sched.cron_schedule(second=5)
def s():
print "hi"
sched.add_interval_job(s, seconds=10)
i=0
while True:
print i
i=i+1
time.sleep(3)
sched.shutdown()
I am sure I am missing something basic. Could someone please point it out?
Also would you recommend a crontab to the advanced scheduler? I want my script to run every 24 hours.
Thanks
Standalone mode means that sched.start() will block, so the code below it will NOT be executed. So first create the scheduler, then add the interval job and finally start the scheduler.
As for the crontab, how about just sched.add_cron_job(s, hour=0)? That will execute it at midnight every day.