bazarr/bazarr/notifier.py

127 lines
3.2 KiB
Python
Raw Normal View History

# coding=utf-8
import apprise
import os
import logging
from get_args import args
2019-08-20 18:53:16 +00:00
from database import TableSettingsNotifier, TableShows, TableEpisodes, TableMovies
def update_notifier():
# define apprise object
a = apprise.Apprise()
2019-06-11 18:45:48 +00:00
# Retrieve all of the details
results = a.details()
2019-06-11 18:45:48 +00:00
notifiers_new = []
notifiers_old = []
2019-06-11 18:45:48 +00:00
2019-08-21 01:06:10 +00:00
notifiers_current_db = TableSettingsNotifier.select(
2019-08-20 18:53:16 +00:00
TableSettingsNotifier.name
)
2019-08-21 01:06:10 +00:00
notifiers_current = []
for notifier in notifiers_current_db:
notifiers_current.append(notifier.name)
for x in results['schemas']:
2019-08-21 01:06:10 +00:00
if x['service_name'] not in notifiers_current:
notifiers_new.append(x['service_name'])
logging.debug('Adding new notifier agent: ' + x['service_name'])
else:
notifiers_old.append(x['service_name'])
2019-08-21 01:06:10 +00:00
notifier_current = [i for i in notifiers_current]
2019-06-11 18:45:48 +00:00
notifiers_to_delete = list(set(notifier_current) - set(notifiers_old))
2019-06-11 18:45:48 +00:00
for notifier_new in notifiers_new:
2019-08-20 18:53:16 +00:00
TableSettingsNotifier.insert(
{
TableSettingsNotifier.name: notifier_new,
TableSettingsNotifier.enabled: 0
}
).execute()
2019-06-11 18:45:48 +00:00
for notifier_to_delete in notifiers_to_delete:
2019-08-20 18:53:16 +00:00
TableSettingsNotifier.delete().where(
2019-08-21 01:06:10 +00:00
TableSettingsNotifier.name == notifier_to_delete
2019-08-20 18:53:16 +00:00
).execute()
def get_notifier_providers():
2019-08-20 18:53:16 +00:00
providers = TableSettingsNotifier.select(
TableSettingsNotifier.name,
TableSettingsNotifier.url
).where(
TableSettingsNotifier.enabled == 1
)
2019-06-11 18:45:48 +00:00
return providers
def get_series_name(sonarrSeriesId):
2019-08-20 18:53:16 +00:00
data = TableShows.select(
TableShows.title
).where(
TableShows.sonarr_series_id == sonarrSeriesId
).first()
2019-06-11 18:45:48 +00:00
2019-08-20 18:53:16 +00:00
return data.title
def get_episode_name(sonarrEpisodeId):
2019-08-20 18:53:16 +00:00
data = TableEpisodes.select(
TableEpisodes.title,
TableEpisodes.season,
TableEpisodes.episode
).where(
TableEpisodes.sonarr_episode_id == sonarrEpisodeId
).first()
2019-06-11 18:45:48 +00:00
2019-08-20 18:53:16 +00:00
return data.title, data.season, data.episode
2018-04-24 14:48:52 +00:00
def get_movies_name(radarrId):
2019-08-20 18:53:16 +00:00
data = TableMovies.select(
TableMovies.title
).where(
TableMovies.radarr_id == radarrId
).first()
2019-06-11 18:45:48 +00:00
2019-08-20 18:53:16 +00:00
return data.title
2018-04-24 14:48:52 +00:00
def send_notifications(sonarrSeriesId, sonarrEpisodeId, message):
providers = get_notifier_providers()
series = get_series_name(sonarrSeriesId)
episode = get_episode_name(sonarrEpisodeId)
2019-06-11 18:45:48 +00:00
apobj = apprise.Apprise()
2019-06-11 18:45:48 +00:00
for provider in providers:
2019-08-21 17:38:33 +00:00
if provider.url is not None:
apobj.add(provider.url)
2019-06-11 18:45:48 +00:00
apobj.notify(
title='Bazarr notification',
2019-08-21 17:48:07 +00:00
body=(series + ' - S' + str(episode[1]).zfill(2) + 'E' + str(episode[2]).zfill(2) + ' - ' + episode[0] + ' : ' + message),
2018-04-24 14:48:52 +00:00
)
def send_notifications_movie(radarrId, message):
providers = get_notifier_providers()
movie = get_movies_name(radarrId)
2019-06-11 18:45:48 +00:00
2018-04-24 14:48:52 +00:00
apobj = apprise.Apprise()
2019-06-11 18:45:48 +00:00
2018-04-24 14:48:52 +00:00
for provider in providers:
2019-08-21 17:38:33 +00:00
if provider.url is not None:
apobj.add(provider.url)
2019-06-11 18:45:48 +00:00
2018-04-24 14:48:52 +00:00
apobj.notify(
title='Bazarr notification',
body=movie + ' : ' + message,
)