Skip to content

Commit

Permalink
feat: support Added event type
Browse files Browse the repository at this point in the history
  • Loading branch information
danielx committed Oct 20, 2024
1 parent 2d9b67a commit 7a236df
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 7 deletions.
5 changes: 3 additions & 2 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,11 +22,12 @@ 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.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
2 changes: 1 addition & 1 deletion app/schemas/_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ class SonarrEvent(BaseModel):
eventType: str

series: Series
episodes: list[Episode]
episodes: list[Episode] = []
14 changes: 13 additions & 1 deletion tests/test_radarr.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class Result(NamedTuple):
notification_sent=False,
),
),
( # only notify on eventType=Download
( # notify on eventType=Download
Payload(
json={
"eventType": "Download",
Expand All @@ -55,6 +55,18 @@ class Result(NamedTuple):
notification_sent=True,
),
),
( # notify on eventType=MovieAdded
Payload(
json={
"eventType": "MovieAdded",
"movie": {"title": "string", "year": 0, "tmdbId": 0},
}
),
Result(
status_code=204,
notification_sent=True,
),
),
( # verify invalid auth
Payload(
auth=(settings.basic_auth_username, b"invalid-password"),
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 7a236df

Please sign in to comment.