bazarr/bazarr/notifier.py

113 lines
3.4 KiB
Python
Raw Normal View History

# coding=utf-8
import apprise
import os
import sqlite3
import logging
from get_args import args
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
conn_db = sqlite3.connect(os.path.join(args.config_dir, 'db', 'bazarr.db'), timeout=30)
c_db = conn_db.cursor()
notifiers_current = c_db.execute('SELECT name FROM table_settings_notifier').fetchall()
for x in results['schemas']:
if x['service_name'] not in str(notifiers_current):
notifiers_new.append(x['service_name'])
logging.debug('Adding new notifier agent: ' + x['service_name'])
else:
notifiers_old.append(x['service_name'])
notifier_current = [i[0] 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:
c_db.execute('INSERT INTO `table_settings_notifier` (name, enabled) VALUES (?, ?);', (notifier_new, '0'))
2019-06-11 18:45:48 +00:00
for notifier_to_delete in notifiers_to_delete:
c_db.execute('DELETE FROM `table_settings_notifier` WHERE name=?', (notifier_to_delete,))
2019-06-11 18:45:48 +00:00
conn_db.commit()
c_db.close()
def get_notifier_providers():
conn_db = sqlite3.connect(os.path.join(args.config_dir, 'db', 'bazarr.db'), timeout=30)
c_db = conn_db.cursor()
providers = c_db.execute('SELECT name, url FROM table_settings_notifier WHERE enabled = 1').fetchall()
c_db.close()
2019-06-11 18:45:48 +00:00
return providers
def get_series_name(sonarrSeriesId):
conn_db = sqlite3.connect(os.path.join(args.config_dir, 'db', 'bazarr.db'), timeout=30)
c_db = conn_db.cursor()
data = c_db.execute('SELECT title FROM table_shows WHERE sonarrSeriesId = ?', (sonarrSeriesId,)).fetchone()
c_db.close()
2019-06-11 18:45:48 +00:00
return data[0]
def get_episode_name(sonarrEpisodeId):
conn_db = sqlite3.connect(os.path.join(args.config_dir, 'db', 'bazarr.db'), timeout=30)
c_db = conn_db.cursor()
data = c_db.execute('SELECT title, season, episode FROM table_episodes WHERE sonarrEpisodeId = ?',
(sonarrEpisodeId,)).fetchone()
c_db.close()
2019-06-11 18:45:48 +00:00
return data[0], data[1], data[2]
2018-04-24 14:48:52 +00:00
def get_movies_name(radarrId):
conn_db = sqlite3.connect(os.path.join(args.config_dir, 'db', 'bazarr.db'), timeout=30)
2018-04-24 14:48:52 +00:00
c_db = conn_db.cursor()
data = c_db.execute('SELECT title FROM table_movies WHERE radarrId = ?', (radarrId,)).fetchone()
c_db.close()
2019-06-11 18:45:48 +00:00
2018-04-24 14:48:52 +00:00
return data[0]
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[1] is not None:
apobj.add(provider[1])
2019-06-11 18:45:48 +00:00
apobj.notify(
title='Bazarr notification',
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[1] is not None:
apobj.add(provider[1])
2019-06-11 18:45:48 +00:00
2018-04-24 14:48:52 +00:00
apobj.notify(
title='Bazarr notification',
body=movie + ' : ' + message,
)