I'm making a Reddit bot that goes through comments on certain subreddits and replies to those with certain keyphrases.
I originally did not have a loop, and it worked fine, but I had to click run again every few minutes. I am running my python script on pythonanywhere.com, using PRAW.
import praw
import time
SECONDS_PER_MIN = 60
subreddit = reddit.subreddit('memes+dankmemes+comics+funny+pics')
keyphrase = ('Sauce+Sauce?')
def main():
while True:
for comment in subreddit.stream.comments():
if keyphrase in comment.body:
comment.reply('[Here.](https://www.youtube.com/watch?v=dQw4w9WgXcQ)\n\nI am a bot and this action was performed automatically. Learn more at [https://saucebot.com/](https://www.youtube.com/watch?v=dQw4w9WgXcQ)')
print('Posted!')
time.sleep(SECONDS_PER_MIN * 11)
if __name__ == '__main__':
main()
I expect it to respond to a random person who says "sauce" every 10 minutes, but now it won't respond to anyone.
Are you running your script on a PC? You could potentially use the task scheduler for that without using python at all. Just save your script as a binary using pyinstaller, then schedule it to run every ten minutes.
Related
So basically, i have made a tkinter app that has a reminder utiliy in specific to generate notifications at the scheduled time. Everything works fine until I run the app module and another module having the notification generating function one at a time , but when I call the notification generating function intto the app module, my app doesnt work but the notification works. I want the app to run such that the notification generating function kind of runs in the background until the app module is open.
github link: https://github.com/click-boom/Trella
Looking into chatgpt i found terms like threading and multiprocessing, but i have no concept of that and still tried but didnt work.
Sure enough what you are looking for is multithreading.
Here is a simple example of how multithreading works (sorry for my lack of drawing skills).
This is how all monothread programs work. In most programming languages this is the default behaviour.
So in this example Second Task will have to wait for First Task to complete.
If you want several tasks to run concurrently, you can use multithreading.
This is how you could implement this in Python.
Monothreading:
from time import sleep
def firstTask():
time = 10
for i in range(time):
sleep(1)
print(f'I have been running for {i}s')
def secondTask():
print('All I want to do is run once')
firstTask()
secondTask()
Here, secondTask will only run after firstTask is done (i.e after 10 seconds).
Multithreading:
from threading import Thread
from time import sleep
def firstTask():
time = 10
for i in range(time):
sleep(1)
print(f'I have been running for {i}s')
def secondTask():
print('All I want to do is run once')
first_thread = Thread(target=firstTask)
second_thread = Thread(target=secondTask)
first_thread.start()
second_thread.start()
I hope this will be a help to someone !
I have created a Python script for scraping the seller ranking for a particular item on amazon. I would like to see how this ranking changes every hour, therefore I need to run this script every hour. I have done:
import schedule
import time
def run_program():
exec(open("/users/myuser/documents/scraper.py").read())
schedule.every(10).seconds.do(run_program)
while 1:
schedule.run_pending()
time.sleep(1)
And it works great, but it only does it one time and then no more output from there. What am I missing?
I am using the python schedule module. I have a function that I am trying to run at a specfic time. When I try to run it on my local machine it runs at the specified time, however on an ubuntu server it doesn't. When I try to run the task every 5 seconds it works on an ubuntu server so I'm confused on the problem.
Just to note. When I tried testing this an an ubuntu server I changed the time to a couple of minutes ahead of the current time and then started the bot, I'm not sure if this could be the problem.
import schedule
import time
def job():
print('hello')
if __name__ == "__main__":
schedule.every(10).seconds.do(job) // this works
schedule.every().day.at("08:00").do(job)
while 1:
schedule.run_pending()
time.sleep(1)
As Stradivari said, the problem was that the server time wasn't the same as my local machine time.
I fixed it by following this guide: https://www.digitalocean.com/community/tutorials/how-to-set-up-time-synchronization-on-ubuntu-18-04
I'm totally new to Python.
I just wrote this simple script that prints a message on my desktop:
from plyer import notification
notification.notify(
title="HI MATTEO!",
message="Here's the notification to be sent every hour!",
timeout=10
)
Here's the screenshot of the notification
I'd like to know if there's a way to run this notification every hour (or at a given time)
Thanks so much
Matteo Fossati
You should look at the schedule package (https://pypi.org/project/schedule/).
Then, run something like:
schedule.every().hour.do(my_notification)
...
while True:
schedule.run_pending()
time.sleep(1)
I want to write a simple reminder-bot for telegram. Bot gets from user time (Hours:Minutes) and saves it. When system time equals to users remind time, bot sends message to user.
This is how i track current time:
import time
def timer():
now = time.strftime('%X').split(':')[0:2]
return now
The question is:
How can I make my code wait till time to send message comes without using time.sleep() and checking current time each minute (uses too much memory of raspberrypi)?
If you use python-telegram-bot library, you can use the JobQueue to set up timed jobs.
See here for an example.