moogar0880 / PyTrakt

A Pythonic interface to the Trakt.tv REST API
Other
66 stars 51 forks source link

[python-pytrakt] TVShow: Update seasons to fill episodes in single request #176

Open glensc opened 2 years ago

glensc commented 2 years ago

This will make only one trakt.tv API request to create list of TVSeason with TVEpisode objects.

The properties are indexed in a dict by .number rather being a list. (moved to https://github.com/moogar0880/PyTrakt/pull/185)

    show = TVShow('Game of Thrones')

    episodes = show.episodes
    assert len(episodes) == 9
    assert episodes[1].episodes[0].title == 'Winter Is Coming'

this will make only two requests:

2022-01-12 21:33:17,816 DEBUG[trakt.core]:RESPONSE [get] (https://api-v2launch.trakt.tv/shows/game-of-thrones?extended=full): request: GET https://api-v2launch.trakt.tv/shows/game-of-thrones?extended=full, response: 200 (1.21 KiB), created: 2022-01-12 19:03:22 EET, expires: N/A (fresh)
2022-01-12 21:33:17,827 DEBUG[trakt.core]:RESPONSE [get] (https://api-v2launch.trakt.tv/shows/game-of-thrones/seasons?extended=episodes): request: GET https://api-v2launch.trakt.tv/shows/game-of-thrones/seasons?extended=episodes, response: 200 (45.46 KiB), created: 2022-01-12 20:55:16 EET, expires: N/A (fresh)

This is a continued effort to replace internal implementation:

glensc commented 2 years ago

Perhaps instead of adding episodes property, replace the implementation with seasons property?

but then it would be a breaking change due list vs dict.

glensc commented 2 years ago

Note that the current data is all fetched and filled by TVShow only (no extra requests), not in TVSeason, TVEpisode individually, so the old code needs to remain for the TVSeason or TVEpisode direct creation to continue working.

glensc commented 2 years ago

or rename new property to season_episodes?

glensc commented 2 years ago

perhaps the list vs dict is not an issue since accessing season from list by index is unreliable anyway (season 0 aka extras may be present and may be missing). however the same does not apply for episodes, in list episode 1 is episodes[0] not episodes[1].

glensc commented 2 years ago

I've given this a thought, perhaps:

  1. change the types to be list
  2. replace the .episodes added here as .seasons
  3. in v3 change type to be dict

I've also started abstracting HTTP and API layer:

so, if someone wants to make raw queries without the abstraction of the object they can use just the API client class. reasons could be any of:

glensc commented 2 years ago

Ok. I've removed changes making this incompatible for v3

glensc commented 2 years ago

Incompatible changes moved to separate PR for v4:

glensc commented 2 years ago

Carried to my fork: