Skip to content

Commit

Permalink
chore(justwatch): Leverage trakt to gather justwatch providers
Browse files Browse the repository at this point in the history
  • Loading branch information
rfsbraz committed Apr 17, 2024
1 parent 7a6a9c5 commit 189313c
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 30 deletions.
15 changes: 11 additions & 4 deletions app/deleterr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
88 changes: 62 additions & 26 deletions app/scripts/justwatch_providers.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 189313c

Please sign in to comment.