diff --git a/lib/lsprint.py b/lib/lsprint.py index 1612d4f..3626df3 100755 --- a/lib/lsprint.py +++ b/lib/lsprint.py @@ -192,4 +192,34 @@ def display_table(table, title='No Title'): text += f'{temp}\t' tlen += len(temp) print(text) - print_pattern('+', lmax, c.BLUE) \ No newline at end of file + print_pattern('+', lmax, c.BLUE) + + +def display_scorers(scorers, title = "No title"): + title = f'{title} TOP SCORERS' + length = _max_length_scorers(scorers) + max_length = sum(length) + 9 #(3*3) + + print_pattern('+', max_length, c.BLUE) + print(title.center(max_length)) + print_pattern('+', max_length, c.BLUE) + + color = c.GREEN + for i, row in enumerate(scorers): + if i == 1: + print_pattern('-', max_length, c.RESET) + line = f' {" ".join([e.ljust(length[j]) for j, e in enumerate(row)])}' + print(color + line) + color = c.DRAW + + print_pattern('+', max_length, c.BLUE) + + + + +def _max_length_scorers(scorers): + length = [0] * 3 + for scorer in scorers: + for i in range(3): + length[i] = max(length[i], len(scorer[i])) + return length diff --git a/lib/lsweb.py b/lib/lsweb.py index 88acbde..564e605 100755 --- a/lib/lsweb.py +++ b/lib/lsweb.py @@ -21,14 +21,24 @@ def get_tz_offset(): return offset / 60 / 60 * -1 -def get_livescores_url(name, type): +def get_livescores_url(name, type, scorers = False): tz_offset = get_tz_offset() - url = details.get(type).get(name).get('url') + f'/?tz={tz_offset}' + if not scorers: + url = details.get(type).get(name).get('url') + f'/?tz={tz_offset}' + else: + url = details.get(type).get(name).get('url') + f'/stats/goals/' + url = url[:17] + url[18:] + # Line 30 removes the trailing 's' from 'livescores'. + # This is due to the fact that the top scorers ranking was no + # longer available in the old website + # New website: www.livescore.com + # Old website: www.livescores.com + return url -def get_soup(name='bpl', event_type='competition'): - url = get_livescores_url(name, event_type) +def get_soup(name='bpl', event_type='competition', scorers = False): + url = get_livescores_url(name, event_type, scorers) html = requests.get(url).text soup = BeautifulSoup(html, 'html.parser') return soup @@ -139,4 +149,42 @@ def parse_table(soup): def get_table(name, event_type='competition'): soup = get_soup(name, event_type) table = parse_table(soup) - return table \ No newline at end of file + return table + +def get_scorers(name, event_type='competition'): + soup = get_soup(name, event_type, scorers = True) + scorers = parse_scorers(soup) + return scorers + +def parse_scorers(soup): + ''' + Returns a list containing tuples with the following structure: + (name, team, goals) + + players = [('Player', 'Team', 'Goals'), + ('Dusan Vlahovic', 'Juventus', '2'), + (''Marcus Thuram', 'Inter', '2'), + ('Jonathan David', 'Juventus', '1'), + ...] + ''' + + players_html = soup.find_all('div', attrs = {'class': 'e'}) + players = [('Player','Team','Goals')] + + for player in players_html: + name_div = player.find('div', attrs = {'class': 'h'}) + name = name_div.text.strip() if name_div else None + + team_div = name_div.parent.find_all('div') + team = team_div[1].text.strip() if len(team_div) > 1 and team_div[1] else None + + goals_span = player.find('span', attrs = {'class': 'i'}) + goals = goals_span.text.strip() if goals_span else None + + if goals and team and name: + players.append((name,team,goals)) + + return players + + + diff --git a/livescore.py b/livescore.py index be8635c..b4ff502 100755 --- a/livescore.py +++ b/livescore.py @@ -1,8 +1,8 @@ #!/usr/bin/env python3 # -*- coding:utf-8 -*- -from lib.lsweb import get_games, get_table, is_connected -from lib.lsprint import display_games, display_table, clear_screen +from lib.lsweb import get_games, get_table, is_connected, get_scorers +from lib.lsprint import display_games, display_table, clear_screen, display_scorers from lib.cli import args from lib.urls import details, base_url import time @@ -11,12 +11,13 @@ def main(): b_score = bool(args.score) b_table = bool(args.table) + b_scorers = bool(args.scorers) prev_data = {} while True: try: for cname in args.competition: - if is_connected('www.livescores.com'): + if is_connected('www.livescores.com') and is_connected('www.livescore.com'): event_type = 'competition' title = details.get(event_type).get(cname).get('title') @@ -33,6 +34,11 @@ def main(): print(f'displaying table for {title}') clear_screen() display_table(table, title) + if b_scorers: + scorers = get_scorers(cname, event_type) + print(f'displaying top scorers for {title}') + clear_screen() + display_scorers(scorers, title) else: print(f"couldn't connect to the livescore website. check your internet connection.")