How I scheduled same-day Global Entry Interview (Part 2)

How I scheduled same-day Global Entry Interview (Part 2)

Now we can do logical comparison and stufflikethat with next_available_date

if self.old_appointment_date is None:
    try:
        match = re.search(r'\d{4}-\d{2}-\d{2}', self.api.user_timeline(self.api.me().id, count=1)[0].text)
        if match is None:
            self.old_appointment_date = next_available_date
        self.old_appointment_date = datetime.datetime.strptime(match.group(), '%Y-%m-%d').date()
    except:
        self.old_appointment_date = self.today + datetime.timedelta(365)  # set an arbitrary date
logging.info("Comparing {} with {}".format(next_available_date, self.old_appointment_date))

but there are bugs in the code because self.old_appointment_date and self.api are not attributes of GlobalEntryBot. Let's add them:

import tweepy
import logging


class GlobalEntryBot(object):
    
    def __init__(self):
        self.old_appointment_date = None
        self.api = tweepy

I've also imported tweepy to interact with the Twitter API, and logging to log (not print) information. Now we have an old_appointment_date and api attribute upon instantiation of GlobalEntryBot

See Tweepy API Documentation

Here's how I think of the comparison needed:

if there is no old appointment date, or "previously scheduled interview date", look for the last posted date on my twitter feed, and if None on my twitter feed, set an arbitrary date as my old appointment date

To accomplish the above comparison, I'm using regex with match = re.search() to find the last posted date on twitter, and if no match exists, the old_appointment_date becomes the next_available_date, establishing an "old" date for our next comparison. If something fails in our attempt to call twitter, our fallback is to set old_appointment_date as an arbitrary date for comparison. I've decided to be dramatic and set it for today + 365 days, mainly because the next_available_date should be sooner than one year out (for our next comparison).

Let's go ahead and create a today attribute on the bot. It's not really needed, but convenient to have in memory since we're doing date comparisons

class GlobalEntryBot(object):
    
    def __init__(self):
        self.old_appointment_date = None
        self.api = tweepy
        self.today = datetime.date.today()

By now your main.py file should look like this

import datetime
import json
import logging
import re
import requests
import tweepy


class Constants:
    ATL_AIRPORT_ID = 5182
    LOCATION_ID = ATL_AIRPORT_ID
    SCHEDULER_API_ENDPOINT = 'https://ttp.cbp.dhs.gov/schedulerapi/slots?orderBy=soonest&limit=1&locationId={}&minimum=1'.format(
        LOCATION_ID)
        
class GlobalEntryBot(object):
    
    def __init__(self):
        self.old_appointment_date = None
        self.api = tweepy
        self.today = datetime.date.today()
        
     def check_global_entry_schedule(self):
        """
        check date for global entry interview
        """
        res = requests.get(Constants.SCHEDULER_API_ENDPOINT)
        next_available_date_time = json.loads(res.text)[0]['startTimestamp'].split("T")
        next_available_date = datetime.datetime.strptime(next_available_date_time[0], '%Y-%m-%d').date()
        if self.old_appointment_date is None:
            try:
                match = re.search(r'\d{4}-\d{2}-\d{2}', self.api.user_timeline(self.api.me().id, count=1)[0].text)
                if match is None:
                    self.old_appointment_date = next_available_date
                self.old_appointment_date = datetime.datetime.strptime(match.group(), '%Y-%m-%d').date()
            except:
                self.old_appointment_date = self.today + datetime.timedelta(365)  # set an arbitrary date
        logging.info("Comparing {} with {}".format(next_available_date, self.old_appointment_date))

By now we have a next_available_date along with self.old_appointment_date to do the actual comparison

        if next_available_date != self.old_appointment_date:
            self.old_appointment_date = next_available_date
            logging.info(
                "Next interview date: {} | Old next interview date: {}".format(next_available_date,
                                                                               self.old_appointment_date))
            try:
                self.api.update_status(
                    "Global Entry appointment available in Atlanta on {} at {}".format(
                        next_available_date, next_available_date_time[1]))
            except Exception as e:
                logging.info("Exception: {}".format(e))

As you can see, the logic is if the next available interview time is not the same as my current interview time, make the next available interview time same as the current interview time. Note: this is the current setting for my twitter bot, so let me show you how to do this if you already have a scheduled time for a sooner appointment:

In [35]: bot = GlobalEntryBot()

In [36]: bot.old_appointment_date = datetime.datetime.today() # set this as your interview date

just set the old_appointment_date to the date of your interview (must be a datetime object)

Now What?

Part 3: Connect to Twitter and schedule task

Show Comments