Skip to content

Commit

Permalink
Merge pull request #164 from danielx/feat/added-notification
Browse files Browse the repository at this point in the history
feat: support for "Added" notification
  • Loading branch information
danielx authored Oct 20, 2024
2 parents 8c9dc9f + 7a236df commit 0580fcd
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 16 deletions.
7 changes: 4 additions & 3 deletions app/api_v1/endpoints/radarr.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
router = APIRouter()

MESSAGE = string.Template(
"""Now available: *$title* ($year)
"""$event: *$title* ($year)
https://www.themoviedb.org/movie/$id"""
)

Expand All @@ -22,14 +22,15 @@ async def radarr(event: schemas.RadarrEvent):
"""Incoming webhook for radarr events."""
logging.info(repr(event))

if event.eventType != "Download":
if event.eventType not in ["Download", "MovieAdded"]:
return Response(status_code=status.HTTP_204_NO_CONTENT)

await libs.gchat.send_message(
MESSAGE.substitute(
event="Now available" if event.eventType == "Download" else "Added",
title=event.movie.title.strip(),
year=event.movie.year,
id=event.remoteMovie.tmdbId,
id=event.movie.tmdbId,
)
)

Expand Down
5 changes: 3 additions & 2 deletions app/api_v1/endpoints/sonarr.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
router = APIRouter()

MESSAGE = string.Template(
"""Now available: *$title*
"""$event: *$title*
$items
https://www.thetvdb.com/dereferrer/series/$id"""
)
Expand All @@ -25,7 +25,7 @@ async def sonarr_webhook(event: schemas.SonarrEvent):
"""Incoming webhook for sonarr events."""
logging.info(repr(event))

if event.eventType != "Download":
if event.eventType not in ["Download", "SeriesAdd"]:
return Response(status_code=status.HTTP_204_NO_CONTENT)

items = []
Expand All @@ -39,6 +39,7 @@ async def sonarr_webhook(event: schemas.SonarrEvent):

await libs.gchat.send_message(
MESSAGE.substitute(
event="Now available" if event.eventType == "Download" else "Added",
title=event.series.title.strip(),
items="\n".join(items),
id=event.series.tvdbId,
Expand Down
6 changes: 1 addition & 5 deletions app/schemas/_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,13 @@
class Movie(BaseModel):
title: str
year: int


class RemoteMovie(BaseModel):
tmdbId: int


class RadarrEvent(BaseModel):
eventType: str

movie: Movie
remoteMovie: RemoteMovie


class Series(BaseModel):
Expand All @@ -32,4 +28,4 @@ class SonarrEvent(BaseModel):
eventType: str

series: Series
episodes: list[Episode]
episodes: list[Episode] = []
20 changes: 15 additions & 5 deletions tests/test_radarr.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ class Payload(NamedTuple):
)
json: dict | None = {
"eventType": "Grab",
"movie": {"title": "string", "year": 0},
"remoteMovie": {"tmdbId": 0},
"movie": {"title": "string", "year": 0, "tmdbId": 0},
}


Expand All @@ -44,12 +43,23 @@ class Result(NamedTuple):
notification_sent=False,
),
),
( # only notify on eventType=Download
( # notify on eventType=Download
Payload(
json={
"eventType": "Download",
"movie": {"title": "string", "year": 0},
"remoteMovie": {"tmdbId": 0},
"movie": {"title": "string", "year": 0, "tmdbId": 0},
}
),
Result(
status_code=204,
notification_sent=True,
),
),
( # notify on eventType=MovieAdded
Payload(
json={
"eventType": "MovieAdded",
"movie": {"title": "string", "year": 0, "tmdbId": 0},
}
),
Result(
Expand Down
14 changes: 13 additions & 1 deletion tests/test_sonarr.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class Result(NamedTuple):
notification_sent=False,
),
),
( # only notify on eventType=Download
( # notify on eventType=Download
Payload(
json={
"eventType": "Download",
Expand All @@ -63,6 +63,18 @@ class Result(NamedTuple):
notification_sent=True,
),
),
( # notify on eventType=SeriesAdd
Payload(
json={
"eventType": "SeriesAdd",
"series": {"title": "string", "tvdbId": 0},
}
),
Result(
status_code=204,
notification_sent=True,
),
),
( # verify invalid auth
Payload(
auth=(settings.basic_auth_username, b"invalid-password"),
Expand Down

0 comments on commit 0580fcd

Please sign in to comment.