Reputation: 31
I have a script that will scrape Facebook post in a while loop. But to prevent getting banned by Facebook I prefer the scraping function run(self)
only run at a certain time and stop after 1 hour. How can I achieve that? I saw someone post about starting a function at a certain time but not stopping it after a specific hour.
Code to start a function at a certain time:
import schedule
import time
def job(t):
print ("I'm working...", t)
return
schedule.every().day.at("18:44").do(job,'It is 01:00')
while True:
schedule.run_pending()
time.sleep(60) # wait one minute
My script code
def run(self):
while True:
try:
wanted = "Pecahan setiap negeri (Kumulatif):" # wanted post
for post in get_posts("myhealthkkm", pages=5):
if post.get("post_text") is not None and wanted in post.get("post_text"):
# print("Found", t)
listposts.append(post.get("post_text"))
# append until 3 page finish then go here
time.sleep(1)
print(listposts)
global listView
if listposts != 0:
listView = listposts.copy()
print(listView)
listposts.clear()
except exceptions.TemporarilyBanned:
print("Temporarily banned, sleeping for 10m")
time.sleep(600)
I want the run()
function only execute at 3pm and stop after 1 hour which is 4pm everyday. How can I achieve this goal ? Thanks
Upvotes: 1
Views: 682
Reputation: 31
Thanks to the respond from @larsks, I tried to put a time check inside the function and exit the loop if it reached 3600second
which is equal to 1 hour.
Here is the sample code:
import schedule
import time
def job():
program_starts = time.time()
while True:
now = time.time()
timeframe = now - program_starts
print("It has been {0} seconds since the loop started".format(timeframe))
if timeframe > 3600:
break
schedule.every().day.at("19:20:30").do(job)
while True:
schedule.run_pending()
#time.sleep(60) # wait one minute
Upvotes: 1