Skip to content

Commit

Permalink
Support some IMDb TV show links, rename /imdb to /movie
Browse files Browse the repository at this point in the history
  • Loading branch information
stekc committed Oct 10, 2024
1 parent 7deb88e commit 2161758
Showing 1 changed file with 74 additions and 27 deletions.
101 changes: 74 additions & 27 deletions cogs/movies.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,17 @@ async def callback(self, interaction: discord.Interaction):
view.add_item(TrailerButton(trailer))
if recommended:
view.add_item(RecommendedButton(recommended_embed))

stremio_url = f"https://keto.boats/stremio?id={movie['ImdbId']}"
if movie.get("IsTVSeries", False):
stremio_url += "&series=true"

view.add_item(
discord.ui.Button(
style=discord.ButtonStyle.link,
label="Open in Stremio",
emoji="<:stremio:1292976659829362813>",
url=f"https://keto.boats/stremio?id={movie['ImdbId']}",
url=stremio_url,
)
)

Expand All @@ -127,6 +132,52 @@ async def callback(self, interaction: discord.Interaction):
else:
return embed, view

async def get_movie_data(self, query_or_id):
async with aiohttp.ClientSession() as session:
try:
async with session.get(
f"https://api.radarr.video/v1/movie/imdb/{query_or_id}", timeout=5
) as response:
movie_data = await response.json()

if movie_data and isinstance(movie_data, list) and len(movie_data) > 0:
return movie_data[0]

if not movie_data:
try:
async with session.get(
f"https://imdb.mainframe.stkc.win/meta/{quote_plus(query_or_id)}",
timeout=5,
) as response:
if response.status == 200:
fallback_data = await response.json()
else:
return None

if fallback_data:
is_tv_series = (
fallback_data.get("titleType") == "TV_SERIES"
or fallback_data.get("titleType") == "TV_MINI_SERIES"
)
return {
"Title": fallback_data.get(
"primaryTitle", "Unknown Title"
),
"Year": str(fallback_data.get("startYear", "")),
"Genres": fallback_data.get("genres", []),
"Overview": "No overview available",
"ImdbId": fallback_data.get("id"),
"Runtime": fallback_data.get("runtime"),
"IsTVSeries": is_tv_series,
}
except (aiohttp.ContentTypeError, asyncio.TimeoutError):
return None

except asyncio.TimeoutError:
return None

return None

@commands.Cog.listener()
async def on_message(self, message: discord.Message):
if not message.guild:
Expand All @@ -139,45 +190,41 @@ async def on_message(self, message: discord.Message):
return
if match := self.pattern.search(message.content.strip("<>")):
link = match.group(1)
async with aiohttp.ClientSession() as session:
async with session.get(
f"https://api.radarr.video/v1/movie/imdb/{link}"
) as response:
movie_data = await response.json()

if (
not movie_data
or not isinstance(movie_data, list)
or len(movie_data) == 0
):
return
movie = await self.get_movie_data(link)

movie = movie_data[0]
embed, view = await self.process_movie_data(movie, is_imdb_link=True)

await message.reply(embed=embed, view=view)
await self.config_cog.increment_link_fix_count("imdb")
await asyncio.sleep(0.75)
await message.edit(suppress=True)
if movie:
embed, view = await self.process_movie_data(movie, is_imdb_link=True)
await message.reply(embed=embed, view=view)
await self.config_cog.increment_link_fix_count("imdb")
await asyncio.sleep(0.75)
await message.edit(suppress=True)

@commands.hybrid_command(
name="imdb",
name="movie",
description="Search for a movie on IMDb.",
)
@app_commands.describe(query="Title of the movie you want to search for.")
@app_commands.allowed_installs(guilds=True, users=True)
@app_commands.allowed_contexts(guilds=True, dms=True, private_channels=True)
async def imdb(self, context: Context, *, query: str):
async with aiohttp.ClientSession() as session:
async with session.get(
f"https://api.radarr.video/v1/search?q={quote_plus(query)}&year="
) as response:
movie_data = await response.json()
try:
async with session.get(
f"https://api.radarr.video/v1/search?q={quote_plus(query)}&year=",
timeout=5,
) as response:
search_results = await response.json()
except asyncio.TimeoutError:
return await context.send("Search timed out. Please try again later.")

if not search_results:
return await context.send("No results found.")

movie = await self.get_movie_data(search_results[0]["ImdbId"])

if not movie_data or not isinstance(movie_data, list) or len(movie_data) == 0:
if not movie:
return await context.send("No results found.")

movie = movie_data[0]
await self.process_movie_data(movie, context, is_imdb_link=False)


Expand Down

0 comments on commit 2161758

Please sign in to comment.