bazarr/bazarr/notifier.py

97 lines
2.8 KiB
Python
Raw Normal View History

# coding=utf-8
2019-09-13 19:12:26 +00:00
from __future__ import absolute_import
import apprise
import os
import logging
from get_args import args
2019-10-22 02:13:37 +00:00
from database import database
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-10-22 02:13:37 +00:00
notifiers_current_db = database.execute("SELECT name FROM table_settings_notifier")
2019-08-20 18:53:16 +00:00
2019-08-21 01:06:10 +00:00
notifiers_current = []
for notifier in notifiers_current_db:
2019-11-13 01:40:16 +00:00
notifiers_current.append([notifier['name']])
2019-08-21 01:06:10 +00:00
for x in results['schemas']:
2019-11-13 01:40:16 +00:00
if [x['service_name']] not in notifiers_current:
notifiers_new.append([x['service_name'], 0])
logging.debug('Adding new notifier agent: ' + x['service_name'])
else:
2019-11-13 01:40:16 +00:00
notifiers_old.append([x['service_name']])
2019-06-11 18:45:48 +00:00
2019-11-13 01:40:16 +00:00
notifiers_to_delete = [item for item in notifiers_current if item not in notifiers_old]
database.execute("INSERT INTO table_settings_notifier (name, enabled) VALUES (?, ?)", notifiers_new, execute_many=True)
2019-06-11 18:45:48 +00:00
2019-11-13 01:40:16 +00:00
database.execute("DELETE FROM table_settings_notifier WHERE name=?", notifiers_to_delete, execute_many=True)
def get_notifier_providers():
2019-10-22 02:13:37 +00:00
providers = database.execute("SELECT name, url FROM table_settings_notifier WHERE enabled=1")
return providers
def get_series_name(sonarrSeriesId):
2019-10-27 01:16:59 +00:00
data = database.execute("SELECT title FROM table_shows WHERE sonarrSeriesId=?", (sonarrSeriesId,), only_one=True)
2019-06-11 18:45:48 +00:00
2019-10-27 03:17:14 +00:00
return data['title'] or None
def get_episode_name(sonarrEpisodeId):
2019-10-27 01:16:59 +00:00
data = database.execute("SELECT title, season, episode FROM table_episodes WHERE sonarrEpisodeId=?",
(sonarrEpisodeId,), only_one=True)
2019-06-11 18:45:48 +00:00
2019-10-22 02:13:37 +00:00
return data['title'], data['season'], data['episode']
2018-04-24 14:48:52 +00:00
def get_movies_name(radarrId):
2019-10-27 01:16:59 +00:00
data = database.execute("SELECT title FROM table_movies WHERE radarrId=?", (radarrId,), only_one=True)
2019-10-28 00:45:15 +00:00
2019-10-22 02:13:37 +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:
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:
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,
)