Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor notify_snatch to use title, message instead of episode name #5584

Merged
merged 6 commits into from
Nov 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions medusa/notifiers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
import socket

from medusa import app
from medusa.common import (
NOTIFY_SNATCH,
NOTIFY_SNATCH_PROPER,
notifyStrings,
)
from medusa.logger.adapters.style import BraceAdapter
from medusa.notifiers import (
boxcar2,
Expand Down Expand Up @@ -102,13 +107,24 @@ def notify_subtitle_download(ep_obj, lang):
try:
n.notify_subtitle_download(ep_obj, lang)
except (RequestException, socket.gaierror, socket.timeout) as error:
log.debug(u'Unable to send download notification. Error: {0}', error.message)
log.debug(u'Unable to send subtitle download notification. Error: {0}', error.message)


def notify_snatch(ep_obj, result):
ep_name = ep_obj.pretty_name_with_quality()
is_proper = bool(result.proper_tags)
title = notifyStrings[(NOTIFY_SNATCH, NOTIFY_SNATCH_PROPER)[is_proper]]

if all([app.SEEDERS_LEECHERS_IN_NOTIFY, result.seeders not in (-1, None),
result.leechers not in (-1, None)]):
message = u'{0} with {1} seeders and {2} leechers from {3}'.format(
ep_name, result.seeders, result.leechers, result.provider.name)
else:
message = u'{0} from {1}'.format(ep_name, result.provider.name)

def notify_snatch(ep_name, is_proper):
for n in notifiers:
try:
n.notify_snatch(ep_name, is_proper)
n.notify_snatch(title, message)
except (RequestException, socket.gaierror, socket.timeout) as error:
log.debug(u'Unable to send snatch notification. Error: {0}', error.message)

Expand Down
5 changes: 2 additions & 3 deletions medusa/notifiers/boxcar2.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,10 @@ def _send_boxcar2(self, msg, title, accesstoken):
log.debug('Boxcar2 notification successful.')
return True

def notify_snatch(self, ep_name, is_proper):
def notify_snatch(self, title, message):
"""Send the snatch message."""
title = common.notifyStrings[(common.NOTIFY_SNATCH, common.NOTIFY_SNATCH_PROPER)[is_proper]]
if app.BOXCAR2_NOTIFY_ONSNATCH:
self._notify_boxcar2(title, ep_name)
self._notify_boxcar2(title, message)

def notify_download(self, ep_obj, title=common.notifyStrings[common.NOTIFY_DOWNLOAD]):
"""Send the download message."""
Expand Down
71 changes: 41 additions & 30 deletions medusa/notifiers/emailnotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@
from email.utils import formatdate

from medusa import app, db
from medusa.helper.encoding import ss
from medusa.common import (
NOTIFY_DOWNLOAD,
NOTIFY_GIT_UPDATE,
NOTIFY_LOGIN,
NOTIFY_SUBTITLE_DOWNLOAD,
notifyStrings,
)
from medusa.logger.adapters.style import BraceAdapter

log = BraceAdapter(logging.getLogger(__name__))
Expand Down Expand Up @@ -51,25 +57,22 @@ def test_notify(self, host, port, smtp_from, use_tls, user, pwd, to):
"""
msg = MIMEText('This is a test message from Medusa. If you\'re reading this, the test succeeded.')
if app.EMAIL_SUBJECT:
msg['Subject'] = '[TEST] ' + app.EMAIL_SUBJECT
msg['Subject'] = '[TEST] {0}'.format(app.EMAIL_SUBJECT)
else:
msg['Subject'] = 'Medusa: Test Message'
msg['From'] = smtp_from
msg['To'] = to
msg['Date'] = formatdate(localtime=True)
return self._sendmail(host, port, smtp_from, use_tls, user, pwd, [to], msg, True)

def notify_snatch(self, ep_name, is_proper, title='Snatched:'):
def notify_snatch(self, title, message):
"""
Send a notification that an episode was snatched.

ep_name: The name of the episode that was snatched
title: The title of the notification (optional)
"""
ep_name = ss(ep_name)

if app.USE_EMAIL and app.EMAIL_NOTIFY_ONSNATCH:
parsed = self._parse_name(ep_name)
parsed = self._parse_name(message)
to = self._generate_recipients(parsed['show'])
if not to:
log.debug('Skipping email notify because there are no configured recipients')
Expand All @@ -92,21 +95,22 @@ def notify_snatch(self, ep_name, is_proper, title='Snatched:'):

except Exception:
try:
msg = MIMEText(ep_name)
msg = MIMEText(message)
except Exception:
msg = MIMEText('Episode Snatched')
msg = MIMEText(title)

if app.EMAIL_SUBJECT:
msg['Subject'] = '[SN] ' + app.EMAIL_SUBJECT
msg['Subject'] = '{0}: {1}'.format(title, app.EMAIL_SUBJECT)
else:
msg['Subject'] = 'Snatched: ' + ep_name
msg['Subject'] = '{0}: {1}'.format(title, message)
msg['From'] = app.EMAIL_FROM
msg['To'] = ','.join(to)
msg['Date'] = formatdate(localtime=True)

if self._sendmail(app.EMAIL_HOST, app.EMAIL_PORT, app.EMAIL_FROM, app.EMAIL_TLS,
app.EMAIL_USER, app.EMAIL_PASSWORD, to, msg):
log.debug('Snatch notification sent to {recipient} for {episode}',
{'recipient': to, 'episode': ep_name})
{'recipient': to, 'episode': message})
else:
log.warning('Snatch notification error: {0}', self.last_err)

Expand All @@ -117,9 +121,10 @@ def notify_download(self, ep_obj, title='Completed:'):
ep_name: The name of the episode that was downloaded
title: The title of the notification (optional)
"""
ep_name = ss(ep_obj.pretty_name_with_quality())

if app.USE_EMAIL and app.EMAIL_NOTIFY_ONDOWNLOAD:
title = notifyStrings[NOTIFY_DOWNLOAD]
ep_name = ep_obj.pretty_name_with_quality()

parsed = self._parse_name(ep_name)
to = self._generate_recipients(parsed['show'])
if not to:
Expand All @@ -145,32 +150,34 @@ def notify_download(self, ep_obj, title='Completed:'):
try:
msg = MIMEText(ep_name)
except Exception:
msg = MIMEText('Episode Downloaded')
msg = MIMEText(title)

if app.EMAIL_SUBJECT:
msg['Subject'] = '[DL] ' + app.EMAIL_SUBJECT
msg['Subject'] = '{0}: {1}'.format(title, app.EMAIL_SUBJECT)
else:
msg['Subject'] = 'Downloaded: ' + ep_name
msg['Subject'] = '{0}: {1}'.format(title, ep_name)
msg['From'] = app.EMAIL_FROM
msg['To'] = ','.join(to)
msg['Date'] = formatdate(localtime=True)

if self._sendmail(app.EMAIL_HOST, app.EMAIL_PORT, app.EMAIL_FROM, app.EMAIL_TLS,
app.EMAIL_USER, app.EMAIL_PASSWORD, to, msg):
log.debug('Download notification sent to {recipient} for {episode}',
{'recipient': to, 'episode': ep_name})
else:
log.warning('Download notification error: {0}', self.last_err)

def notify_subtitle_download(self, ep_obj, lang, title='Downloaded subtitle:'):
def notify_subtitle_download(self, ep_obj, lang):
"""
Send a notification that a subtitle was downloaded.

ep_name: The name of the episode that was downloaded
lang: Subtitle language wanted
"""
ep_name = ss(ep_obj.pretty_name())

if app.USE_EMAIL and app.EMAIL_NOTIFY_ONSUBTITLEDOWNLOAD:
title = notifyStrings[NOTIFY_SUBTITLE_DOWNLOAD]
ep_name = ep_obj.pretty_name()

parsed = self._parse_name(ep_name)
to = self._generate_recipients(parsed['show'])
if not to:
Expand All @@ -195,16 +202,17 @@ def notify_subtitle_download(self, ep_obj, lang, title='Downloaded subtitle:'):
'html'))
except Exception:
try:
msg = MIMEText(ep_name + ': ' + lang)
msg = MIMEText('{0}: {1}'.format(ep_name, lang))
except Exception:
msg = MIMEText('Episode Subtitle Downloaded')
msg = MIMEText(title)

if app.EMAIL_SUBJECT:
msg['Subject'] = '[ST] ' + app.EMAIL_SUBJECT
msg['Subject'] = '{0} [{1}]: {2}'.format(title, lang, app.EMAIL_SUBJECT)
else:
msg['Subject'] = lang + ' Subtitle Downloaded: ' + ep_name
msg['Subject'] = '{0} [{1}]: {2}'.format(title, lang, ep_name)
msg['From'] = app.EMAIL_FROM
msg['To'] = ','.join(to)

if self._sendmail(app.EMAIL_HOST, app.EMAIL_PORT, app.EMAIL_FROM, app.EMAIL_TLS,
app.EMAIL_USER, app.EMAIL_PASSWORD, to, msg):
log.debug('Download notification sent to {recipient} for {episode}',
Expand All @@ -219,6 +227,7 @@ def notify_git_update(self, new_version='??'):
new_version: The commit Medusa was updated to
"""
if app.USE_EMAIL:
title = notifyStrings[NOTIFY_GIT_UPDATE]
to = self._generate_recipients(None)
if not to:
log.debug('Skipping email notify because there are no configured recipients')
Expand All @@ -238,12 +247,13 @@ def notify_git_update(self, new_version='??'):
try:
msg = MIMEText(new_version)
except Exception:
msg = MIMEText('Medusa updated')
msg = MIMEText(title)

msg['Subject'] = 'Updated: {0}'.format(new_version)
msg['Subject'] = '{0}: {1}'.format(title, new_version)
msg['From'] = app.EMAIL_FROM
msg['To'] = ','.join(to)
msg['Date'] = formatdate(localtime=True)

if self._sendmail(app.EMAIL_HOST, app.EMAIL_PORT, app.EMAIL_FROM, app.EMAIL_TLS,
app.EMAIL_USER, app.EMAIL_PASSWORD, to, msg):
log.debug('Update notification sent to {recipient}',
Expand All @@ -258,6 +268,7 @@ def notify_login(self, ipaddress=''):
ipaddress: The ip Medusa was logged into from
"""
if app.USE_EMAIL:
title = notifyStrings[NOTIFY_LOGIN]
to = self._generate_recipients(None)
if not to:
log.debug('Skipping email notify because there are no configured recipients')
Expand All @@ -277,12 +288,13 @@ def notify_login(self, ipaddress=''):
try:
msg = MIMEText(ipaddress)
except Exception:
msg = MIMEText('Medusa Remote Login')
msg = MIMEText(title)

msg['Subject'] = 'New Login from IP: {0}'.format(ipaddress)
msg['Subject'] = '{0}: {1}'.format(title, ipaddress)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These new subjects are somewhat less informative then the old. Whas that on purpose?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ow i swe that these gave all been added to notifystrings. Never mind.

msg['From'] = app.EMAIL_FROM
msg['To'] = ','.join(to)
msg['Date'] = formatdate(localtime=True)

if self._sendmail(app.EMAIL_HOST, app.EMAIL_PORT, app.EMAIL_FROM, app.EMAIL_TLS,
app.EMAIL_USER, app.EMAIL_PASSWORD, to, msg):
log.debug('Login notification sent to {recipient}', {'recipient': to})
Expand Down Expand Up @@ -371,8 +383,6 @@ def _sendmail(self, host, port, smtp_from, use_tls, user, pwd, to, msg, smtp_deb

@classmethod
def _parse_name(cls, ep_name):
ep_name = ss(ep_name)

# @TODO: Prone to issues, best solution is to have a dictionary passed to notifiers
match = cls.name_pattern.match(ep_name)

Expand All @@ -390,4 +400,5 @@ def _parse_name(cls, ep_name):

log.debug('Email notifier parsed "{0}" into {1!r}',
ep_name, result)

return result
7 changes: 2 additions & 5 deletions medusa/notifiers/freemobile.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
NOTIFY_GIT_UPDATE_TEXT,
NOTIFY_LOGIN,
NOTIFY_LOGIN_TEXT,
NOTIFY_SNATCH,
NOTIFY_SNATCH_PROPER,
NOTIFY_SUBTITLE_DOWNLOAD,
notifyStrings,
)
Expand Down Expand Up @@ -78,10 +76,9 @@ def _sendFreeMobileSMS(self, title, msg, cust_id=None, apiKey=None):
log.info(message)
return True, message

def notify_snatch(self, ep_name, is_proper):
title = notifyStrings[(NOTIFY_SNATCH, NOTIFY_SNATCH_PROPER)[is_proper]]
def notify_snatch(self, title, message):
if app.FREEMOBILE_NOTIFY_ONSNATCH:
self._notifyFreeMobile(title, ep_name)
self._notifyFreeMobile(title, message)

def notify_download(self, ep_obj, title=notifyStrings[NOTIFY_DOWNLOAD]):
if app.FREEMOBILE_NOTIFY_ONDOWNLOAD:
Expand Down
7 changes: 2 additions & 5 deletions medusa/notifiers/growl.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,9 @@ def test_notify(self, host, password):
return self._sendGrowl('Test Growl', 'Testing Growl settings from Medusa', 'Test', host, password,
force=True)

def notify_snatch(self, ep_name, is_proper):
def notify_snatch(self, title, message):
if app.GROWL_NOTIFY_ONSNATCH:
self._sendGrowl(
common.notifyStrings[
(common.NOTIFY_SNATCH, common.NOTIFY_SNATCH_PROPER)[is_proper]
], ep_name)
self._sendGrowl(title, message)

def notify_download(self, ep_obj):
if app.GROWL_NOTIFY_ONDOWNLOAD:
Expand Down
6 changes: 3 additions & 3 deletions medusa/notifiers/join.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ def test_notify(self, join_api, join_device):
force=True
)

def notify_snatch(self, ep_name, is_proper):
def notify_snatch(self, title, message):
"""Send Join notification when nzb snatched if selected in config."""
if app.JOIN_NOTIFY_ONSNATCH:
self._sendjoin(
join_api=None,
event=common.notifyStrings[(common.NOTIFY_SNATCH, common.NOTIFY_SNATCH_PROPER)[is_proper]] + ' : ' + ep_name,
message=ep_name
event=title,
message=message
)

def notify_download(self, ep_name):
Expand Down
17 changes: 8 additions & 9 deletions medusa/notifiers/kodi.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def _get_kodi_version(self, host, username, password, dest_app='KODI'):
else:
return False

def _notify_kodi(self, message, title='Medusa', host=None, username=None, password=None,
def _notify_kodi(self, title, message, host=None, username=None, password=None,
force=False, dest_app='KODI'):
"""Private wrapper for the notify_snatch and notify_download functions.

Expand Down Expand Up @@ -421,39 +421,38 @@ def _update_library(self, host=None, series_name=None): # pylint: disable=too-m
# Public functions which will call the JSON or Legacy HTTP API methods
##############################################################################

def notify_snatch(self, ep_name, is_proper):
def notify_snatch(self, title, message):
"""Send the snatch message."""
if app.KODI_NOTIFY_ONSNATCH:
self._notify_kodi(ep_name, common.notifyStrings[(common.NOTIFY_SNATCH,
common.NOTIFY_SNATCH_PROPER)[is_proper]])
self._notify_kodi(title, message)

def notify_download(self, ep_obj):
"""Send the download message."""
if app.KODI_NOTIFY_ONDOWNLOAD:
self._notify_kodi(ep_obj.pretty_name_with_quality(), common.notifyStrings[common.NOTIFY_DOWNLOAD])
self._notify_kodi(common.notifyStrings[common.NOTIFY_DOWNLOAD], ep_obj.pretty_name_with_quality())

def notify_subtitle_download(self, ep_obj, lang):
"""Send the subtitle download message."""
if app.KODI_NOTIFY_ONSUBTITLEDOWNLOAD:
self._notify_kodi(ep_obj.pretty_name() + ': ' + lang, common.notifyStrings[common.NOTIFY_SUBTITLE_DOWNLOAD])
self._notify_kodi(common.notifyStrings[common.NOTIFY_SUBTITLE_DOWNLOAD], ep_obj.pretty_name() + ': ' + lang)

def notify_git_update(self, new_version='??'):
"""Send update available message."""
if app.USE_KODI:
update_text = common.notifyStrings[common.NOTIFY_GIT_UPDATE_TEXT]
title = common.notifyStrings[common.NOTIFY_GIT_UPDATE]
self._notify_kodi(update_text + new_version, title)
self._notify_kodi(title, update_text + new_version)

def notify_login(self, ipaddress=''):
"""Send the new login message."""
if app.USE_KODI:
update_text = common.notifyStrings[common.NOTIFY_LOGIN_TEXT]
title = common.notifyStrings[common.NOTIFY_LOGIN]
self._notify_kodi(update_text.format(ipaddress), title)
self._notify_kodi(title, update_text.format(ipaddress))

def test_notify(self, host, username, password):
"""Test notifier."""
return self._notify_kodi('Testing KODI notifications from Medusa', 'Test Notification', host, username,
return self._notify_kodi('Test Notification', 'Testing KODI notifications from Medusa', host, username,
password, force=True)

def update_library(self, series_name=None):
Expand Down
4 changes: 2 additions & 2 deletions medusa/notifiers/libnotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ def init_notify(self):
self.gobject = GObject
return True

def notify_snatch(self, ep_name, is_proper):
def notify_snatch(self, title, message):
if app.LIBNOTIFY_NOTIFY_ONSNATCH:
self._notify(common.notifyStrings[(common.NOTIFY_SNATCH, common.NOTIFY_SNATCH_PROPER)[is_proper]], ep_name)
self._notify(title, message)

def notify_download(self, ep_obj):
if app.LIBNOTIFY_NOTIFY_ONDOWNLOAD:
Expand Down
Loading