2

I am making a script wich post every hour on twitter every case of coronavirus. I have it finish but idk how to make that it posts every hour. any idea? (if you could post the following script with the solution in would be perfect)

import sys
CONSUMER_KEY = 'XXXX'
CONSUMER_SECRET = 'XXXX'
ACCESS_TOKEN = 'XXXX'
ACCESS_TOKEN_SECRET = 'XXXX'
import tweepy

import requests
from lxml import html


def create_tweet():
    response = requests.get('https://www.worldometers.info/coronavirus/')
    doc = html.fromstring(response.content)
    total, deaths, recovered = doc.xpath('//div[@class="maincounter-number"]/span/text()')

    tweet = f'''Coronavirus Latest Updates
Total cases: {total}
Recovered: {recovered}
Deaths: {deaths}

Source: https://www.worldometers.info/coronavirus/

#coronavirus #covid19 #coronavirusnews #coronavirusupdates #COVID19
'''
    return tweet


if __name__ == '__main__':
    auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
    auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)

    # Create API object
    api = tweepy.API(auth)

    try:
        api.verify_credentials()
        print('Authentication Successful')
    except:
        print('Error while authenticating API')
        sys.exit(5)

    tweet = create_tweet()
    api.update_status(tweet)
    print('Tweet successful')
MemePlay
  • 29
  • 1
  • 5

1 Answers1

1

Best way is to Schedule Python Script using Windows Scheduler on Windows & Cron Jobs on Linux. Following are the steps for Scheduling your python script on windows.

  1. Prepare the Python Script
  2. Save the Python Script
  3. Create Batch File to Run the Python Script with extension .Bat

    Content should be as follows "Path where your Python exe is stored\python.exe" "Path where your Python script is stored\script name.py" pause

    This batch file will run the Python script when double-clicking on it

  4. In the final step below, you’ll see how to schedule that batch file to execute the Python Script using the Windows Scheduler.

    • First, open the Control Panel and then click on the Administrative
      Tools: Next, double-click on the Task Scheduler, and then choose the option to ‘Create Basic Task…’

    • Type a name for your task (you can also type a description if needed), and then press Next.

    • Next, I chose to start the task ‘Daily’ since we wish to run the Python script daily at 6am:
    • The action will then recur everyday at 6am, staring from start date. You can adjust those timing parameters to suit your needs.
    • Select, Start a program, and then press Next:
    • Next, use the Browse button to find the batch file that runs the Python script.
    • click on Finish,
    • We go to the ‘Triggers’ tab and select the ‘Edit’ option:
    • An ‘Edit Trigger’ screen will appear. To set the script to run hourly, we select the ‘Repeat task…’ option and enable it. We select the ‘1 hour’ option, indicating that we wish for the task to execute on an hourly basis, and select the duration as indefinite under the duration option.
    • We then press the ‘OK’ button and exit the popup. Our batch script is enabled to run hourly at the :00 mark!
    • you should be good to go.

Above shared information is enough to do your job, But for more information, Feel free to use the link as below: https://datatofish.com/python-script-windows-scheduler/ and https://techrando.com/2019/06/22/how-to-execute-a-task-hourly-in-task-scheduler/

halfer
  • 19,824
  • 17
  • 99
  • 186
sanjeev
  • 67
  • 1
  • 9