pyircbot/pyircbot/modules/NFLLive.py

225 lines
8.3 KiB
Python
Raw Normal View History

2014-10-12 21:08:20 -07:00
"""
.. module:: NFLLive
2015-11-01 18:03:11 -08:00
:synopsis: Show upcoming NFL games and current scores.
2014-10-12 21:08:20 -07:00
.. moduleauthor:: Dave Pedu <dave@davepedu.com>
"""
2017-11-27 18:58:20 -08:00
from pyircbot.modulebase import ModuleBase, command
from pyircbot.modules.ModInfo import info
2014-10-12 21:08:20 -07:00
from time import time
from requests import get
from lxml import etree
2017-01-01 14:59:01 -08:00
from datetime import datetime, timedelta
2014-10-12 21:08:20 -07:00
class NFLLive(ModuleBase):
2015-11-01 18:03:11 -08:00
def __init__(self, bot, moduleName):
2017-01-01 14:59:01 -08:00
ModuleBase.__init__(self, bot, moduleName)
2015-11-01 18:03:11 -08:00
self.cache = None
2017-01-01 14:59:01 -08:00
self.cacheAge = 0
2017-11-27 18:58:20 -08:00
@info("nfl show nfl schedule & score", cmds=["nfl"])
@command("nfl")
def nflitup(self, message, cmd):
games = self.getNflGamesCached()
msg = []
liveGames = []
gamesLaterToday = []
gamesToday = []
gamesUpcoming = []
gamesEarlierWeek = []
# sort games
for game in games["games"]:
if game["time"] is not None:
liveGames.append(game)
elif game["quarter"] == "P" and game["startdate"].day == datetime.now().day:
gamesLaterToday.append(game)
elif game["startdate"].day == datetime.now().day:
gamesToday.append(game)
elif game["startdate"].day > datetime.now().day:
gamesUpcoming.append(game)
2015-11-01 18:03:11 -08:00
else:
2017-11-27 18:58:20 -08:00
gamesEarlierWeek.append(game)
# create list of formatted games
liveGamesStr = []
for game in liveGames:
liveGamesStr.append(self.formatGameLive(game))
liveGamesStr = ", ".join(liveGamesStr)
gamesLaterTodayStr = []
for game in gamesLaterToday:
gamesLaterTodayStr.append(self.formatGameFuture(game))
gamesLaterTodayStr = ", ".join(gamesLaterTodayStr)
gamesTodayStr = []
for game in gamesToday:
gamesTodayStr.append(self.formatGamePast(game))
gamesTodayStr = ", ".join(gamesTodayStr)
gamesUpcomingStr = []
for game in gamesUpcoming:
gamesUpcomingStr.append(self.formatGameFuture(game))
gamesUpcomingStr = ", ".join(gamesUpcomingStr)
gamesEarlierWeekStr = []
for game in gamesEarlierWeek:
gamesEarlierWeekStr.append(self.formatGamePast(game))
gamesEarlierWeekStr = ", ".join(gamesEarlierWeekStr)
msgPieces = []
msgPieces.append("\x02NFL week %s\x02:" % (games["season"]["week"]))
# Depending on args build the respon pieces
if len(cmd.args) > 0 and cmd.args[0] == "today":
if not liveGamesStr == "":
msgPieces.append("\x02Playing now:\x02 %s" % liveGamesStr)
if not gamesLaterTodayStr == "":
msgPieces.append("\x02Later today:\x02 %s" % gamesLaterTodayStr)
if not gamesTodayStr == "":
msgPieces.append("\x02Earlier today:\x02 %s" % gamesTodayStr)
elif len(cmd.args) > 0 and cmd.args[0] == "live":
if not liveGamesStr == "":
msgPieces.append("\x02Playing now:\x02 %s" % liveGamesStr)
elif len(cmd.args) > 0 and cmd.args[0] == "scores":
if not liveGamesStr == "":
msgPieces.append("\x02Playing now:\x02 %s" % liveGamesStr)
if not gamesTodayStr == "":
msgPieces.append("\x02Earlier today:\x02 %s" % gamesTodayStr)
if not gamesEarlierWeekStr == "":
msgPieces.append("\x02Earlier this week: \x02 %s" % gamesEarlierWeekStr)
else:
if not liveGamesStr == "":
msgPieces.append("\x02Playing now:\x02 %s" % liveGamesStr)
if not gamesLaterTodayStr == "":
msgPieces.append("\x02Later today:\x02 %s" % gamesLaterTodayStr)
if not gamesTodayStr == "":
msgPieces.append("\x02Earlier today:\x02 %s" % gamesTodayStr)
if not gamesEarlierWeekStr == "":
msgPieces.append("\x02Earlier this week: \x02 %s" % gamesEarlierWeekStr)
if not gamesUpcomingStr == "":
msgPieces.append("\x02Upcoming:\x02 %s" % gamesUpcomingStr)
# Collaspe the list into a repsonse string. Fix grammar
msg = ", ".join(msgPieces).replace(":, ", ": ")
# Nothing means there were probably no games
if len(msgPieces) == 1:
msg = "No games!"
if len(msg) > 0:
# The message can be long so chunk it into pieces splitting at commas
while len(msg) > 0:
piece = msg[0:330]
msg = msg[330:]
while not piece[-1:] == "," and len(msg) > 0:
piece += msg[0:1]
msg = msg[1:]
self.bot.act_PRIVMSG(message.args[0], "%s: %s" % (message.prefix.nick, piece.strip()))
2017-01-01 14:59:01 -08:00
2015-11-01 18:03:11 -08:00
def formatGameLive(self, game):
c_vis = 3 if int(game["visitor_score"]) > int(game["home_score"]) else 4
c_home = 4 if int(game["visitor_score"]) > int(game["home_score"]) else 3
2017-01-01 14:59:01 -08:00
2015-11-01 18:03:11 -08:00
return "\x03%s%s(%s)\x03 @ \x03%s%s(%s)\x03 Q%s %s" % (
c_vis,
game["visitor"],
game["visitor_score"],
c_home,
game["home"],
game["home_score"],
game["quarter"],
game["time"]
)
2017-01-01 14:59:01 -08:00
2015-11-01 18:03:11 -08:00
def formatGameFuture(self, game):
return "\x02%s\x02@\x02%s\x02" % (
game["visitor"],
game["home"]
)
2017-01-01 14:59:01 -08:00
2015-11-01 18:03:11 -08:00
def formatGamePast(self, game):
c_vis = 3 if int(game["visitor_score"]) > int(game["home_score"]) else 4
c_home = 4 if int(game["visitor_score"]) > int(game["home_score"]) else 3
2017-01-01 14:59:01 -08:00
2015-11-01 18:03:11 -08:00
return "\x03%s%s(%s)\x03@\x03%s%s(%s)\x03" % (
c_vis,
game["visitor"],
game["visitor_score"],
c_home,
game["home"],
game["home_score"]
)
2017-01-01 14:59:01 -08:00
2015-11-01 18:03:11 -08:00
def getNflGamesCached(self):
2017-01-01 14:59:01 -08:00
if time() - self.cacheAge > self.config["cache"]:
2015-11-01 18:03:11 -08:00
self.cache = NFLLive.getNflGames()
self.cacheAge = time()
return self.cache
2017-01-01 14:59:01 -08:00
2015-11-01 18:03:11 -08:00
@staticmethod
def getNflGames():
result = {}
2017-01-01 14:59:01 -08:00
2015-11-01 18:03:11 -08:00
# Fetch NFL information as XML
nflxml = get("http://www.nfl.com/liveupdate/scorestrip/ss.xml?random=1413140448433")
doc = etree.fromstring(nflxml.content)
games = doc.xpath("/ss/gms")[0]
2017-01-01 14:59:01 -08:00
result["season"] = {
"week": games.attrib["w"],
"year": games.attrib["y"],
"type": NFLLive.translateSeasonType(games.attrib["t"]), # R for regular season, probably P for pre (?)
"gameday": int(games.attrib["gd"]), # 1 or 0 for gameday or not (?)
"bph": games.attrib["bph"] # not sure
2015-11-01 18:03:11 -08:00
}
2017-01-01 14:59:01 -08:00
result["games"] = []
2015-11-01 18:03:11 -08:00
for game in games.getchildren():
gameblob = {
2017-01-01 14:59:01 -08:00
"home": game.attrib["h"],
"home_name": game.attrib["hnn"],
"home_score": game.attrib["hs"],
"visitor": game.attrib["v"],
"visitor_name": game.attrib["vnn"],
"visitor_score": game.attrib["vs"],
"gametype": game.attrib["gt"], # REGular season, probably P for preseason (?)
"quarter": game.attrib["q"], # P if not started, 1-4, F is finished
"time": game.attrib["k"] if "k" in game.attrib else None,
"id": game.attrib["eid"],
"gamenum": game.attrib["gsis"],
"starttime": game.attrib["t"],
"startdate": datetime.strptime(game.attrib["eid"][0:-2] + " " + game.attrib["t"], "%Y%m%d %I:%M") + \
timedelta(hours=12) # NHL provides a 12 hour EST clock with all times PM.
# Add 12 hours so the datetime obj is PM instead of AM.
2015-11-01 18:03:11 -08:00
}
2017-01-01 14:59:01 -08:00
2015-11-01 18:03:11 -08:00
# Add 4 more hours to make it GMT
2017-01-01 14:59:01 -08:00
gameblob["startdate_gmt"] = gameblob["startdate"] + timedelta(hours=4)
gameblob["nfl_link"] = "http://www.nfl.com/gamecenter/%s/%s/%s%s/%s@%s" % (
2015-11-01 18:03:11 -08:00
gameblob["id"],
result["season"]["year"],
gameblob["gametype"],
result["season"]["week"],
gameblob["visitor_name"],
gameblob["home_name"])
2017-01-01 14:59:01 -08:00
2015-11-01 18:03:11 -08:00
result["games"].append(gameblob)
return result
2017-01-01 14:59:01 -08:00
2015-11-01 18:03:11 -08:00
@staticmethod
def translateSeasonType(season):
2017-01-01 14:59:01 -08:00
if season == "R":
2015-11-01 18:03:11 -08:00
return "Regular"
2017-01-01 14:59:01 -08:00
if season == "P":
2015-11-01 18:03:11 -08:00
return "Pre"
return season