/
hipchat.py
154 lines (130 loc) · 5.71 KB
/
hipchat.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
from __future__ import unicode_literals, division, absolute_import
import logging
from flexget import plugin
from flexget.event import event
from flexget.utils import json
from flexget.utils.template import RenderError
from flexget.config_schema import one_or_more
log = logging.getLogger('hipchat')
class OutputHipchat(object):
"""
Example::
hipchat:
auth:
auth_token: <AUTH_TOKEN>
room_key: <ROOM_KEY>
[color: green|yellow|red|grey|purple (default: green)]
[notify: true|false (default: false)]
[title: <TITLE> (default: "New download started:")]
[message: <MESSAGE_BODY> (default: "{{ series_name }} {{ series_id }} {{ quality }}" -- accepts Jinja2)]
[url: <HIPCHAT_URL> (default: https://api.hipchat.com)]
Configuration parameters are also supported from entries (eg. through set).
"""
default_message = ('{% if series_name is defined %}{{tvdb_series_name|d(series_name)}} {{series_id}} '
'{{tvdb_ep_name|d('')}}{% elif imdb_name is defined %}{{imdb_name}} '
'{{imdb_year}}{% else %}{{title}}{% endif %}')
auth = {
'type': 'object',
'properties': {
'auth_token': {'type': 'string'},
'room_key': {'type': 'string'}
},
'required': ['auth_token', 'room_key'],
'additionalProperties': False
}
schema = {
'type': 'object',
'properties': {
'auth': one_or_more(auth),
'color': {'type': 'string', 'default': 'green'},
'notify': {'type': 'string', 'default': 'false'},
'title': {'type': 'string', 'default': 'New download started:'},
'message': {'type': 'string', 'default': default_message},
'url': {'type': 'string', 'default': 'https://api.hipchat.com'},
},
'required': ['auth'],
'additionalProperties': False
}
# Run last to make sure other outputs are successful before sending notification
@plugin.priority(0)
def on_task_output(self, task, config):
for entry in task.accepted:
auth = config['auth']
color = config['color']
notify = config['notify']
title = config['title']
message = config['message']
url = config['url']
# Attempt to render the color field
try:
color = entry.render(color)
except RenderError as e:
log.warning('Problem rendering `color`: %s' % e)
color = 'green'
# Attempt to render the notify field
try:
notify = entry.render(notify)
except RenderError as e:
log.warning('Problem rendering `notify`: %s' % e)
notify = 'false'
# Attempt to render the title field
try:
title = entry.render(title)
except RenderError as e:
log.warning('Problem rendering `title`: %s' % e)
title = 'New download started:'
# Attempt to render the message field
try:
message = entry.render(message)
except RenderError as e:
log.warning('Problem rendering `message`: %s' % e)
message = entry['title']
# Attempt to render the url field
try:
url = entry.render(url)
except RenderError as e:
log.warning('Problem rendering `url`: %s' % e)
url = 'https://api.hipchat.com'
full_url = '%s/v2/room/%s/notification?auth_token=%s' % (url, auth['room_key'], auth['auth_token'])
self.send_push(task, auth['auth_token'], auth['room_key'], color, notify, title, message, full_url)
def send_push(self, task, auth_token, room_key, color, notify, title, message, url):
body = '%s %s' % (title, message)
data = {'color': color, 'message': body, 'notify': notify, 'message_format': "text"}
# Check for test mode
if task.options.test:
log.info('Test mode. Hipchat notification would be:')
log.info(' Auth Token: %s' % auth_token)
log.info(' Room Key: %s' % room_key)
log.info(' Color: %s' % color)
log.info(' Notify: %s' % notify)
log.info(' Title: %s' % title)
log.info(' Message: %s' % message)
log.info(' URL: %s' % url)
log.info(' Raw Data: %s' % json.dumps(data))
# Test mode. Skip remainder.
return
# Make the request
headers = {
'Content-Type': 'application/json'
}
response = task.requests.post(url, headers=headers, data=json.dumps(data), raise_status=False)
# Check if it succeeded
request_status = response.status_code
# error codes and messages from Hipchat API
if request_status == 200:
log.debug('Hipchat notification sent')
elif request_status == 500:
log.warning('Hipchat notification failed, Hipchat API having issues')
# TODO: Implement retrying. API requests 5 seconds between retries.
elif request_status >= 400:
if response.content:
try:
error = json.loads(response.content)['error']
except ValueError:
error = 'Unknown Error (Invalid JSON returned)'
log.error('Hipchat API error: %s' % error['message'])
else:
log.error('Unknown error when sending Hipchat notification')
@event('plugin.register')
def register_plugin():
plugin.register(OutputHipchat, 'hipchat', api_ver=2)