-
Notifications
You must be signed in to change notification settings - Fork 278
/
prowl.py
166 lines (136 loc) · 6.37 KB
/
prowl.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# coding=utf-8
from __future__ import unicode_literals
import ast
import logging
import socket
import time
from builtins import object
from medusa import app, common, db
from medusa.helper.encoding import ss
from medusa.logger.adapters.style import BraceAdapter
from requests.compat import urlencode
from six.moves.http_client import HTTPException, HTTPSConnection
try:
# this only exists in 2.6
from ssl import SSLError
except ImportError:
# make a fake one since I don't know what it is supposed to be in 2.5
class SSLError(Exception):
pass
log = BraceAdapter(logging.getLogger(__name__))
log.logger.addHandler(logging.NullHandler())
class Notifier(object):
def test_notify(self, prowl_api, prowl_priority):
return self._send_prowl(prowl_api, prowl_priority, event='Test', message='Testing Prowl settings from Medusa', force=True)
def notify_snatch(self, ep_name, is_proper):
ep_name = ss(ep_name)
if app.PROWL_NOTIFY_ONSNATCH:
show = self._parse_episode(ep_name)
recipients = self._generate_recipients(show)
if not recipients:
log.debug('Skipping prowl notify because there are no configured recipients')
else:
for api in recipients:
self._send_prowl(prowl_api=api, prowl_priority=None, event=common.notifyStrings[(common.NOTIFY_SNATCH, common.NOTIFY_SNATCH_PROPER)[is_proper]],
message=ep_name + ' :: ' + time.strftime(app.DATE_PRESET + ' ' + app.TIME_PRESET))
def notify_download(self, ep_name):
ep_name = ss(ep_name)
if app.PROWL_NOTIFY_ONDOWNLOAD:
show = self._parse_episode(ep_name)
recipients = self._generate_recipients(show)
if not recipients:
log.debug('Skipping prowl notify because there are no configured recipients')
else:
for api in recipients:
self._send_prowl(prowl_api=api, prowl_priority=None, event=common.notifyStrings[common.NOTIFY_DOWNLOAD],
message=ep_name + ' :: ' + time.strftime(app.DATE_PRESET + ' ' + app.TIME_PRESET))
def notify_subtitle_download(self, ep_name, lang):
ep_name = ss(ep_name)
if app.PROWL_NOTIFY_ONSUBTITLEDOWNLOAD:
show = self._parse_episode(ep_name)
recipients = self._generate_recipients(show)
if not recipients:
log.debug('Skipping prowl notify because there are no configured recipients')
else:
for api in recipients:
self._send_prowl(prowl_api=api, prowl_priority=None, event=common.notifyStrings[common.NOTIFY_SUBTITLE_DOWNLOAD],
message=ep_name + ' [' + lang + '] :: ' + time.strftime(app.DATE_PRESET + ' ' + app.TIME_PRESET))
def notify_git_update(self, new_version='??'):
if app.USE_PROWL:
update_text = common.notifyStrings[common.NOTIFY_GIT_UPDATE_TEXT]
title = common.notifyStrings[common.NOTIFY_GIT_UPDATE]
self._send_prowl(prowl_api=None, prowl_priority=None,
event=title, message=update_text + new_version)
def notify_login(self, ipaddress=''):
if app.USE_PROWL:
update_text = common.notifyStrings[common.NOTIFY_LOGIN_TEXT]
title = common.notifyStrings[common.NOTIFY_LOGIN]
self._send_prowl(prowl_api=None, prowl_priority=None,
event=title, message=update_text.format(ipaddress))
@staticmethod
def _generate_recipients(show=None):
apis = []
mydb = db.DBConnection()
# Grab the global recipient(s)
if app.PROWL_API:
for api in app.PROWL_API:
if api.strip():
apis.append(api)
# Grab the per-show-notification recipients
if show is not None:
for value in show:
for subs in mydb.select('SELECT notify_list FROM tv_shows WHERE show_name = ?', (value,)):
if subs['notify_list']:
if subs['notify_list'][0] == '{': # legacy format handling
entries = dict(ast.literal_eval(subs['notify_list']))
for api in entries['prowlAPIs'].split(','):
if api.strip():
apis.append(api)
apis = set(apis)
return apis
@staticmethod
def _send_prowl(prowl_api=None, prowl_priority=None, event=None, message=None, force=False):
if not app.USE_PROWL and not force:
return False
if prowl_api is None:
prowl_api = ','.join(app.PROWL_API)
if not prowl_api:
return False
if prowl_priority is None:
prowl_priority = app.PROWL_PRIORITY
title = app.PROWL_MESSAGE_TITLE
log.debug(u'PROWL: Sending notice with details: title="{0}" event="{1}", message="{2}", priority={3}, api={4}',
title, event, message, prowl_priority, prowl_api)
http_handler = HTTPSConnection('api.prowlapp.com')
data = {'apikey': prowl_api,
'application': title,
'event': event,
'description': message.encode('utf-8'),
'priority': prowl_priority}
try:
http_handler.request('POST',
'/publicapi/add',
headers={'Content-type': 'application/x-www-form-urlencoded'},
body=urlencode(data))
except (SSLError, HTTPException, socket.error):
log.error(u'Prowl notification failed.')
return False
response = http_handler.getresponse()
request_status = response.status
if request_status == 200:
log.info(u'Prowl notifications sent.')
return True
elif request_status == 401:
log.error(u'Prowl auth failed: {0}', response.reason)
return False
else:
log.error(u'Prowl notification failed.')
return False
@staticmethod
def _parse_episode(ep_name):
ep_name = ss(ep_name)
sep = ' - '
titles = ep_name.split(sep)
titles.sort(key=len, reverse=True)
log.debug('TITLES: {0}', titles)
return titles