-1

I have a script for a Twitter bot which will send out a Tweet every Friday, however I'm struggling to find a way to make keep it from killing the code if it is not Friday.

I have tried using a loop which does usually work for my Discord bot, but my 11pm brain didn't realise it was discord.py syntax until before I executed that. As a result, I can't think of anything else to try. I have looked over Google and the links to this site that Google gives but I can't find much that helps me.

Here is the code I am using:

for i in range(10000):
  if date.today().weekday() == 6:
    i += 1
    print("Congratulating those who made it to Friday :D")
    api.update_status(f"Week {i} of Congratulations Sailor, you made it to Friday!\nhttps://www.youtube.com/videoid")
    print("Congratulated\n")
  else:
    print("not friday :(")
    asyncio.sleep(10)

But that just constantly prints not friday :( into the console for 10 seconds before killing the code and not trying again.

So any help would be appreciated. Also, I am sorry if I am missing something so simple, my 11pm brain isn't the best.

Thanks

  • You're probably better off having the code perform the action without any checks and using a scheduler such as `cron` trigger it once a week. – imbuedHope Jul 11 '22 at 22:29
  • This likely a duplicate of https://stackoverflow.com/questions/11523918/start-a-function-at-given-time – imbuedHope Jul 11 '22 at 22:32
  • Python's Datetime module has the isoweekday() method which returns the day of the week as an integer where '5' is Friday. – Cheese Jul 11 '22 at 22:32

3 Answers3

0

You can use the schedule module to schedule a job to run once a week. Schedule is a light-weight in-process scheduler for periodic jobs in Python.

import time
import schedule

def job():
    print("I'm working...")
    # add your Twitter code here

# Run job on a specific day of the week
schedule.every().friday.do(job)

# Run job on a specific day of the week and time
schedule.every().friday.at("13:15").do(job)

while True:
    schedule.run_pending()
    time.sleep(1800)

More examples are found here.

Alternatively, rather than running a Python process 24x7, you could use the native scheduler to run your application. On Windows search for "Task Scheduler" and for both macOS and Linux a cron job can be specified. Python executes with your script on your specified schedule then terminates when the application is done.

CodeMonkey
  • 22,825
  • 4
  • 35
  • 75
  • Hey, this *schedule* module just returns an error - it seems the module is broken. It's returning `for file_info in version_info: /home/runner/replit-project-name/venv/lib/python3.8/site-packages/setuptools/command/install.py:34: SetuptoolsDeprecationWarning: setup.py install is deprecated. Use build and pip and other standards-based tools. warnings.warn( exit status 1`. How would I fix this? Because even commenting the `import schedule` line doesn't block this error and the Replit package installer treats it as not installed. – Slender the Blender Jul 12 '22 at 19:30
  • Did you run command `pip install schedule` ? Also, do you have a python script called "schedule.py" in the local folder? (that would conflict it with the schedule module). – CodeMonkey Jul 12 '22 at 21:54
  • I'm pretty sure I did but I'll give it another check. Also no, I do not have a *schedule.py* file. – Slender the Blender Jul 17 '22 at 11:19
  • Turns out I didn't. This works, thanks. I'll mark your answer as Accepted as it hasn't thrown any errors but I'll let you know if it works as intended on Friday. – Slender the Blender Jul 17 '22 at 11:31
0

What about Rocketry?

from rocketry import Rocketry
from rocketry.conds import weekly, time_of_day

app = Rocketry()

@app.task(weekly.on("Friday") & time_of_day.between("08:00", "22:00"))
def do_on_friday():
    ...

if __name__ == "__main__":
    app.run()
miksus
  • 2,426
  • 1
  • 18
  • 34
-1

Not sure if it helps but you could try using the time module. It has a sleep function, and you could perhaps make it sleep for a day.

There are a few issues with using sleep for a long time since sleep(n) doesn't sleep exactly for n seconds, it could sleep for a slightly shorter time. Hard coding it using the time function might be useful if that happens.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 13 '22 at 11:51