#!/usr/bin/env python

import requests
import json
import re

import weechat as w

w.register('weebullet', 'Lefty', '0.0.1', 'BSD', 'weebullet pushes notifications from IRC to Pushbullet.', '', '')

w.hook_print("", "irc_privmsg", "", 1, "priv_msg_cb", "")
w.hook_command(
    "send_push_note", #command
    "send a push note", # description
    "[message]" # arguments description, 
    "", #argument
    "",
    "",
    "cmd_send_push_note", "")

credentials = {
    "api_key": ""
}

for option, default_value in credentials.items():
    if w.config_get_plugin(option) == "":
        w.prnt("", w.prefix("error") + "pushbullet: Please set option: %s" % option)
        w.prnt("", "pushbullet: /set plugins.var.python.weebullet.%s STRING" % option)

def send_push(title, body):

    apiurl = 'https://api.pushbullet.com/api/pushes'
    apikey = w.config_get_plugin("api_key")

    payload = {'type': 'note', 'title': title, 'body': body}

    r = requests.post(apiurl, data=payload, auth=(apikey, ''))

    if r.status_code is not 200:
	return False
    return True

def cmd_send_push_note(data, buffer, args):
    send_push(
            title="Manual Notification from weechat",
            body=args
            )
    return w.WEECHAT_RC_OK

def priv_msg_cb(data, bufferp, uber_empty, tagsn, isdisplayed,
        ishilight, prefix, message):
    """Sends highlighted message to be printed on notification"""

    am_away = w.buffer_get_string(bufferp, 'localvar_away')

    if not am_away:
	w.prnt("", "[weebullet] Not away, skipping notification")
	return w.WEECHAT_RC_OK

    notif_body = u"<%s> %s" % (
        prefix.decode('utf-8'),
	message.decode('utf-8')
    )

    # Check that it's in a "/q" buffer and that I'm not the one writing the msg
    is_pm = w.buffer_get_string(bufferp, "localvar_type") == "private"
    is_notify_private = re.search(r'(^|,)notify_private(,|$)', tagsn) is not None
    # PM (query)
    if (is_pm and is_notify_private):
	send_push(
		title="Privmsg from %s" % prefix.decode('utf-8'),
		body=notif_body
	)

    # Highlight (your nick is quoted)
    elif (ishilight == "1"):
        bufname = (w.buffer_get_string(bufferp, "short_name") or
                w.buffer_get_string(bufferp, "name"))
	send_push(
		title="Highlight in %s" % bufname.decode('utf-8'),
		body=notif_body
	)

    return w.WEECHAT_RC_OK

@@ -27,24 +27,8 @@
 ort 
-requests%0Aimport 
 json
@@ -37,16 +37,30 @@
 mport re
+%0Aimport urllib
 %0A%0Aimport
@@ -445,22 +445,41 @@
 )%0A%0Ac
-redentials = %7B
+onfigs = %7B%0A#    %22timeout%22: 20000,
 %0A   
@@ -530,17 +530,13 @@
 in c
-redential
+onfig
 s.it
@@ -669,16 +669,61 @@
 option)%0A
+        if type(default_value) == %22str%22:%0A    
         
@@ -805,150 +805,776 @@
 on)%0A
-%0Adef send_push(title, body):%0A%0A    apiurl = 'https://api.pushbullet.com/api/pushes'%0A    apikey = w.config_get_plugin(%22api_key%22)%0A%0A    payload = 
+        elif type(default_value) == %22int%22:%0A            w.prnt(%22%22, %22pushbullet: /set plugins.var.python.weebullet.%25s INT%22 %25 option)%0A        else:%0A            w.prnt(%22%22, %22pushbullet: /set plugins.var.python.weebullet.%25s VALUE%22 %25 option)%0A%0A%0Adef process_pushbullet_cb(data, url, return_code, response, err):%0A    # response is the string of http body%0A    if return_code == w.WEECHAT_HOOK_PROCESS_ERROR:%0A        w.prnt(%22%22, %22%5Bweebullet%5D Error sending to pushbullet: %25s - %25s%22 %25 (status, url))%0A        return w.WEECHAT_RC_ERROR%0A%0A    return w.WEECHAT_RC_OK%0A%0Adef send_push(title, body):%0A    apikey = w.config_get_plugin(%22api_key%22)%0A    apiurl = %22https://%25s:@api.pushbullet.com/api/pushes%22 %25 (apikey)%0A    timeout = 20000 # FIXME - actually use config%0A    payload = urllib.urlencode(
 %7B'ty
@@ -1619,136 +1619,120 @@
 ody%7D
-%0A
+)
 %0A    
-r = requests.post(apiurl, data=payload, auth=(apikey, ''))%0A%0A    if r.status_code is not 200:%0A%09return False%0A    return True
+w.hook_process_hashtable(%22url:%22 + apiurl, %7B %22postfields%22: payload %7D, timeout, %22process_pushbullet_cb%22, %22%22)
 %0A%0Ade

use hook_process_hashtable instead of request function. Less flexable as we can't check status code or anything (that i've been able to find), but is async