Ejemplo n.º 1
0
def eventLoop(browser, interval, detail, nnotify, onotify):
    '''
    Check for tickets periodically.
    Support for three intervals complicates this function.
    interval: seconds between checks to Noctane.
    nnotify: seconds between sending notifications about new tickets.
    onotify: seconds between sending notifications about old tickets.
    '''
    last = 0
    nlast = 0
    olast = 0
    while True:
        try:
            print('loop')
            current = int(time.time())
            print('%i > %i + %i (%i)' % (current, last, interval, last + interval))
            if current > last + interval:
                print('load tickets')
                Pytane.loadTickets(browser)
                last = current
            else:
                print('Sleeping for %i' % (last + interval - current))
                if last + interval - current < 2:
                    # Corrects for rounding errors. Could avoid infinite, short sleeps.
                    time.sleep(2)
                else:
                    time.sleep(last + interval - current)
                continue
        except Pytane.WrongPageError as e:
            if e.page == URL + '/sign_in':
                notify("Fatal Error", 'Noctane session failed/expired. Rerun pytane to login again.')
                sys.exit(1)
        else:
            oldtickets, newtickets = Pytane.scrapTickets(browser, detail) 
            newtickets_shown = False
            if newtickets and current > nlast + nnotify:
                msg = []
                for i in newtickets:
                    msg.append('%s: %s\t\t%s' % (i.date_latest, i.description[:30], i.customer_name[:15]))
                notify("New Tickets", '\n=========\n'.join(msg))
                nlast = current
                newtickets_shown = True
            print('Old: %i > %i + %i (%i)' % (current, last, onotify, last + onotify))
            if oldtickets and current > olast + onotify:
                if newtickets_shown:
                    # Give new tickets notification a chance to clear.
                    time.sleep(10)
                msg = []
                for i in oldtickets:
                    msg.append('%s: %s\t\t%s' % (i.date_latest, i.description[:30], i.customer_name[:15]))
                notify("Old Tickets", '\n=========\n'.join(msg))
                olast = current

    return
Ejemplo n.º 2
0
                    # Give new tickets notification a chance to clear.
                    time.sleep(10)
                msg = []
                for i in oldtickets:
                    msg.append('%s: %s\t\t%s' % (i.date_latest, i.description[:30], i.customer_name[:15]))
                notify("Old Tickets", '\n=========\n'.join(msg))
                olast = current

    return
    
if __name__ == '__main__':
    try:
        if not pynotify.init("Pytane"):
            sys.exit("Could not initialize notifications")
        args = parser()
        browser = Pytane.browserInit()
        
        cookie = None
        good_cookie = True
        if args.cookie:
            try:
                cookie = pickle.load(open(args.cookie))
                if not isinstance(cookie, CookieJar):
                    raise TypeError
            except (pickle.UnpicklingError, TypeError):
                sys.exit('%s is not a valid cookie' % args.cookie)
            except EOFError:
                pass
            except IOError:
                # File doesn't currently exist. Try to open
                # and see if we get an actual error.