From 189313c28b673095293b21c316470da46b6f292a Mon Sep 17 00:00:00 2001 From: Rodrigo Braz Date: Wed, 17 Apr 2024 21:04:34 +0100 Subject: [PATCH] chore(justwatch): Leverage trakt to gather justwatch providers --- app/deleterr.py | 15 +++-- app/scripts/justwatch_providers.py | 88 +++++++++++++++++++++--------- 2 files changed, 73 insertions(+), 30 deletions(-) diff --git a/app/deleterr.py b/app/deleterr.py index bbc0700..4ebbd84 100644 --- a/app/deleterr.py +++ b/app/deleterr.py @@ -105,15 +105,22 @@ def main(): args, unknown = parser.parse_known_args() + config = load_config(args.config) + config.validate() + # If providers flag is set, gather JustWatch providers and exit if args.jw_providers: from app.scripts.justwatch_providers import gather_providers - gather_providers() - return + providers = gather_providers( + config.settings.get("trakt", {}).get("client_id"), + config.settings.get("trakt", {}).get("client_secret"), + ) - config = load_config(args.config) - config.validate() + print(providers) + logger.info("# of Trakt Providers: " + str(len(providers))) + + return Deleterr(config) diff --git a/app/scripts/justwatch_providers.py b/app/scripts/justwatch_providers.py index 155f310..6a914e6 100644 --- a/app/scripts/justwatch_providers.py +++ b/app/scripts/justwatch_providers.py @@ -1,34 +1,70 @@ from app.modules.justwatch import JustWatch +from app.modules.trakt import Trakt -SHOWS = [ - ("Loki", 2021), - ("Stranger Things", 2016), - ("Reacher", 2022), - ("Logan", 2017), - ("Severance", 2022), - ("Cobra Kai", 2018), - ("Bo Burnham: What", 2013), - ("Interstellar", 2014), - ("Eraserhead", 1977), - ("Life on Earth", 1979), - ("24", 2001), - ("Current Sea", 2020), -] - - -def gather_providers(): - # Create a JustWatch instance - justwatch = JustWatch("US", "en") + +def gather_providers(trakt_id, trakt_secret): + # Create a Trakt instance + trakt = Trakt( + trakt_id, + trakt_secret, + ) + + # Get the most popular shows + shows = trakt.get_all_items_for_url( + "show", + { + "max_items_per_list": 200, + "lists": [ + "https://trakt.tv/shows/trending", + "https://trakt.tv/shows/popular", + "https://trakt.tv/shows/watched/yearly", + "https://trakt.tv/shows/collected/yearly", + ], + }, + ) + + # List of country codes to check providers for + countries = [ + "US", + "BR", + "NG", + "IN", + "CN", + "RU", + "AU", + "PT", + "FR", + "DE", + "ES", + "IT", + "JP", + "KR", + "GB", + ] # Create a set to store the providers providers = set() - # Iterate shows and collect all the different providers in a set - for title, year in SHOWS: - result = justwatch.search_by_title_and_year(title, year, "show") - if result: - for offer in result.offers: - providers.add(offer.package.technical_name) + # Iterate over the countries + for country in countries: + # Create a JustWatch instance for the current country + justwatch = JustWatch(country, "en") + + # Iterate shows and collect all the different providers in a set + for show in shows: + try: + title = shows[show]["trakt"].title + year = shows[show]["trakt"].year + result = justwatch.search_by_title_and_year(title, year, "show") + if result: + for offer in result.offers: + providers.add(offer.package.technical_name) + except AttributeError: + # Skip if the show doesn't have a title or year + continue + except TypeError: + # There is a null error inside justwatch library, this is a workaround + continue # Print the providers - print(providers) + return providers