Skip to content

Instantly share code, notes, and snippets.

@Azure-Agst
Last active June 25, 2018 21:31
Show Gist options
  • Save Azure-Agst/93fab0c3894f2a4a8cc554dc9b12269b to your computer and use it in GitHub Desktop.
Save Azure-Agst/93fab0c3894f2a4a8cc554dc9b12269b to your computer and use it in GitHub Desktop.
Team Scraper for HSEL
"""
TeamScraper.py
(c) 2018 Andrew Augustine
Based off a script by Moelandblue
"""
#import modules
import gspread, requests, json, time
from oauth2client.service_account import ServiceAccountCredentials
#import games.json
with open('games.json') as f:
gameids = json.load(f)
#declare start because https://www.serebii.net/pokedex-xy/360.shtml
print("TeamScraper.py; (c) 2018 Andrew Augustine")
#get start time
timestamp = time.ctime(time.time())
print("Started at: "+timestamp)
#set scope for google API to sheets and drive
scope = ['https://spreadsheets.google.com/feeds',
'https://www.googleapis.com/auth/drive']
#authenticate with google API
print("Connecting to Google API...")
credentials = ServiceAccountCredentials.from_json_keyfile_name('client.json', scope)
gc = gspread.authorize(credentials)
#grab sheet data for doc "NEW! HSEL Teams!"
print("Grabbing sheet data...")
sheet = gc.open("NEW! HSEL Teams!").sheet1
#reset sheet
print("Clearing sheet...")
sheet.clear()
#header
print("Inserting header data...")
sheet.append_row(["Hello! This is a list of every Summer Open team rurrently enrolled in a game! Press Ctrl+F (or ⌘+F on Mac) to search for a team."])
sheet.append_row(["Keep in mind, this does not include schools enrolled in the open. To see a list of schools, please visit https://www.varsityesports.com/clubs"])
sheet.append_row(["This sheet was autogenerated by TeamScraper.py. Currently Updating..."])
sheet.append_row([" ----- "])
sheet.append_row(["[Team ID]", "[Team Name]", "[Team Game]", "[Team URL]"])
#start grabbing data
print("Starting Scrape!")
for x in range(206): #207 teams, 206 since arrays start at 0
#API: https://www.varsityesports.com/api/team/{teamid}
teamid = str(x+1)
#get team's general data
team = requests.get("https://www.varsityesports.com/api/team/"+teamid)
if team.status_code != 200: #If the call was not a success
print("Error grabbing team id: "+teamid+"; Skipping...")
elif team.json() == []: #if we recieve an empty array
print("Team id "+teamid+" does not exist; Skipping...")
else: #We should be fine?
teamdata = team.json()
#get team's roster data for game ID
games = requests.get("https://www.varsityesports.com/api/team/"+teamid+"/roster")
if games.status_code != 200: #If the call was not a success
print("Error grabbing team games: "+teamid+"; Skipping...")
elif games.json() == []: #if we recieve an empty array
#sheet.append_row([teamid, teamdata['teamName'], " - ", "https://www.varsityesports.com/team/"+teamid])
print("Team id "+teamid+"; Name: "+teamdata['teamName']+"; Game not found; Skipping...")
else: #We should be fine?
gamedata = games.json()
teamgameid = int(gamedata[0]['gamesId'])
teamname = teamdata['teamName']
teamgame = gameids[str(teamgameid)]
sheet.append_row([teamid, teamname, teamgame, "https://www.varsityesports.com/team/"+teamid])
print("Team id "+teamid+"; Name: "+teamname+"; Game: "+teamgame+"; Added!")
sheet.append_row([" ----- "])
sheet.append_row(["Congrats, you made it to the end! If you want to see the source code of the bot used to make this page, see below!"])
sheet.append_row(["https://gist.github.com/Azure-Agst/93fab0c3894f2a4a8cc554dc9b12269b"])
sheet.update_cell(3, 1, "This sheet was autogenerated by TeamScraper.py. Last Update: "+timestamp)
print("Done! Exiting...")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment