Python Script Scheduling - python

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.

Related

Python Multithreading Rest API

I download Data over a restAPI and wrote a module. The download takes lets say 10sec. During this time, the rest of the script in 'main' and in the module is not running until the download is finished.
How can I change it, e.g. by processing it in another core?
I tried this code but it does not do the trick (same lag). Then I tried to implement this approach and it just gives me errors, as I suspect it 'map' does not work with 'wget.download'?
My code from the module:
from multiprocessing.dummy import Pool as ThreadPool
import urllib.parse
#define the needed data
function='TIME_SERIES_INTRADAY_EXTENDED'
symbol='IBM'
interval='1min'
slice='year1month1'
adjusted='true'
apikey= key[0].rstrip()
#create URL
SCHEME = os.environ.get("API_SCHEME", "https")
NETLOC = os.environ.get("API_NETLOC", "www.alphavantage.co") #query?
PATH = os.environ.get("API_PATH","query")
query = urllib.parse.urlencode(dict(function=function, symbol=symbol, interval=interval, slice=slice, adjusted=adjusted, apikey=apikey))
url = urllib.parse.urlunsplit((SCHEME, NETLOC,PATH, query, ''))
#this is my original code to download the data (working but slow and stopping the rest of the script)
wget.download(url, 'C:\\Users\\x\\Desktop\\Tool\\RAWdata\\test.csv')
#this is my attempt to speed things up via multithreading from code
pool = ThreadPool(4)
if __name__ == '__main__':
futures = []
for x in range(1):
futures.append(pool.apply_async(wget.download, url,'C:\\Users\\x\\Desktop\\Tool\\RAWdata\\test.csv']))
# futures is now a list of 10 futures.
for future in futures:
print(future.get())
any suggestions or do you see the error i make?
ok, i figured it out, so i will leave it here in case someone else needs it.
I made a module called APIcall which has a function APIcall() which uses wget.download() to download my data.
in main, i create a function (called threaded_APIfunc) which calls the APIcall() function in my modul APIcall
import threading
import APIcall
def threaded_APIfunc():
APIcall.APIcall(function, symbol, interval, slice, adjusted, apikey)
print ("Data Download complete for ${}".format(symbol))
and then i run the threaded_APIfunc within a thread like so
threading.Thread(target=threaded_APIfunc).start()
print ('Start Downloading Data for ${}'.format(symbol))
what happends is, that the .csv file gets downloaded in the background, while the main loop doesent wait till the download ir completed, it does the code what comes after the threading right away

Scheduling multiple schedules

I would like to start my code at the start of every hour I have tried using the minutes function of schedule however, the start time of the next process is dependent upon the end of the previous process and with the code I am trying to process the delay accumulates pretty quickly hence, I am trying this method instead. All help is appreciated. Thanks.
The code:
import schedule
import time
schedule.every().day.at("00:00").do(consolidated)
schedule.every().day.at("01:00").do(consolidated)
schedule.every().day.at("02:00").do(consolidated)
schedule.every().day.at("03:00").do(consolidated)
schedule.every().day.at("04:00").do(consolidated)
schedule.every().day.at("05:00").do(consolidated)
schedule.every().day.at("06:00").do(consolidated)
schedule.every().day.at("07:00").do(consolidated)
schedule.every().day.at("08:34").do(consolidated)
schedule.every().day.at("09:00").do(consolidated)
schedule.every().day.at("10:00").do(consolidated)
schedule.every().day.at("11:00").do(consolidated)
schedule.every().day.at("12:00").do(consolidated)
schedule.every().day.at("13:00").do(consolidated)
schedule.every().day.at("14:00").do(consolidated)
schedule.every().day.at("15:00").do(consolidated)
schedule.every().day.at("16:00").do(consolidated)
schedule.every().day.at("17:00").do(consolidated)
schedule.every().day.at("18:00").do(consolidated)
schedule.every().day.at("19:00").do(consolidated)
schedule.every().day.at("20:00").do(consolidated)
schedule.every().day.at("21:00").do(consolidated)
schedule.every().day.at("22:00").do(consolidated)
schedule.every().day.at("23:00").do(consolidated)
while True:
schedule.run_pending()
time.sleep(1)
The schedule package has an option for doing a task every hour:
import schedule
import time
schedule.every().hour.do(consolidated)
while True:
schedule.run_pending()
time.sleep(1)

Schedule a job to run every 5 mins only on a particular day in python

I want to run a cron job for every 5 mins only on a particular day of the week. I am using python schedule library and I can do these jobs individually. But how do I club them?
code
import schedule
def check():
print('Checking')
schedule.every(10).monday.do(check)
while True:
# Checks whether a scheduled task is pending to run or not
schedule.run_pending()
This gives an error IntervalError: Use mondays instead of monday when I try with mondays, I get an error AttributeError: 'Job' object has no attribute 'mondays' can someone help me with this.
I don't think the schedule package supports combinations of different time units the way you want. You can achieve what you want doing something like:
import schedule
import time
import datetime
def job():
if datetime.datetime.today().weekday() == 0:
print("I'm working...")
schedule.every(5).minutes.do(job)
while True:
schedule.run_pending()
time.sleep(1)
Maybe not the most elegant solution, though.
Did you try with "time"?
import time
...
schedule.every().monday.do(job)
while True:
schedule.run_pending()
time.sleep(300)

APScheduler not executing the python

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.

How to schedule a Python script to run at a certain time?

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.

Categories

Resources