exc-59ea2d328a02c7c938c9512e

OpenTable Affiliate Referral Links:

A Python Project – The Code

Part 2

Welcome to the second part of my 2 part series on writing a Python Application to handle OpenTable Affiliate Referral Links. If you missed my first article, it has a writeup of my favorite new development environment: Python 3.x + PyCharm Pro + Docker, so be sure to check it out.

I have been teaching myself Python over the course of the last 5 years. Prior to Python, most of the languages I dealt with were scripting languages inherent to operating systems maintenance or specialized within embedded systems or applications. I’ve hacked at some PHP, Powershell, Bash, HTML, and even some Java/Javascript over the years, but I’ve never considered myself a programmer or developer by trade.

Over the last decade, I’ve noticed a fundamental shift in the type of required work for systems engineers. Back when I started at the University of Nevada, Las Vegas (UNLV) in 2011, my primary job function was Systems Administration, but I was immediately pushed into systems integration, data manipulation, and system architectural design. That being said, I’ve casually observed the shift towards DevOps, as well as the overwhelming surge of new projects and specialized application deployments. In higher education, the cliche phrase: “Publish or perish” is often mentioned, but for higher education IT departments like mine, the new phrase is: “Automate or annihilate”. If you aren’t building systems or working towards an environment that self-heals, automates expansion, and can pivot on demand, you’re failing to deliver customer expectations.

With that said, I’ve been working on honing my Python skills for work, love of the language, and a chance to use my INTP wizarding ways. So when Teri asked me how to get links for our restaurant reviews and showed me that I had to deal with an OAuth2-based RESTful API, I jumped at the chance. In the sections below, you will find my code to help others who may be overwhelmed when dealing with OpenTable’s Affiliate’s Director API and OAuth2. The scripts that I’ve shared are by no means perfect for everyone’s needs, but should show the overall concept how to query the API and build a simple offline search. I invite any Python gurus to leave a comments with suggestions for improvements, as I am always interested in learning more elegant ways to write my code.

requirements.txt

Here are the contents that I used for my requirements.txt file shared between both scripts. If you do not want to use my Docker+PyCharm development environment method, install these plugins with PIP or the scripts will produce errors:

requests
pandas
tqdm

opentable-api.py

This script needs to be run first to collect all of the directory data from the OpenTable Directory API. Re-running this script will refresh cached directory data. It will generate a file called opentable_cache and will display a tqdm-based progress bar as the script loops through the pagination of the API. Pagination is required, as the API will only provide 100 results at a time and there are well over 40,000 restaurants in OpenTable’s Directory. You will need to update all relevant variables with information provided by OpenTable in your affiliate account. 

# /usr/bin/python3
# The purpose of this script is to cache data from the OpenTable Directory API.

# Import Required Modules
import pickle
import requests
from tqdm import tqdm

# Define OpenTable Related Variables
opentable_token_uri = 'url provided by opentable'
opentable_offset = 0
opentable_directory_api = 'url provided by opentable'
opentable_directory_query = '?offset='
opentable_directory_uri = opentable_directory_api + opentable_directory_query + str(opentable_offset)
opentable_clientid = 'provided by opentable'
opentable_secret = 'provided by opentable'

# Send POST to OpenTable to retrieve authentication token.
r = requests.post(opentable_token_uri, auth=(opentable_clientid, opentable_secret))

# Create variable for managing authentication and populate it from initial request
jar = r.cookies

# Create variable for holding the active access token
access_token = r.json()['access_token']

# Create mandatory header variable for interaction with the OpenTable Director API
headers = {'Content-Type': 'application/json', 'Accept-Encoding': 'lz4', 'Authorization': 'bearer '+access_token}

# Query OpenTable Directory API
r2 = requests.get(opentable_directory_uri, headers=headers, cookies=jar)
raw = r2.json()
opentable_data_set = []
opentable_data_set.extend(raw['items'])
print("Loading Data From OpenTable Directory API...")

# Initialize Progress Bar
pbar = tqdm(total=raw['total_items'])

# Loop through API at 100 restaurants/per query until all items are stored in opentable_data_set
while raw['offset'] < raw['total_items']:
    opentable_offset = opentable_offset + 100
    opentable_directory_uri = opentable_directory_api + opentable_directory_query + str(opentable_offset)
    r2 = requests.get(opentable_directory_uri, headers=headers, cookies=jar)
    raw = r2.json()
    opentable_data_set.extend(raw['items'])
    pbar.update(100)  # Trigger Progress Bar Update

# Terminate Progress Bar
pbar.close()

# Refresh Cache
print('')
print('')
print('OpenTable Directory API Cache Refreshed')
with open('opentable_cache', 'wb') as handle:
    pickle.dump(opentable_data_set, handle)
handle.close()

opentable-search.py

After running opentable-api.py and generating the opentable_cache file, the next step is to run this script, as necessary, to search the cache for names to locate your desired restaurant to generate your required referral links.

# /usr/bin/python3
# The purpose of this script is to search cached data from the OpenTable Directory API.

# Import Required Modules
import pickle
import pandas as pd

# Define OpenTable Related Variables
opentable_refID = 'provided by opentable'

# OpenTable Directory API Cache Refresh Function


with open('opentable_cache', 'rb') as handle:
    opentable_data_set = pickle.load(handle)

# Load OpenTable Directory JSON Data into a Pandas Dataframe
df = pd.DataFrame(opentable_data_set)

# Request Name Restaurant's Name From User
restaurant = input("Search By Restaurant Name: ")

# Select Variables
results = df[df['name'].str.contains(restaurant)]
results_found = results['name'].count()
formatted_results = results[['name', 'metro_name', 'country']]

# Prevent Pandas from printing automatically formatted across multiple lines.
pd.set_option('expand_frame_repr', False)
# Print Total Search Results
print('')
print('Total Results Found: ' + str(results_found))

# Print Blank Line
print('')

# Print Formatted Results
print(formatted_results)

# Print Blank Line
print('')

# Request Row Number From User
row = int(input('Enter the leftmost line number to select a restaurant: '))

# Print Blank Line
print('')

# Set Selected Row
row_selected = df.loc[row]
reservation_link = str(row_selected['reservation_url'])

# Print Referral Link
print('Opentable Referral Link:')
print(reservation_link + '?ref=' + opentable_refID)
handle.close()
I hope these scripts help others save time when dealing with the OpenTable Directory API. Please leave a comment in the comments section if you have feedback on the scripts, as I’d love to hear from you.

Cheers!


Discover more from Warmest Robot + Warmest Human

Subscribe to get the latest posts sent to your email.

One thought on “OpenTable Affiliate Referral Links – Part 2”

Comments are closed.