-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
146 lines (133 loc) · 6.8 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env python
import base64
import json
import requests
requests.packages.urllib3.disable_warnings()
from prettytable import PrettyTable
import gspread
from oauth2client.service_account import ServiceAccountCredentials
from re import sub
from decimal import Decimal
# read json settings
auth_json = json.loads(open('auth.json').read())
params_json = json.loads(open('params.json').read())
# stubhub headers
headers = {
'Content-Type' : 'application/x-www-form-urlencoded',
'Authorization' : 'Bearer '+ auth_json['auth_bearer']
}
sh_update_headers = {
'Content-Type' : 'application/json',
'Authorization' : 'Bearer '+ auth_json['auth_bearer']
}
# stubhub URLS and vars
events_url = 'https://api.stubhub.com/search/catalog/events/v3'
events_qs = params_json['event_qs']
events = requests.get(events_url, params=events_qs, headers=headers)
events_json = json.loads(events.text)
sorted_events = sorted(events_json['events'], key=lambda d: d['id'])
inventory_url = 'https://api.stubhub.com/search/inventory/v2'
listings_url = 'https://api.stubhub.com/accountmanagement/listings/v1/seller/%s' % auth_json['stubhub_userid']
# use creds to create a client to interact with the Google Drive API
scope = ['https://spreadsheets.google.com/feeds']
creds = ServiceAccountCredentials.from_json_keyfile_name('client_secret.json', scope)
client = gspread.authorize(creds)
# Find a workbook by name and open the first sheet
# Make sure you use the right name here.
sheet = client.open(params_json['g_spreadsheet']).sheet1
# Find google sheet column to check if we need to update our price
find_update_col = sheet.find('Update?')
update_col = find_update_col.col
# Get google sheet row count so we can iterate over it
row_count = sheet.row_count
# Get the column for the price we have listed
find_listed_col = sheet.find('Listed')
listed_col = find_listed_col.col
# find the stubhub event id
find_event_id = sheet.find('SH event_id')
event_id_col = find_event_id.col
# Iterate over all the rows
curr_row = 1
while curr_row <= row_count:
# only act on rows that have Update? set to Y
if sheet.cell(curr_row, update_col).value == 'Y':
# get the price that we want to set
update_price = sheet.cell(curr_row, listed_col).value
# make our update price a float
update_price2 = Decimal(sub(r'[^\d.]', '', update_price))
event_id = sheet.cell(curr_row, event_id_col).value
print("Need to update price to " + str(update_price) + " for event id " + str(event_id))
# set the filter for the eventid so we can get the listing id
listings_eventid = 'filters=EVENT:%s' % event_id
my_listings = requests.get(listings_url, params=listings_eventid, headers=headers)
my_listings_json = json.loads(my_listings.text)
print(my_listings.status_code)
# set the payload to update the listing
update_json='{"listing": { "pricePerTicket": "%s" } }' % update_price2
# update listing url with listingid we got from the last call
update_listings_url = 'https://api.stubhub.com/inventory/listings/v1/%s' % my_listings_json['listings']['listing'][0]['id']
update_listing = requests.put(update_listings_url, headers=sh_update_headers, data=update_json)
update_listing.raw
print(update_listing.status_code)
# reset the Update? to N so we don't keep trying to update prices
sheet.update_cell(curr_row, update_col, 'N')
# increment the current row
curr_row += 1
for i in sorted_events:
eventid = 'eventid=' + str(i['id']) + '§ionstats=true&rows=1000§ionidlist=' + params_json['sectionidlist']
inventory = requests.get(inventory_url, params=eventid, headers=headers)
inventory_json = json.loads(inventory.text)
eventid2 = 'eventid=' + str(i['id']) + '§ionstats=true'
inventory2 = requests.get(inventory_url, params=eventid2, headers=headers)
inventory2_json = json.loads(inventory2.text)
s_stats = inventory_json['section_stats']
s_stats_table = PrettyTable(['Section',
'Remaining',
'Min price',
'Avg price',
'Max price'])
event_table = PrettyTable(['EventID',
'Event Time',
'Opponent',
'Tickets Remaining'])
event_table.add_row([str(i['id']),
str(i['eventDateLocal']),
str(i['performersCollection'][1]['name']),
str(inventory2_json['totalTickets'])])
print event_table
event_table = ''
section_prices = {}
for k in inventory_json['listing']:
section_prices.setdefault(k['sectionId'], {}).setdefault('prices', []).append(k['listingPrice']['amount'])
for l in section_prices:
min_price = format(min(section_prices[l]['prices']), '.2f')
max_price = format(max(section_prices[l]['prices']), '.2f')
total_prices = 0.0
for prices in section_prices[l]['prices']:
total_prices += prices
avg_price = format(total_prices / len(section_prices[l]['prices']), '.2f')
section_prices[l]['min_price'] = min_price
section_prices[l]['max_price'] = max_price
section_prices[l]['avg_price'] = avg_price
for sstats in inventory_json['section_stats']:
sectionid = sstats['sectionId']
sectionname = sstats['sectionName']
totaltickets = sstats['totalTickets']
section_prices[sectionid]['sectionname'] = sectionname
section_prices[sectionid]['totaltickets'] = totaltickets
s_stats_table.add_row([str(section_prices[sectionid]['sectionname']),
str(section_prices[sectionid]['totaltickets']),
str(section_prices[sectionid]['min_price']),
str(section_prices[sectionid]['avg_price']),
str(section_prices[sectionid]['max_price'])])
find_event_id = sheet.find(str(i['id']))
row_find = find_event_id.row
find_section_id = sheet.find(str(sstats['sectionId']))
col_find = find_section_id.col
e_t_remain = sheet.find('event_tix_remain')
e_t_r_col_find = e_t_remain.col
event_link_url_1 = 'https://www.stubhub.com/event/'
event_link_url_2 = '?sort=price+asc&sid='
sheet.update_cell(row_find, e_t_r_col_find, inventory2_json['totalTickets'])
sheet.update_cell(row_find, col_find, '=HYPERLINK("' + event_link_url_1 + str(i['id']) + event_link_url_2 + find_section_id.value + '","' + str(section_prices[sectionid]['min_price']) + '/' + str(section_prices[sectionid]['avg_price']) + '/' + str(section_prices[sectionid]['max_price']) + '")' )
print s_stats_table.get_string(sortby='Section')