amosbastian / understat

An asynchronous Python package for https://understat.com/.
MIT License
150 stars 30 forks source link

Only last week #8

Closed FWeissenb closed 4 years ago

FWeissenb commented 4 years ago

I use

understat.get_league_results("Bundesliga",2019,) 

to get the results. How can I set options to only get the previous week?

amosbastian commented 4 years ago

You'd have to filter the results list by certain dates yourself. Something like this should do the trick:

import asyncio
import datetime
import json

import aiohttp
from dateutil.parser import parse

from understat import Understat

def previous_week_range(date):
    start_date = date + datetime.timedelta(-date.weekday(), weeks=-1)
    end_date = date + datetime.timedelta(-date.weekday() - 1)
    return start_date, end_date

async def main():
    date_one, date_two = previous_week_range(datetime.date.today())

    async with aiohttp.ClientSession() as session:
        understat = Understat(session)
        results = await understat.get_league_results("Bundesliga", 2019)

    filtered_results = [result for result in results
                        if parse(result["datetime"]).date() >= date_one and
                        parse(result["datetime"]).date() <= date_two]

    print(json.dumps(filtered_results, indent=4))

if __name__ == "__main__":
    asyncio.run(main())
FWeissenb commented 4 years ago

But this only works if the last matchday was a week before 😄 So if we have a break for national games (like atm) it doesn't work anymore 😢

If I look at understat they also filter there data and only show "current matchday" and some buttons to switch prev and next. How do they do this? Is there any list "this matchday contains the following games: id1, id2, id3.." so that I can get this list and every single game?

amosbastian commented 4 years ago

I'm not sure how Understat does this specifically but they probably do something like this:

import asyncio
import datetime
import json

import aiohttp
from dateutil.parser import parse

from understat import Understat

def get_latest_week_results(results, week):
    latest_results = [
        result for result in results
        if parse(result["datetime"]).date().isocalendar()[1] == week]

    if not latest_results:
        return get_latest_week_results(results, week - 1)

    return latest_results

async def main():
    async with aiohttp.ClientSession() as session:
        understat = Understat(session)
        results = await understat.get_league_results("Bundesliga", 2019)

    current_week = datetime.datetime.now().isocalendar()[1]
    print(json.dumps(get_latest_week_results(results, current_week), indent=4))

if __name__ == "__main__":
    asyncio.run(main())

I mean, it's basically the same thing as above, but it just uses the week number instead. It always gets the latest results, which is what I think you want. To get the next fixtures they'd probably have a similar function to get_latest_week_results, but the other way around (and using fixtures instead of results), which executes when someone clicks the buttons.