Coverage for footballbot/FootballBot.py : 25%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
"""LICENSE Copyright 2019 Hermann Krumrey <hermann@krumreyh.com>
This file is part of footballbot.
footballbot is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
footballbot is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with footballbot. If not, see <http://www.gnu.org/licenses/>. LICENSE"""
""" Class that defines the behaviour of the bot """
"bundesliga": "BL1", "germany1": "BL1", "germany2": "BL2", "germany3": "BL3", "premier league": "PL", "premierleague": "PL", "serie a": "SA", "seriea": "SA", "primera division": "PD", "primeradivision": "PD", "england1": "PL", "italy1": "SA", "spain1": "PD" }
""" Handles received messages :param message: The received message :param address: The address of the sender :return: None """ parsed = self.parse(message) if parsed is None: return _, command, args = parsed reply = cast(TextMessage, message.make_reply())
if command == "list": reply.body = "Available leagues:\n" \ "1. Bundesliga (germany1)\n" \ "2. Bundesliga (germany2)\n" \ "3. Liga (germany3)\n" \ "Premier League (england1)\n" \ "Primera Division (spain1)\n" \ "Serie A (italy1)" elif command == "table": reply.body = self._get_table(args["league"]) elif command == "scorers": reply.body = self._get_goalscorers(args["league"]) else: return
self.connection.send(reply)
""" :return: The name of the bot """ return "footballbot"
""" Defines the command parsers to be used :return: The parsers to use """ return [TableParser()]
""" :return: The list of required settings parameters """ return ["football_info_api_key"]
""" Load data from an API endpoint :param endpoint: The endpoint to call :return: The retrieved data, or None if something failed """ headers = {"X-Auth-Token": self.extras["football_info_api_key"]} url = "https://api.football-data.org/v2/{}".format(endpoint) resp = requests.get(url, headers=headers)
if resp.status_code != 200: return None
return json.loads(resp.text)
""" Retrieves the current league table of a league :param league: The league for which to fetch the data :return: The (formatted) table as a string, or an error message if the requested league/year did not exist """ league = self.leaguemap.get(league.lower(), league) endpoint = "competitions/{}/standings".format(league) data = self._load_data(endpoint)
if data is None: return "Invalid League {}".format(league)
table_msg = "# | Team | Points" table = data["standings"][0]["table"] for position in table: team = position["team"] table_msg += "\n{} | {} | {}".format( position["position"], team["name"], position["points"] )
return table_msg
""" Retrieves a list of goal scorers for a league :param league: :return: """
league = self.leaguemap.get(league.lower(), league) endpoint = "competitions/{}/scorers?limit=25".format(league) data = self._load_data(endpoint)
if data is None: return "Invalid League {}".format(league)
scorers_msg = "# | Player | Goals" for i, scorer in enumerate(data["scorers"]): scorer_name = scorer["player"]["name"] goals = scorer["numberOfGoals"] scorers_msg += "\n{} | {} | {}".format( i + 1, scorer_name, goals )
return scorers_msg |