GithubHelp home page GithubHelp logo

supercoach's Introduction

Supercoach

Scrape data from the Supercoach website using Python for easier player analysis.

Watch the full YT tutorial:

IMAGE ALT TEXT

import requests
from bs4 import BeautifulSoup
import json
from oauthlib.oauth2 import LegacyApplicationClient
from requests_oauthlib import OAuth2Session

def get_sc_token():
    # Credentials to generate token
    client_id = 'INSERT_YOUR_CLIENT_ID'
    client_secret = ''
    get_token_url = 'https://supercoach.heraldsun.com.au/2019/api/afl/classic/v1/access_token'

    # Supercoach credentials
    sc_user = 'INSERT_YOUR_SC_USERNAME'
    sc_pass = 'INSERT_YOUR_SC_PASSWORD'

    # Create token
    oauth = OAuth2Session(client=LegacyApplicationClient(client_id=client_id))
    token = oauth.fetch_token(token_url=get_token_url,
            username=sc_user, password=sc_pass, client_id=client_id,
            client_secret=client_secret)

    # Get token value from the Access_Token key
    sc_token = token["access_token"]
    return sc_token

def download_stats():
    # Get a token
    sc_token = get_sc_token()

    # Append token to the end of the Stats Centre URL
    sc_url = 'https://supercoach.heraldsun.com.au/afl/draft/statscentre?access_token=' + sc_token

    # Create text file to store AFL stats
    file_output = open("stats.txt", "w")

    # HTTP request to Stats Centre URL
    res = requests.get(sc_url)

    # Parse the response as HTML using the BeautifulSoup library
    soup = BeautifulSoup(res.text, 'html.parser')

    # Find the start and end position of the data which is stored in the researchGridData variable
    start_id = "var researchGridData = "
    end_id = "}]"
    stat_start = str(soup).find(start_id) + len(start_id)
    soup_len = len(str(soup))
    stat_end = str(soup)[stat_start:soup_len].find(end_id) + len(end_id) + stat_start

    # Format it to JSON
    raw_stats = str("{\n\"researchGridData\": " + str(soup)[stat_start:stat_end] + "}").encode('utf8')

    # Parse to JSON to make extracting key values much easier
    json_stats = json.loads(raw_stats)

    # Write to file
    # Starting with the heading
    file_output.write("First Name|Last Name|Pos1|Pos2|Team|Total Pts|Rds|Rd Pts|Avg|Avg 3|Avg 5|MVP|Status Comment|Player Note\n")
    # Then write each player to file
    for each in json_stats['researchGridData']:
        #==============================================================================================
        # Note: Some players don't have Player Notes or Status Comments, which will throw an exception.
        # So this will default the value to N/A if there's no Player Notes or Status Comments
        player_note = each.get('player_note', "N/A")
        status_comment = each.get('status_comment', "N/A")
        #==============================================================================================
        line = (str(each['fn']) + '|' + str(each['ln']) + '|' + str(each['pos']) + '|' + str(each['pos2']) + '|' + str(each['team']) + '|' + str(each['tpts']) + '|' + str(each['rds']) + '|' +
            str(each['pts']) + '|' + str(each['avg']) + '|' + str(each['avg3']) + '|' + str(each['avg5']) + '|' + str(each['mvp']) + '|' +
            str(status_comment) + '|' +
            str(player_note) + '\n')
        print(line)
        file_output.write(line)
    file_output.close()


# Call the Download Stats function
download_stats()

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.