Git Product home page Git Product logo

Comments (3)

amosbastian avatar amosbastian commented on May 28, 2024 1

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.

from understat.

amosbastian avatar amosbastian commented on May 28, 2024

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())

from understat.

FWeissenb avatar FWeissenb commented on May 28, 2024

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?

from understat.

Related Issues (17)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.