Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion lib/lsprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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
58 changes: 53 additions & 5 deletions lib/lsweb.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
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



12 changes: 9 additions & 3 deletions livescore.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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')

Expand All @@ -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.")
Expand Down