#!/usr/bin/env python # -*- coding: utf-8 -*- """------------------------------------------------------------------------------------------- -- IMPORTS -------------------------------------------------------------------------------------------""" """ --- PYTHON IMPORTS --- """ import os from os.path import dirname, abspath """ --- STASHR CORE IMPORTS --- """ from stashr import log, utils from stashr import database as stashr_database from stashr.stashr import stashr_notification, stashr_notification from stashr.api import create_json_return """ --- STASHR PLUGIN IMPORTS --- """ from . import forms from .config import pushconfig """ --- STASHR PLUGIN DEPENDENCY IMPORTS --- """ try: from pushbullet import Pushbullet except: utils.install_package('pushbullet.py') from pushbullet import Pushbullet """ --- FLASK EXTENSION IMPORTS --- """ from flask_login import login_required, current_user """ --- STASHR DEPENDENCY IMPORTS --- """ from flask import Blueprint, render_template, request, redirect, flash, url_for """ --- CREATE LOGGER --- """ logger = log.stashr_logger(__name__) """------------------------------------------------------------------------------------------- -- PLUGIN -------------------------------------------------------------------------------------------""" __plugin_name__ = "Stashr Pushbullet" __version__ = "0.1.0" __author__ = "Stashr" __description__ = "Send notifications via Pushbullet" """------------------------------ - PUSHBULLET OBJECT ------------------------------""" pb = None pb_active = False """------------------------------ - PLUGIN ROUTES ------------------------------""" """ --- DEFINE BLUEPRINT --- """ bp = Blueprint('pushbullet', __name__, root_path=dirname(abspath(__file__)), template_folder='templates', static_folder='static') """ --- PAGES --- """ @bp.route('/settings', methods=['GET', 'POST']) @login_required def pushbullet_settings_page(): if current_user.role != 'admin': flash('Permission Denied', 'error') return redirect(url_for('index_page')) settings_form = forms.settings_form() if request.method == 'POST': if settings_form.validate(): pushconfig['PUSHBULLET']['api_key'] = settings_form.api_key.data pushconfig['PUSHBULLET']['channel'] = settings_form.channel.data pushconfig.write() define_pushbullet() flash('Pushbullet Settings Updated', 'success') else: for error in settings_form.errors.items(): flash(f'{error[0]}: {error[1]}', 'error') return render_template( 'pushbullet_settings.html', title='Pushbullet Settings', settings_form=settings_form ) """ --- API --- """ @bp.route('/api/settings', methods=['GET']) def api_get_settings(): user = current_user if not user.is_authenticated: api_key = request.args.get('api_key') if api_key == "": return create_json_return('100') user = stashr_database.session \ .query(stashr_database.Users) \ .filter(stashr_database.Users.api_key == api_key) \ .first() if user is None: return create_json_return('100') if user.role.lower() != 'admin': return create_json_return('401') settings = pushconfig return create_json_return('200', results=settings) """------------------------------ - PLUGIN FUNCTIONS ------------------------------""" # --- Subscribe to stashr_notifications signal --- # @stashr_notification.connect def send_notification(*args, **kwargs): global pb global pb_active if not pb_active: define_pushbullet() if 'title' in kwargs and 'message' in kwargs: pb_channel = pb.get_channel(pushconfig['PUSHBULLET']['channel']) pb_channel.push_note(kwargs['title'], kwargs['message']) def define_pushbullet(): global pb global pb_active try: pb = Pushbullet(pushconfig['PUSHBULLET']['api_key']) pb_active = True except Exception as e: logger.error(e) """------------------------------ - REGISTER PLUGIN ------------------------------""" def register(): return dict( bep=dict( blueprint=bp, prefix='/pushbullet' ), tep = dict( settings_menu='pushbullet_tep_settings_menu.html', ) )