-
-
Save ciberkeley/f5ac08ebcaf44b59b450f08c3f82e761 to your computer and use it in GitHub Desktop.
Proof of Concept task to get ticket prices and event info using StubHub's API with Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import base64 | |
import json | |
import pprint | |
import pandas as pd | |
import datetime | |
import email.utils | |
#### Step 1: # Obtaining StubHub User Access Token #### | |
## Enter user's API key, secret, and Stubhub login | |
app_token = raw_input('Enter app token: ') | |
consumer_key = raw_input('Enter consumer key: ') | |
consumer_secret = raw_input('Enter consumer secret: ') | |
stubhub_username = raw_input('Enter Stubhub username (email): ') | |
stubhub_password = raw_input('Enter Stubhub password: ') | |
## Generating basic authorization token | |
combo = consumer_key + ':' + consumer_secret | |
basic_authorization_token = base64.b64encode(combo) | |
print basic_authorization_token | |
## POST parameters for API call | |
headers = { | |
'Content-Type':'application/x-www-form-urlencoded', | |
'Authorization':'Basic '+basic_authorization_token,} | |
body = { | |
'grant_type':'password', | |
'username':stubhub_username, | |
'password':stubhub_password, | |
'scope':'PRODUCTION'} | |
## Making the call | |
url = 'https://api.stubhub.com/login' | |
r = requests.post(url, headers=headers, data=body) | |
token_respoonse = r.json() | |
access_token = token_respoonse['access_token'] | |
user_GUID = r.headers['X-StubHub-User-GUID'] | |
#### Step 2 - Searching inventory for an event #### | |
inventory_url = 'https://api.stubhub.com/search/inventory/v1' | |
headers['Authorization'] = 'Bearer ' + access_token | |
headers['Accept'] = 'application/json' | |
headers['Accept-Encoding'] = 'application/json' | |
eventid = '9394728' | |
data = {'eventid':eventid} | |
inventory = requests.get(inventory_url, headers=headers, params=data) | |
inv = inventory.json() | |
listing = inv['listing'] | |
## Flattening some nested dictionary for ticket price | |
for t in listing: | |
for k,v in t.items(): | |
if k == 'currentPrice': | |
t['amount'] = v['amount'] | |
## Converting to Pandas dataframe and exporting to CSV | |
listing_df = pd.DataFrame(listing) | |
listing_df.to_csv(open('export.csv', 'wb')) | |
#### Step 3 - Adding Event and Venue Info #### | |
## Calling the eventsearch api | |
info_url = 'https://api.stubhub.com/catalog/events/v2/' + eventid | |
info = requests.get(info_url, headers=headers) | |
pprint.pprint(info.json()) | |
info_dict = info.json() | |
event_date = datetime.datetime.strptime(info_dict['eventDateLocal'][:10], '%Y-%m-%d') | |
full_title = info_dict['title'].split('[', 2) | |
event_name = full_title[0].strip() | |
event_date = full_title[1][:10] | |
venue = info_dict['venue']['name'] | |
snapshotdate = datetime.datetime.today().strftime('%m/%d/%Y') | |
my_col = ['SnapshotDate','EventName','EventDate', 'Venue', 'sectionName', 'row', | |
'seatNumbers', 'quantity', 'deliveryTypeList', 'amount'] | |
listing_df['SnapshotDate'] = snapshotdate | |
listing_df['EventName'] = event_name | |
listing_df['EventDate'] = event_date | |
listing_df['Venue'] = venue | |
final_df = listing_df[my_col] | |
## Exporting final report | |
final_df.to_csv(open('export.csv', 'wb'), index=None) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment