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

Trying to print all games in current month #86

Open
thepulpit opened this issue Apr 5, 2018 · 4 comments
Open

Trying to print all games in current month #86

thepulpit opened this issue Apr 5, 2018 · 4 comments

Comments

@thepulpit
Copy link

thepulpit commented Apr 5, 2018

Hi, I am trying to do something similar to what is on the documentation with printing games, but I have used datetime to get the current month and year and use that to build the list of upcoming and recent games. However, whenever I try to print anything in the future or current timeframe, the line that calls mlbgame.games() does not ever complete running.

Here is what I have:

from __future__ import print_function
import mlbgame
import datetime
year = datetime.date.today().year
month = datetime.date.today().month
new_month = mlbgame.games(year,month,home='Red Sox', away='Red Sox')
games = mlbgame.combine_games(new_month)
for game in games:
print(game)

@trevor-viljoen
Copy link
Contributor

In [2]: from __future__ import print_function
   ...: import mlbgame
   ...: import datetime
   ...: year = datetime.date.today().year
   ...: month = datetime.date.today().month
   ...: new_month = mlbgame.games(year,month,home='Red Sox', away='Red Sox')
   ...: print(new_month)
   ...: games = mlbgame.combine_games(new_month)
   ...: for game in games:
   ...:     print(game)
   ...:
Red Sox (2) at Rays (1)
Red Sox (7) at Marlins (3)
Red Sox (4) at Marlins (2)
Rays (2) at Red Sox (3)
Rays (0) at Red Sox (0)
Rays (0) at Red Sox (0)
Yankees (0) at Red Sox (0)
Yankees (0) at Red Sox (0)
Yankees (0) at Red Sox (0)
Orioles (0) at Red Sox (0)
Orioles (0) at Red Sox (0)
Orioles (0) at Red Sox (0)
Orioles (0) at Red Sox (0)
Red Sox (0) at Angels (0)
Red Sox (0) at Angels (0)
Red Sox (0) at Angels (0)
Red Sox (0) at Athletics (0)
Red Sox (0) at Athletics (0)
Red Sox (0) at Athletics (0)
Red Sox (0) at Blue Jays (0)
Red Sox (0) at Blue Jays (0)
Red Sox (0) at Blue Jays (0)
Rays (0) at Red Sox (0)
Rays (0) at Red Sox (0)
Rays (0) at Red Sox (0)
Royals (0) at Red Sox (0)

It takes a while, but it looks like it runs. Part of the slowness is likely due to sequential downloads. I have some ideas to drastically speed that up, but I have some other PRs to finish before I could get to that.

@PeterKaminski09
Copy link

@trevor-viljoen just from looking at this in the browser, the actual gd2.mlb.com API is taking a long time for 2018 games. Is there a more efficient way to query the API for a range of dates rather than day by day.

I'm happy to take a look at this/help contribute

@trevor-viljoen
Copy link
Contributor

trevor-viljoen commented Apr 28, 2018

@PeterKaminski09 I haven't had time to dig into #90. I'd like to implement the requests library to use sessions and with that, https://github.com/reclosedev/requests-cache. This would help in reducing the bandwidth required, particularly for completed games. Additionally, this would help with an issue that was brought up by the MLB-LED-Scoreboard project. They sometimes experience an error related to open connections.

urllib2.URLError: <urlopen error [Errno -3] Temporary failure in name resolution>

In your case some local caching using something like Redis might be useful. I recently had to do something similar for another project I'm working on. The work flow is as follows:

  1. Check for existence of the (ID) key in redis.
  2. If the key is not present in the redis database, query the API and cache the pickled python object in redis.
  3. If the key is present, don't query the API. Retrieve the key from redis and unpickle the python object.

As for a more efficient way to query for a range of dates, that depends on what data you actually need.

@panzarino
Copy link
Owner

@trevor-viljoen I used to have a method for caching XML files but as the library expanded I found it to be too hard to maintain. However, if you can come up with a better method for caching then I would be glad to accept a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants