vasqued2 / ha-teamtracker

Home Assistant integration that provides real-time scores in multiple professional (NBA, NFL, NHL, MLB, MLS, and more), college (NCAA), and international (soccer, golf, tennis, mma, racing) sports using ESPN APIs.
168 stars 20 forks source link

Amazing! Couple Questions #1

Closed esmoyer closed 2 years ago

esmoyer commented 2 years ago

I just stumbled across this while looking to fully replace my MagicMirror with HA and it's awesome. Really appreciate your work on this. Just a couple questions:

  1. Have you noticed any API rate limiting with ESPN? I thought I remember reading about that somewhere when dealing with the MagicMirror module but I also may be crazy.
  2. I was able to get a couple NFL teams setup and working but can't seem to get any NCAAF teams to work. Is there somewhere I can view debug logging to try to figure that out? The HA log doesn't seem to show much.

Otherwise, works and looks awesome!

vasqued2 commented 2 years ago

I've been iterating pretty heavily over the last week and didn't realize anyone had found it yet and was trying it. It's possible you got a version that was a little kludgy. You may have to uninstall and reinstall because I broke backward compatibility for a little bit early this week. I'll try to be more careful going forward. I'm still trying to find my way around github and how it iteracts w/ HACS, etc. First, make sure you are on the latest version. NCAAF is working for me now. If you are still having problems, do the following and I'll try to recreate it.

You can turn on the debugging output by adding the following to your configuration.yaml file. Then search for teamtracker in the full logs. logger: default: info logs: custom_components.teamtracker: debug

At a minimum, you should see the API and team info it is using. You can paste the API into a browser to get the JSON response to make sure your team is included in the JSON response. Even if it's not though, it should still create your sensor.

As for throttling the API calls, I haven't see any. I've been testing w/ 5+ different games/sports going on at the same time all doing updates every 5 seconds and haven't had any problem.

Thanks for the kind words and let me know if you still are having problems and I'll see what I can do.

esmoyer commented 2 years ago

No worries, sorry I jumped the gun! I have been playing around with ESPN API URLs the last couple days on Postman and in the browser but you're right, my team (NEB) isn't showing in the API for some reason. Looks like ESPN only returns 22 results with the call, which is odd. I'll keep exploring to try and see how to get it to return all events.

esmoyer commented 2 years ago

Ok, I think I maybe figured it out. I looked at the current MagicMirror module I use (https://github.com/jclarke0000/MMM-MyScoreboard/blob/master/providers/ESPN.js) and looked at the URL they use.

    var url = "http://site.api.espn.com/apis/site/v2/sports/" +
      this.getLeaguePath(league) +
      "/scoreboard?dates=" +
      moment(gameDate).format("YYYYMMDD") + "&limit=200";

The API URL they have adds the "dates" parameter. Once I add that to the API URL, today's game shows up in the browser JSON!

Not sure if looking at their code will give you any help, but they pull in ESPN as well as some EU and other sports.

Also, really like the ability to do custom sports on this, I am going to try it with a some college VB and see how it goes.

I'm not super great at coding or Github, just enough to fumble around and mostly piece things together, but I am happy to test or troubleshoot!

vasqued2 commented 2 years ago

The API I am using for college football is: http://site.api.espn.com/apis/site/v2/sports/football/college-football/scoreboard

It's working right now for me for the MICH/CSU game. What team are you looking for? I'll try it and see if I can figure out what is happening.

Volleyball doesn't work yet. I actually tried it last night as I was testing custom APIs. It was missing some fields but I can't remember which ones. I will also have to do something to handle the concepts of games and matches.

The API you will want to use is: https://site.api.espn.com/apis/site/v2/sports/volleyball/womens-college-volleyball/scoreboard

esmoyer commented 2 years ago

Nice! The URL I used to find the Nebraska (NEB) game is https://site.api.espn.com/apis/site/v2/sports/football/college-football/scoreboard?dates=20220903 as it adds the "dates" parameter. Otherwise, without it, it just pulls in 22 games which didn't include NEB. Maybe once the game starts at 2:30pm it will show.

I got the VB one figured out, just need to wait for the next match to see how it goes, but agree, will probably be a little inconsistent. Just exciting to test out!

vasqued2 commented 2 years ago

I knew the API was only pulling the larger games (ranked teams maybe?) but didn't realize that you could get more by adding the date query.

Volleyball was erroring out because it wasn't returning team_colors and opponent_colors. If you go into init.py and set default colors if none were found when the game is IN progress, it worked. The score seems to show the number of games/sets won. Haven't found the points for the individual games yet.

I captured the json and will look at it later. Let me know if you find anything.

esmoyer commented 2 years ago

Doing some further reading I found:

by default, ESPN returns only the Top 25 ranked teams for NCAAF and NCAAM. By appending the group parameter (80 for NCAAF and 50 for NCAAM, found in the URL of their respective scoreboard pages on ESPN.com) we'll get the entire game list.

So to fix it for me for now, I just appended ?groups=80 to the URL_TAIL line in const.py so it's URL_TAIL = "/scoreboard?groups=80". Restarted HA and boom, the Nebraska game info came up! Unfortunately, that breaks the other sports so will probably need some sort of conditional; but at least progress!

esmoyer commented 2 years ago

Alright, last update for now, not sure if it was the right approach or if I even did it efficiently but it works! Added conditional to __init__.py:

    if league_id == "NCAAF":
        url = url + "?groups=80";
    elif league_id == "NCAAM":
        url = url + "?groups=50";
    else:
        url = URL_HEAD + sport_path + "/" + league_path + URL_TAIL

image

vasqued2 commented 2 years ago

Nice! I'll probably do something to allow it to be user configurable. Looks like it's needed for March Madness too so there could be others out there too.

vasqued2 commented 2 years ago

Created a branch for these changes and will merge them w/ the master later this week after I see them running for a bit. Got volleyball support working. It shows sets up in the score and then the points for the current match down where the Win Probability/Shots on Goal display.

There was one volleyball event that didn't have a shortName that caused some issues. It also looks like there can be multiple matches in a single day. At some point it should pick one based on the state of the event.

vasqued2 commented 2 years ago

"?groups=80" does not appear to return all of the games, at least this morning. It is notably missing games when both opponents are ranked (OSU/ND, CIN/ARK, etc.). I don't know if they were included yesterday or not.

You can definitely use groups to pull back all of the games in a particular conference (i.e. Big Ten is 5) and 80 pulls back a lot of games w/ non-ranked opponents but I'm still looking for a pattern. I'll also go back and look at that the date parm is pulling back in more detail.

esmoyer commented 2 years ago

Interesting. Looking at the MM module I think it can pull based on conference too which would explain it. I'm wondering if you'll need to use a combo of the"dates" and "groups" parameters to get them all or if there are others that could be used instead.

I can try out the test branch and check things out if you want or if it'll help.

vasqued2 commented 2 years ago

Thanks. I'll let you know when it's ready. Hopefully tomorrow.

"groups" is actually a way to specify conferences and other "groupings of teams". 1 is ACC teams, 5 is Big Ten, etc. 80 appears to be a subset of unranked games across conferences but I don't know how it is determined or if it is consistent from week to week. I think if you use group 5, it will find NEB whether they are ranked or not.

If you go here, you can select the different conferences from the drop down on the right and see each of their group numbers in the url.

I'm leaning towards just letting you specify a group number when the sensor is initially set up based on what conference the team is in.

vasqued2 commented 2 years ago

Test branch should be good to try.

Three main changes:

There is no way to add the Conference ID to an existing sensor, so you will have to delete the old ones and create new ones if you want to specify one.

I haven't had a lot of games to test with so basically have been testing w/ a couple old json files I saved. Not sure how robust it is.

Let me know if you run into any problems. Thanks for your help. The groups parm really helped a lot.

esmoyer commented 2 years ago

Nice! Football seems to be working; it was previously showing the "post" of their Week 0 game rather than last Saturday's game; now showing correctly. I will check out the "pre" and "live" FB on Saturday and VB during their next match this Wednesday to see how that goes. Thanks for the great work!

vasqued2 commented 2 years ago

Found an issue where the sensor errored out once the first game in the match started but no one had scored a point yet. Just pushed a fix to the branch. In the prior version, the sensor will disappear until the first points are scored.

esmoyer commented 2 years ago

Cool. I have pulled your changes and will see how it goes for tomorrows game. So far NCAAFB has been working well pulling in the "pre" info into the sensor; where before it would say "No Game"

esmoyer commented 2 years ago

So the NEB VB game is going but it's not showing any scores or anything. I'm on my phone but looking at the API, it doesn't appear updated for some reason, but hard to tell for sure on mobile.

I also did notice it uses the term "kick off" which isn't a big deal, just funny.

vasqued2 commented 2 years ago

I noticed NEB isn't getting updates in the API for some reason. The ESPN page isn't showing score updates either. MIZ, BC, and TEX games all are pulling in the live data.

VB should say First Serve. If it isn't, you will either need to clear your browser cache and refresh or update the teamtracker-card. If you are using the mobile app, I noticed that it tends to take a while for the new card to show up. Not sure how to clear the cache for it.

esmoyer commented 2 years ago

Just got home and noticed that. Still in "scheduled" state. I just realized I didn't pull the test branch of the card so the "kick off" issue is on me!

image

esmoyer commented 2 years ago

Seems like things are working well. I will close this for now. Thanks for the great work!