Example #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
Example #2
0
                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.
                try:
                    a = open(args.cookie, 'w')
                    a.close()
                except IOError as e:
                    sys.exit(': '.join((e.strerror, e.filename)))
            else:
                browser.__dict__['_ua_handlers']['_cookies'].cookiejar = cookie

        try:
            Pytane.loadTickets(browser)
        except Pytane.WrongPageError as e:
            if e.page == URL + '/sign_in':
                if args.cookie:
                    good_cookie = False
                    print('Your cookie was invalid or new. Opening a new session.')
                    # Creating a new browser object in case invalid cookies aren't overwritten
                    # once we use password login page.
                    browser = Pytane.browserInit()
                try:
                    Pytane.login(browser, args.user)
                except Pytane.SessionFailureError:
                    sys.exit('Could not login to Noctane.')

    except (KeyboardInterrupt, SystemExit, EOFError):
        sys.exit('\n\nExiting on user command\n')