Skip to content

Instantly share code, notes, and snippets.

@prs-watch
Last active December 23, 2017 04:10
Show Gist options
  • Select an option

  • Save prs-watch/23ef3ef46fcd1319d58bd3b8bb36603b to your computer and use it in GitHub Desktop.

Select an option

Save prs-watch/23ef3ef46fcd1319d58bd3b8bb36603b to your computer and use it in GitHub Desktop.
  • Python Code
import requests
from bs4 import BeautifulSoup
import re


class Base:
    URL = 'https://www.baseball-reference.com/players/g/gallayo01.shtml'
    YEAR = '2011'


class From:
    URL = 'https://www.baseball-reference.com/players/j/jimenub01.shtml'
    YEAR = '2010'


class Style:
    BLACK = '\033[30m'
    RED = '\033[31m'
    GREEN = '\033[32m'
    YELLOW = '\033[33m'
    BLUE = '\033[34m'
    PURPLE = '\033[35m'
    CYAN = '\033[36m'
    WHITE = '\033[37m'
    END = '\033[0m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'
    INVISIBLE = '\033[08m'
    REVERCE = '\033[07m'


class Dumper:
    stat_nms = []

    @classmethod
    def get_http_content(cls, player):
        res = requests.get(player.URL)
        return BeautifulSoup(res.text, 'html.parser')

    @classmethod
    def set_name(cls, player):
        cont = cls.get_http_content(player)
        player.NAME = cont.find('h1').string

    @classmethod
    def set_stats(cls, player):
        stats = {}
        cont = cls.get_http_content(player)
        row_id = 'pitching_standard.{}'.format(player.YEAR)
        tds = cont.find('tr', id=row_id).find_all('td')
        for td in tds:
            stat_nm = td.get('data-stat')
            if stat_nm not in cls.stat_nms:
                cls.stat_nms.append(stat_nm)
            stat_val = td.string
            stats[stat_nm] = stat_val
        player.STATS = stats

    @classmethod
    def dump(cls, players):
        body = []
        header = []
        max_name_len = 0

        body.append(Style.BOLD + '##################' + Style.END)
        body.append(Style.BOLD + '#                #' + Style.END)
        body.append(Style.BOLD + '# COMPARE RESULT #' + Style.END)
        body.append(Style.BOLD + '#                #' + Style.END)
        body.append(Style.BOLD + '##################' + Style.END)
        # header
        for player in players:
            name = player.NAME
            year = player.YEAR
            str = '{name}({year})'.format(
                name=Style.BOLD + name + Style.END, year=year)
            header.append(str)
            max_name_len = len(name) if max_name_len < len(
                name) else max_name_len
        header_str = ' vs. '.join(header)
        body.append(header_str)
        body.append('\n')

        # compare
        for stat_nm in cls.stat_nms:
            stat_class = []
            pattern = re.compile('^[0-9\.]*$')
            stat_class.append(Style.UNDERLINE + '###' + ' ' +
                              stat_nm.upper() + ' ' + '###' + Style.END)
            for player in players:
                bar = ''
                stat_val = 0
                name = player.NAME.rjust(max_name_len, ' ')
                stat_str = player.STATS[stat_nm]
                if stat_str is not None and pattern.match(stat_str):
                    stat_str = '0' + \
                        stat_str if stat_str.startswith('.') else stat_str
                    stat_val = float(stat_str)
                    stat_val = stat_val / 10 if stat_val >= 100 else stat_val
                    stat_val = int(stat_val)
                else:
                    stat_class.clear()
                    break
                for i in range(stat_val):
                    bar = bar + '='
                stat_class.append('{name}|{bar}{stat_val}'.format(
                    name=Style.BOLD + name + Style.END, bar=bar, stat_val=stat_str))
            if len(stat_class) != 0:
                stat_class.append('\n')
                cls_str = '\n'.join(stat_class)
                body.append(cls_str)

        body_str = '\n'.join(body)
        print(body_str)


class UI:
    PLAYERS = [Base, From]

    @classmethod
    def execute(cls):
        for player in cls.PLAYERS:
            Dumper.set_name(player)
            Dumper.set_stats(player)
        Dumper.dump(cls.PLAYERS)

if __name__ == '__main__':
    UI.execute()
  • Result
##################
#                #
# COMPARE RESULT #
#                #
##################
Yovani Gallardo(2011) vs. Ubaldo Jiménez(2010)


### AGE ###
Yovani Gallardo|=========================25
 Ubaldo Jiménez|==========================26


### W ###
Yovani Gallardo|=================17
 Ubaldo Jiménez|===================19


### L ###
Yovani Gallardo|==========10
 Ubaldo Jiménez|========8


### WIN_LOSS_PERC ###
Yovani Gallardo|0.630
 Ubaldo Jiménez|0.704


### EARNED_RUN_AVG ###
Yovani Gallardo|===3.52
 Ubaldo Jiménez|==2.88


### G ###
Yovani Gallardo|=================================33
 Ubaldo Jiménez|=================================33


### GS ###
Yovani Gallardo|=================================33
 Ubaldo Jiménez|=================================33


### GF ###
Yovani Gallardo|0
 Ubaldo Jiménez|0


### CG ###
Yovani Gallardo|=1
 Ubaldo Jiménez|====4


### SHO ###
Yovani Gallardo|=1
 Ubaldo Jiménez|==2


### SV ###
Yovani Gallardo|0
 Ubaldo Jiménez|0


### IP ###
Yovani Gallardo|====================207.1
 Ubaldo Jiménez|======================221.2


### H ###
Yovani Gallardo|===================193
 Ubaldo Jiménez|================164


### R ###
Yovani Gallardo|============================================================================================92
 Ubaldo Jiménez|=========================================================================73


### ER ###
Yovani Gallardo|=================================================================================81
 Ubaldo Jiménez|=======================================================================71


### HR ###
Yovani Gallardo|===========================27
 Ubaldo Jiménez|==========10


### BB ###
Yovani Gallardo|===========================================================59
 Ubaldo Jiménez|============================================================================================92


### IBB ###
Yovani Gallardo|=1
 Ubaldo Jiménez|=======7


### SO ###
Yovani Gallardo|====================207
 Ubaldo Jiménez|=====================214


### HBP ###
Yovani Gallardo|=1
 Ubaldo Jiménez|=========9


### BK ###
Yovani Gallardo|0
 Ubaldo Jiménez|=1


### WP ###
Yovani Gallardo|============12
 Ubaldo Jiménez|================16


### BATTERS_FACED ###
Yovani Gallardo|======================================================================================865
 Ubaldo Jiménez|=========================================================================================894


### EARNED_RUN_AVG_PLUS ###
Yovani Gallardo|===========112
 Ubaldo Jiménez|================161


### FIP ###
Yovani Gallardo|===3.59
 Ubaldo Jiménez|===3.10


### WHIP ###
Yovani Gallardo|=1.215
 Ubaldo Jiménez|=1.155


### HITS_PER_NINE ###
Yovani Gallardo|========8.4
 Ubaldo Jiménez|======6.7


### HOME_RUNS_PER_NINE ###
Yovani Gallardo|=1.2
 Ubaldo Jiménez|0.4


### BASES_ON_BALLS_PER_NINE ###
Yovani Gallardo|==2.6
 Ubaldo Jiménez|===3.7


### STRIKEOUTS_PER_NINE ###
Yovani Gallardo|=========9.0
 Ubaldo Jiménez|========8.7


### STRIKEOUTS_PER_BASE_ON_BALLS ###
Yovani Gallardo|===3.51
 Ubaldo Jiménez|==2.33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment