Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

catchup changes from gamedaybot #135

Merged
merged 12 commits into from
Dec 29, 2023
7 changes: 6 additions & 1 deletion .github/workflows/publish_image.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ jobs:
tags: ${{ env.TAG }}

- name: Test image
run: docker run ${{ env.TAG }} python /usr/src/gamedaybot/setup.py test
run: |
docker run ${{ env.TAG }} /bin/sh -c "\
python -m pip install --upgrade pip && \
pip install -r requirements.txt && \
pip install -r requirements-test.txt && \
pytest"

- name: Push image to registry
uses: docker/build-push-action@v2
Expand Down
39 changes: 39 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: Run tests on PRs and commits

on:
push:
branches:
- main
- master
pull_request:
types: [opened, synchronize]
branches:
- main
- master
workflow_dispatch:

env:
BOT_ID: 916ccfd76a7fda25c74d09e1d5
LEAGUE_ID: 164483
TEST_TAG: user/test_build:test

jobs:
test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Set up Python 3.9
uses: actions/setup-python@v2
with:
python-version: 3.9

- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install -r requirements.txt
python -m pip install -r requirements-test.txt

- name: Test with pytest
run: pytest
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,9 @@ jobs:
tags: ${{ env.TEST_TAG }}

- name: Run image against tests
run: docker run ${{ env.TEST_TAG }} python /usr/src/gamedaybot/setup.py test
run: |
docker run ${{ env.TEST_TAG }} /bin/sh -c "\
python -m pip install --upgrade pip && \
pip install -r requirements.txt && \
pip install -r requirements-test.txt && \
pytest"
2 changes: 1 addition & 1 deletion gamedaybot/espn/env_vars.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
import gamedaybot.espn.functionality as espn
import gamedaybot.utils as utils
import gamedaybot.utils.util as utils


def get_env_vars():
Expand Down
212 changes: 168 additions & 44 deletions gamedaybot/espn/espn_bot.py
Original file line number Diff line number Diff line change
@@ -1,58 +1,166 @@
import sys
import os
sys.path.insert(1, os.path.abspath('.'))
import json
from gamedaybot.espn.env_vars import get_env_vars
import gamedaybot.espn.functionality as espn
import gamedaybot.utils as utils
from gamedaybot.chat.groupme import GroupMe
from gamedaybot.chat.slack import Slack
from gamedaybot.chat.discord import Discord
if os.environ.get("AWS_EXECUTION_ENV") is not None:
# For use in lambda function
import utils.util as util
from chat.groupme import GroupMe
from chat.slack import Slack
from chat.discord import Discord
else:
# For local use
import sys
sys.path.insert(1, os.path.abspath('.'))
import gamedaybot.utils.util as util
from gamedaybot.chat.groupme import GroupMe
from gamedaybot.chat.slack import Slack
from gamedaybot.chat.discord import Discord
from gamedaybot.espn.env_vars import get_env_vars
import gamedaybot.espn.functionality as espn
import gamedaybot.espn.season_recap as recap


from espn_api.football import League
import json
import logging

logger = logging.getLogger(__name__)
# logger.setLevel(logging.INFO)
logger.setLevel(logging.DEBUG)


def espn_bot(function):
"""
This function is used to send messages to a messaging platform (e.g. Slack, Discord, or GroupMe) with information
about a fantasy football league.

Parameters
----------
function: str
A string that specifies which type of information to send (e.g. "get_matchups", "get_power_rankings").

Returns
-------
None

Notes
-----
The function uses the following information from the data dictionary:

str_limit: the character limit for messages on slack.
bot_id: the id of the GroupMe bot.
If not provided, defaults to 1.
slack_webhook_url: the webhook url for the slack bot.
If not provided, defaults to 1.
discord_webhook_url: the webhook url for the discord bot.
If not provided, defaults to 1.
league_id: the id of the fantasy football league.
year: the year of the league.
If not provided, defaults to current year.
swid: the swid of the league.
If not provided, defaults to '{1}'.
espn_s2: the espn s2 of the league.
If not provided, defaults to '1'.
top_half_scoring: a boolean that indicates whether to include only the top half of the league in the standings.
If not provided, defaults to False.
random_phrase: a boolean that indicates whether to include a random phrase in the message.
If not provided, defaults to False.

The function creates GroupMe, Slack, and Discord objects, and a League object using the provided information.
It then uses the specified function to generate a message and sends it through the appropriate messaging platform.

Possible function values:

get_matchups: sends the current week's matchups and the projected scores for the remaining games.
get_monitor: sends a message with a summary of the current week's scores.
get_scoreboard_short: sends a short version of the current week's scores.
get_projected_scoreboard: sends the projected scores for the remaining games.
get_close_scores: sends a message with the scores of games that have a difference of less than 7 points.
get_power_rankings: sends a message with the power rankings for the league.
get_trophies: sends a message with the trophies for the league.
get_standings: sends a message with the standings for the league.
get_final: sends the final scores and trophies for the previous week.
get_waiver_report: sends a message with the waiver report for the league.
init: sends a message to confirm that the bot has been set up.
"""

data = get_env_vars()
bot = GroupMe(data['bot_id'])
slack_bot = Slack(data['slack_webhook_url'])
discord_bot = Discord(data['discord_webhook_url'])
swid = data['swid']
espn_s2 = data['espn_s2']
str_limit = data['str_limit'] # slack char limit

try:
bot_id = data['bot_id']
except KeyError:
bot_id = 1

try:
slack_webhook_url = data['slack_webhook_url']
except KeyError:
slack_webhook_url = 1

try:
discord_webhook_url = data['discord_webhook_url']
except KeyError:
discord_webhook_url = 1

if (len(str(bot_id)) <= 1 and
len(str(slack_webhook_url)) <= 1 and
len(str(discord_webhook_url)) <= 1):
# Ensure that there's info for at least one messaging platform,
# use length of str in case of blank but non null env variable
raise Exception("No messaging platform info provided. Be sure one of BOT_ID, SLACK_WEBHOOK_URL, or DISCORD_WEBHOOK_URL env variables are set")

league_id = data['league_id']
year = data['year']
random_phrase = data['random_phrase']
test = data['test']
top_half_scoring = data['top_half_scoring']
waiver_report = data['waiver_report']

try:
year = int(data['year'])
except KeyError:
year = 2023

try:
swid = data['swid']
except KeyError:
swid = '{1}'

if swid.find("{", 0) == -1:
swid = "{" + swid
if swid.find("}", -1) == -1:
swid = swid + "}"

try:
espn_s2 = data['espn_s2']
except KeyError:
espn_s2 = '1'

try:
top_half_scoring = util.str_to_bool(data['top_half_scoring'])
except KeyError:
top_half_scoring = False

try:
random_phrase = util.str_to_bool(data['random_phrase'])
except KeyError:
random_phrase = False

groupme_bot = GroupMe(bot_id)
slack_bot = Slack(slack_webhook_url)
discord_bot = Discord(discord_webhook_url)

if swid == '{1}' or espn_s2 == '1':
league = League(league_id=league_id, year=year)
else:
league = League(league_id=league_id, year=year, espn_s2=espn_s2, swid=swid)

if league.scoringPeriodId > len(league.settings.matchup_periods) and not test:
print("Not in active season")
return
try:
broadcast_message = data['broadcast_message']
except KeyError:
broadcast_message = None

faab = league.settings.faab

if test:
print(espn.get_scoreboard_short(league))
print(espn.get_projected_scoreboard(league))
print(espn.get_standings(league))
print(espn.get_standings(league, True))
print(espn.get_close_scores(league))
print(espn.get_monitor(league))
print(espn.get_matchups(league))
print(espn.get_power_rankings(league))
print(espn.get_trophies(league))
print(espn.optimal_team_scores(league, full_report=True))
if waiver_report and swid != '{1}' and espn_s2 != '1':
print(espn.get_waiver_report(league, faab))
# bot.send_message("Testing")
# slack_bot.send_message("Testing")
# discord_bot.send_message("Testing")
# always let init and broadcast run
if function not in ["init", "broadcast"] and league.scoringPeriodId > len(league.settings.matchup_periods):
logger.info("Not in active season")
return

text = ''
logger.info("Function: " + function)

if function == "get_matchups":
text = espn.get_matchups(league, random_phrase)
text = text + "\n\n" + espn.get_projected_scoreboard(league)
Expand All @@ -71,6 +179,13 @@ def espn_bot(function):
text = espn.get_trophies(league)
elif function == "get_standings":
text = espn.get_standings(league, top_half_scoring)
elif function == "win_matrix":
text = recap.win_matrix(league)
elif function == "trophy_recap":
text = recap.trophy_recap(league)
# groupme_bot.send_message(text, file_path='/tmp/season_recap.png')
# slack_bot.send_message(text, file_path='/tmp/season_recap.png')
# discord_bot.send_message(text, file_path='/tmp/season_recap.png')
elif function == "get_final":
# on Tuesday we need to get the scores of last week
week = league.current_week - 1
Expand All @@ -79,24 +194,33 @@ def espn_bot(function):
elif function == "get_waiver_report" and swid != '{1}' and espn_s2 != '1':
faab = league.settings.faab
text = espn.get_waiver_report(league, faab)
elif function == "broadcast":
try:
text = broadcast_message
except KeyError:
# do nothing here, empty broadcast message
pass
elif function == "init":
try:
text = data["init_msg"]
except KeyError:
# do nothing here, empty init message
pass
else:
text = "Something happened. HALP"
text = "Something bad happened. HALP"

if text != '' and not test:
messages=utils.str_limit_check(text, data['str_limit'])
logger.debug(data)
if text != '':
logger.debug(text)
messages = util.str_limit_check(text, str_limit)
for message in messages:
bot.send_message(message)
groupme_bot.send_message(message)
slack_bot.send_message(message)
discord_bot.send_message(message)


if __name__ == '__main__':
from gamedaybot.espn.scheduler import scheduler

espn_bot("init")
scheduler()
Loading
Loading