15

I'd like to write a simple python script for doing a specific job. I'm getting some time and link information from a web site.

times=
[
('17.04.2011', '06:41:44', 'abc.php?xxx'),
('17.04.2011', '07:21:31', 'abc.php?yyy'),
('17.04.2011', '07:33:04', 'abc.php?zzz'),
('17.04.2011', '07:41:23', 'abc.php?www'),]

What is the best way to click these links at the right time? Do I need to calculate the time interval between the current and the one in list and sleep for a while?

I'm really stuck at this point and open to any ideas which could be useful.

Fish
  • 153
  • 1
  • 1
  • 4

4 Answers4

11

Take a look at Python's sched module.

Mark Dickinson
  • 27,124
  • 9
  • 79
  • 112
7

you can use schedule module and it is easy to use and will satisfy your requirement.

you can try something like this.

import datetime, schedule, request

TIME = [('17.04.2011', '06:41:44', 'abc.php?xxx'),
    ('17.04.2011', '07:21:31', 'abc.php?yyy'),
    ('17.04.2011', '07:33:04', 'abc.php?zzz'),
    ('17.04.2011', '07:41:23', 'abc.php?www')]

def job():
    global TIME
    date = datetime.datetime.now().strftime("%d.%m.%Y %H:%M:%S")
    for i in TIME:
        runTime = i[0] + " " + i[1]
        if i and date == str(runTime):
            request.get(str(i[2]))

schedule.every(0.01).minutes.do(job)

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

I have use request module and get method to call those URL. You can write whichever methods suits you.

vinit kantrod
  • 1,146
  • 13
  • 15
3

This may help: How do I get a Cron like scheduler in Python? It is about cron-like scheduling in Python. And yes, it is based on sleeps.

wjandrea
  • 23,210
  • 7
  • 49
  • 68
andrew_d
  • 83
  • 5
1

I finally made and used this.

def sleep_till_future(f_minute):
    """
        The function takes the current time, and calculates for how many seconds should sleep until a user provided minute in the future.
    """
    import time,datetime


    t = datetime.datetime.today()
    future = datetime.datetime(t.year,t.month,t.day,t.hour,f_minute)

    if future.minute <= t.minute:
        print("ERROR! Enter a valid minute in the future.")
    else:
        print "Current time: " + str(t.hour)+":"+str(t.minute)
        print "Sleep until : " + str(future.hour)+":"+str(future.minute)

        seconds_till_future = (future-t).seconds
        time.sleep( seconds_till_future )
        print "I slept for "+str(seconds_till_future)+" seconds!"
Kostas Demiris
  • 3,063
  • 4
  • 39
  • 74