adding rich notifications

This commit is contained in:
blacktwin 2020-05-10 22:27:43 -04:00
parent b95ac52e00
commit a8d8b1b66f

View File

@ -142,7 +142,7 @@ def daterange(start_date, end_date):
yield start_date + timedelta(n) yield start_date + timedelta(n)
def get_user_stats(home_stats): def get_user_stats(home_stats, rich):
user_stats_lst = [] user_stats_lst = []
user_stats_dict = {} user_stats_dict = {}
print('Checking users stats.') print('Checking users stats.')
@ -155,13 +155,16 @@ def get_user_stats(home_stats):
if user not in USER_IGNORE: if user not in USER_IGNORE:
user_total = timedelta(seconds=duration) user_total = timedelta(seconds=duration)
USER_STATS = USER_STAT.format(user, user_total) USER_STATS = USER_STAT.format(user, user_total)
# Html formatting if rich:
user_stats_lst += ['<li>{}</li>'.format(USER_STATS)] user_stats_lst += ['{}'.format(USER_STATS)]
else:
# Html formatting
user_stats_lst += ['<li>{}</li>'.format(USER_STATS)]
return user_stats_lst return user_stats_lst
def get_library_stats(libraries, tautulli): def get_library_stats(libraries, tautulli, rich):
section_count = '' section_count = ''
total_size = 0 total_size = 0
sections_stats_lst = [] sections_stats_lst = []
@ -185,11 +188,17 @@ def get_library_stats(libraries, tautulli):
section_count = MOVIE_STAT.format(section['count']) section_count = MOVIE_STAT.format(section['count'])
if section['section_name'] not in LIB_IGNORE and section_count: if section['section_name'] not in LIB_IGNORE and section_count:
# Html formatting if rich:
sections_stats_lst += ['<li>{}: {}</li>'.format(section['section_name'], section_count)] sections_stats_lst += ['{}: {}'.format(section['section_name'], section_count)]
else:
# Html formatting
sections_stats_lst += ['<li>{}: {}</li>'.format(section['section_name'], section_count)]
# Html formatting. Adding the Capacity to bottom of list. if rich:
sections_stats_lst += ['<li>Capacity: {}</li>'.format(sizeof_fmt(total_size))] sections_stats_lst += ['Capacity: {}'.format(sizeof_fmt(total_size))]
else:
# Html formatting. Adding the Capacity to bottom of list.
sections_stats_lst += ['<li>Capacity: {}</li>'.format(sizeof_fmt(total_size))]
return sections_stats_lst return sections_stats_lst
@ -292,14 +301,14 @@ class Tautulli:
class Notification: class Notification:
def __init__(self, notifier_id, subject, body, tautulli, user=None): def __init__(self, notifier_id, subject, body, tautulli, stats=None):
self.notifier_id = notifier_id self.notifier_id = notifier_id
self.subject = subject self.subject = subject
self.body = body self.body = body
self.tautulli = tautulli self.tautulli = tautulli
if user: if stats:
self.user = user self.stats = stats
def send(self, subject='', body=''): def send(self, subject='', body=''):
"""Send to Tautulli notifier. """Send to Tautulli notifier.
@ -315,7 +324,7 @@ class Notification:
body = body or self.body body = body or self.body
self.tautulli.notify(notifier_id=self.notifier_id, subject=subject, body=body) self.tautulli.notify(notifier_id=self.notifier_id, subject=subject, body=body)
def send_discord(self, title, color, poster_url, plex_url, message, footer): def send_discord(self, title, color, stat, footer):
"""Build the Discord message. """Build the Discord message.
Parameters Parameters
@ -324,14 +333,6 @@ class Notification:
The title of the message. The title of the message.
color : int color : int
The color of the message The color of the message
poster_url : str
The media poster URL.
plex_url : str
Plex media URL.
message : str
Message sent to the player.
footer : str
Footer of the message.
""" """
discord_message = { discord_message = {
"embeds": [ "embeds": [
@ -339,34 +340,18 @@ class Notification:
"author": { "author": {
"icon_url": TAUTULLI_ICON, "icon_url": TAUTULLI_ICON,
"name": "Tautulli", "name": "Tautulli",
"url": TAUTULLI_LINK.rstrip('/')
}, },
"color": color, "color": color,
"fields": [ "fields": [
{ {
"inline": True, "name": "{} Stats".format(stat),
"name": "User", "value": "".join(self.stats)
"value": self.user.friendly_name
}, },
{
"inline": True,
"name": "Watched",
"value": self.user.stats
},
{
"inline": False,
"name": "Message Sent",
"value": message
}
], ],
"thumbnail": {
"url": poster_url
},
"title": title, "title": title,
"timestamp": utc_now_iso(), "timestamp": utc_now_iso(),
"url": plex_url,
"footer": { "footer": {
"text": footer "text": " to ".join(x for x in footer)
} }
} }
@ -378,7 +363,7 @@ class Notification:
separators=(',', ': ')) separators=(',', ': '))
self.send(body=discord_message) self.send(body=discord_message)
def send_slack(self, title, color, poster_url, plex_url, message, footer): def send_slack(self, title, color, stat):
"""Build the Slack message. """Build the Slack message.
Parameters Parameters
@ -400,30 +385,17 @@ class Notification:
"attachments": [ "attachments": [
{ {
"title": title, "title": title,
"title_link": plex_url,
"author_icon": TAUTULLI_ICON, "author_icon": TAUTULLI_ICON,
"author_name": "Tautulli", "author_name": "Tautulli",
"author_link": TAUTULLI_LINK.rstrip('/'), "author_link": TAUTULLI_LINK.rstrip('/'),
"color": color, "color": color,
"fields": [ "fields": [
{ {
"title": "User", "title": "{} Stats".format(stat),
"value": self.user.friendly_name, "value": self.stats,
"short": True "short": True
}, },
{
"title": "Watched",
"value": self.user.stats,
"short": True
},
{
"title": "Message Sent",
"value": message,
"short": False
}
], ],
"thumb_url": poster_url,
"footer": footer,
"ts": time.time() "ts": time.time()
} }
@ -443,8 +415,15 @@ if __name__ == '__main__':
help='Enter in number of days to go back. \n(default: %(default)s)') help='Enter in number of days to go back. \n(default: %(default)s)')
parser.add_argument('-t', '--top', default=5, metavar='', type=int, parser.add_argument('-t', '--top', default=5, metavar='', type=int,
help='Enter in number of top users to find. \n(default: %(default)s)') help='Enter in number of top users to find. \n(default: %(default)s)')
parser.add_argument('--notify', type=int,
help='Notification Agent ID number to Agent to '
'send notification.')
parser.add_argument('--richMessage', choices=RICH_TYPE,
help='Rich message type selector.\nChoices: (%(choices)s)')
parser.add_argument('--refresh', action='store_true', parser.add_argument('--refresh', action='store_true',
help='Refresh all libraries in Tautulli') help='Refresh all libraries in Tautulli')
# todo Goals: growth reporting? show library size growth over time?
opts = parser.parse_args() opts = parser.parse_args()
@ -467,19 +446,30 @@ if __name__ == '__main__':
dates_range_lst += [single_date.strftime("%Y-%m-%d")] dates_range_lst += [single_date.strftime("%Y-%m-%d")]
libraries = tautulli_server.get_libraries() libraries = tautulli_server.get_libraries()
lib_stats = get_library_stats(libraries, tautulli_server) lib_stats = get_library_stats(libraries, tautulli_server, opts.richMessage)
sections_stats = "\n".join(lib_stats) sections_stats = "\n".join(lib_stats)
print('Checking user stats from {:02d} days ago.'.format(opts.days)) print('Checking user stats from {:02d} days ago.'.format(opts.days))
home_stats = tautulli_server.get_home_stats(opts.days, 'duration', opts.top) home_stats = tautulli_server.get_home_stats(opts.days, 'duration', opts.top)
user_stats_lst = get_user_stats(home_stats) user_stats_lst = get_user_stats(home_stats, opts.richMessage)
user_stats = "\n".join(user_stats_lst)
end = datetime.strptime(time.ctime(float(TODAY)), "%a %b %d %H:%M:%S %Y").strftime("%a %b %d %Y") end = datetime.strptime(time.ctime(float(TODAY)), "%a %b %d %H:%M:%S %Y").strftime("%a %b %d %Y")
start = datetime.strptime(time.ctime(float(DAYS_AGO)), "%a %b %d %H:%M:%S %Y").strftime("%a %b %d %Y") start = datetime.strptime(time.ctime(float(DAYS_AGO)), "%a %b %d %H:%M:%S %Y").strftime("%a %b %d %Y")
BODY_TEXT = BODY_TEXT.format(end=end, start=start, sections_stats=sections_stats, user_stats=user_stats) user_stats = "\n".join(user_stats_lst)
print('Sending message.') if opts.notify and opts.richMessage:
notify = Notification(NOTIFIER_ID, SUBJECT_TEXT, BODY_TEXT, tautulli_server) user_notification = Notification(opts.notify, None, None, tautulli_server, user_stats)
notify.send() section_notification= Notification(opts.notify, None, None, tautulli_server, sections_stats)
if opts.richMessage == 'slack':
user_notification.send_slack(SUBJECT_TEXT, 10964298, 'User')
section_notification.send_slack(SUBJECT_TEXT, 10964298, 'Section')
elif opts.richMessage == 'discord':
user_notification.send_discord(SUBJECT_TEXT, 10964298, 'User', footer=(end,start))
section_notification.send_discord(SUBJECT_TEXT, 10964298, 'Section', footer=(end,start))
else:
BODY_TEXT = BODY_TEXT.format(end=end, start=start, sections_stats=sections_stats, user_stats=user_stats)
print('Sending message.')
notify = Notification(opts.notify, SUBJECT_TEXT, BODY_TEXT, tautulli_server)
notify.send()