Hello dear community,
I just made an update for the app Sailgoal which can be found in https://openrepos.net/content/emanymton/sailgoal.
The app uses Germanys first TV channel (ARD) Teletext for displaying football results of European Leagues.
I updates the page links and removed leagues that are no longer supported.
The file to be changed can be found in
usr\share\harbour-sailgoal
and it’s called
livescore_api.py
Here is the code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from urllib.request import urlopen
from lxml import html
import time
import datetime
import calendar
import locale
URL = 'https://www.ard-text.de/mobil/{}'
LEAGUE_ID = {'1.Bundesliga': 252,
'2.Bundesliga': 276,
'3.Bundesliga': 502,
'Premier League': 522,
'Serie A': 526,
'Primera Division': 524,
'Ligue 1': 528,
'Eredivisie': 531,
'Jupiler League':532,
'SPORTOTO Süper Lig': 536,
'Super League': 537,
'Liga NOS':535,
'Raiffeisen Super League': 534,
'tipico - Bundesliga':533,
'ALKA Superligaen': 539,
'Scottish Premiership': 538,
'T-Mobile Ekstraklasa': 540,
'SYNOT Liga': 541, #'1. HNL': 546, 'Jelen SuperLiga': 547,
#'Liga I': 548, 'Premier Liga': 549,
#'CL Group A-B': 678, 'CL Group C-D': 679, 'CL Group E-F': 680,
#'CL Group G-H': 681,
#'EL Group A-D': 683, 'EL Group E-H': 684, 'EL Group I-L': 685,
#'Group A-C': 686, 'Group D-F':687, 'Group G-I':688,
#'UEFA EURO 2016 A': 817,
#'1 Champions League': 553,
#'2 Europa League': 554,
#'3 Conference League': 555,
#'DFB-Pokal': 551
}
LEAGUE_COUNTRY = {'1.Bundesliga': 'Germany',
'2.Bundesliga': 'Germany',
'3.Bundesliga': 'Germany',
'Premier League': 'England',
'Serie A': 'Italy',
'Primera Division': 'Spain',
'Eredivisie': 'Netherlands',
'Jupiler League': 'Belgium',
'Ligue 1': 'France',
'SPORTOTO Süper Lig':'Turkey',
'Super League': 'Greece',
'Liga NOS': 'Portugal',
'Raiffeisen Super League': 'Switzerland',
'tipico - Bundesliga': 'Austria',
'ALKA Superligaen': 'Denmark',
#'Allsvenskan': 'Sweden',
'Scottish Premiership': 'Scotland',
'T-Mobile Ekstraklasa': 'Poland',
'SYNOT Liga':'Czechia', #'1. HNL': 'Croatia',
#'Jelen SuperLiga': 'Serbia',
#'Liga I': 'Romania',
#'Premier Liga': 'Russia',
#'CL Group A-B': 'Champions League',
#'CL Group C-D': 'Champions League',
#'CL Group E-F': 'Champions League',
#'CL Group G-H': 'Champions League',
#'EL Group A-D': 'Europa League',
#'EL Group E-H': 'Europa League',
#'EL Group I-L': 'Europa League',
#'Group A': 'UEFA Euro 2016',
#'Group D-F':'UEFA Euro',
#'Group G-I': 'UEFA Euro',
#'1 Champions League': 'EuroCups',
#'2 Europa League': 'EuroCups',
#'3 Conference League': 'EuroCups',
#'DFB-Pokal': 'Germany',
}
def utc_to_local(utc_dt):
# get integer timestamp to avoid precision lost
timestamp = calendar.timegm(utc_dt.timetuple())
local_dt = datetime.datetime.fromtimestamp(timestamp)
assert utc_dt.resolution >= datetime.timedelta(microseconds=1)
return local_dt.replace(microsecond=utc_dt.microsecond)
class LeagueAPI(object):
def __init__(self, league_id):
self.league_id = league_id
def get_matchday(self, matchday=''):
url = URL.format(self.league_id)
#print(url)
# for testing: ###########
#displayed_games = 9
##########################
results = []
tree = html.parse(urlopen(url))
#if self.league_id in [252, 276]:
# xpath_search_string = "//div[@class='std']"
#else:
xpath_search_string = "//div[@class='std']"
#print(xpath_search_string)
#print(tree.xpath(xpath_search_string))
#print(len(tree.xpath(xpath_search_string)))
for item in tree.xpath(xpath_search_string):
match = item.xpath("./div[@class='matchPair']")
result = item.xpath("./div[@class='matchResult']//text()")
print(match, result)
if match:
print(match[0].xpath(".//text()"))
match_pairing = match[0].xpath(".//text()")
if self.league_id in [252, 276]:
home, guest = match[0].xpath(".//text()")[1].split(" - ")
else:
home, guest = match[0].xpath(".//text()")[0].split(" - ")
home = home.strip()
guest = guest.strip()
game_id = match[0].xpath("./a/@href")
if game_id:
game_id = int(game_id[0].split('/')[-1])
else:
game_id = 0
print(home, guest)
print('GAME_ID', game_id)
#game_id = 0
print(result)
if result:
if len(result)>1:
status = 'live'
result = ''.join(result)
elif result[0] == '-:- (-:-)':
status = 'vorschau'
result = result[0]
else:
status = 'beendet'
result = result[0]
else:
status = 'vorschau'
result = ''
### for testing ###########
#if displayed_games > 0:
# status = 'live'
# displayed_games -= 1
########################
#print(home, guest, status, result, game_id, time)
results.append({'home': home, 'result': result,
'guest': guest.strip(),
'game_id': game_id, 'status': status,
'time': time_str})
else:
time_str = item.xpath(".//b/text()")
if time_str:
time_str = time_str[0]
time_str = ' '.join([item for item in time_str.split(' ') if item])
time_str = time_str.replace(',','')
time_str = self.convert_to_datetime(time_str)
else:
time_str =''
#print(time_str)
#print(results)
return results
def get_ranking(self):
if self.league_id in [252, 276, 502, 522, 524, 526, 528]:
league_id = self.league_id + 1
else:
league_id = self.league_id
url = URL.format(league_id)
tree = html.parse(urlopen(url))
ranking = []
for entry in tree.xpath("//div[@class='std']//tr")[1:]:
row = entry.xpath("./td/text()")
row = [item.strip() for item in row]
if row:
#print(row)
if len(row) > 5:
column_goal_diff = 6
else:
column_goal_diff = 3
#print(column_goal_diff, row[column_goal_diff].split(':'))
try:
goals_shot, goals_get = row[column_goal_diff].split(':')
goal_diff = str(int(goals_shot) - int(goals_get))
except:
goal_diff = ''
if len(row) > 5:
rank, team, games_played, _ , _ , _, goals, points = row
else:
rank, team, games_played, goals, points = row
ranking.append({'rank': rank, 'team': team,
'games_played': games_played, 'goals': goals,
'goal_diff': goal_diff, 'points': points})
else:
ranking.append({'rank': '', 'team': '',
'games_played': '', 'goals': '',
'goal_diff': '', 'points': ''})
#for row in ranking:
#print(row)
return ranking
def get_ticker(self):
league_ticker_id = {252:272, 276:287}
url = URL.format(league_ticker_id[self.league_id])
tree = html.parse(urlopen(url))
ticker_text = []
for item in tree.xpath("//div[@class='std']"):
for entry in item.xpath("./h3"):
time = entry.xpath("./text()")[0].strip()
text = entry.xpath("./following-sibling::p[1]/text()")[0].replace('\n', ' ').strip()
ticker_text.append({'time': time, 'ticker_text': text})
return ticker_text
def get_match_info(self, game_id):
matchinfo = []
url = URL.format(game_id)
tree = html.parse(urlopen(url))
game = tree.xpath("//h1/text()")[0].strip()
data = tree.xpath("//div[@class='std']")
result = data[0].xpath('.//text()')[0]
#print(game, result)
for item in data[1:]:
#print(item.xpath('.//text()'))
data1 = item.xpath('.//text()')
yellow_cards = item.xpath('.//i/text()')
#print(data1, yellow_cards)
for yellow_card in yellow_cards:
for data in data1:
#print(yellow_card, data)
if yellow_card in data:
#print("ersetzen")
data1[data1.index(data)] = data.replace(yellow_card,
'<font color="#FFFF00">{}</font>'.format(yellow_card))
#print(data1)
data1 = ''.join([data.replace('\n\n', '<br>') for data in data1])
data1 = data1.replace('\n','<br>')
if self.league_id == 252:
url = URL.format(int(game_id)+1)
tree = html.parse(urlopen(url))
data2 = tree.xpath("//div[@class='std']//text()")
data2 = ''.join([data.replace('\n\n', '<br>') for data in data2])
data1 = data1.replace('\n','<br>')
else:
data2 = ''
matchinfo.append({'game': game, 'result': result, 'data1': data1, 'data2': data2})
#print(matchinfo)
return matchinfo
def convert_to_datetime(self, time_str):
#time_str = time_str.replace('29','28')
this_year = datetime.date.today().year
print(this_year)
UTC_OFFSET = 1
if time_str.endswith("Uhr"):
time_representation_in = "%A %d.%B %H.%M Uhr"
time_representation_out = "%A, %d.%B %H.%M"
else:
time_representation_in = "%A %d.%B"
time_representation_out = "%A, %d.%B"
print(time_str, locale.getlocale())
try:
system_locale = ".".join(locale.getlocale())
print(system_locale)
except TypeError:
system_locale = "de_DE.utf8"
#print("Testing...")
locale.setlocale(locale.LC_TIME, "de_DE.utf8")
# quick and dirty fix for different datetimerepresentation, TODO!
try:
datetime_obj = datetime.datetime.strptime(time_str, time_representation_in)
except ValueError:
time_representation_in = "%A %d.%B %H.%M Uhr"
datetime_obj = datetime.datetime.strptime(time_str, time_representation_in)
datetime_obj = datetime_obj.replace(year = this_year)
datetime_obj = datetime_obj - datetime.timedelta(hours=UTC_OFFSET)
locale.setlocale(locale.LC_TIME, system_locale)
#print(datetime_obj.strftime(time_representation_out))
return "<b> " + utc_to_local(datetime_obj).strftime(time_representation_out) +"</b>"
if __name__ == '__main__':
#for league_id in LEAGUE_ID.values():
for league_id in [252]:
league = LeagueAPI(league_id)
print(league_id)
print(league.get_matchday())
#league.get_ranking()
#print(league.get_ticker())
#print(league.get_match_info(254))
#print(league.get_match_info(278))
just copy and paste everything and overwrite the existing code, you need root rights for it.
unfortunately i didn’t find the sourcecode on any hub pages so I post it here in hope somebody else wants to use it.