# Buggy code
def send_push(
    self, task, api_key, title, body, url=None, destination=None, destination_type=None
):
    if url:
        push_type = "link"
    else:
        push_type = "note"

    data = {"type": push_type, "title": title, "body": body}
    if url:
        data["url"] = url
    if destination:
        data[destination_type] = destination

    # Check for test mode
    if task.options.test:
        log.info("Test mode. Pushbullet notification would be:")
        log.info("    API Key: %s" % api_key)
        log.info("    Type: %s" % push_type)
        log.info("    Title: %s" % title)
        log.info("    Body: %s" % body)
        if destination:
            log.info("    Destination: %s (%s)" % (destination, destination_type))
        if url:
            log.info("    URL: %s" % url)
        log.info("    Raw Data: %s" % json.dumps(data))
        # Test mode.  Skip remainder.
        return

    # Make the request
    headers = {
        "Authorization": "Basic %s" % base64.b64encode(api_key),
        "Content-Type": "application/json",
        "Accept": "application/json",
        "User-Agent": "Flexget",
    }
    response = task.requests.post(
        pushbullet_url, headers=headers, data=json.dumps(data), raise_status=False
    )

    # Check if it succeeded
    request_status = response.status_code

    # error codes and messages from Pushbullet API
    if request_status == 200:
        log.debug("Pushbullet notification sent")
    elif request_status == 500:
        log.warning("Pushbullet notification failed, Pushbullet 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("Pushbullet API error: %s" % error["message"])
    else:
        log.error("Unknown error when sending Pushbullet notification")


# Traceback (TypeError)
2016-06-06 03:30 CRITICAL task          anime-series    BUG: Unhandled error in plugin pushbullet: a bytes-like object is required, not 'str'
Traceback (most recent call last):
File "/usr/local/lib/python3.5/site-packages/flexget/task.py", line 443, in __run_plugin
return method(*args, **kwargs)
File "/usr/local/lib/python3.5/site-packages/flexget/event.py", line 23, in __call__
return self.func(*args, **kwargs)
File "/usr/local/lib/python3.5/site-packages/flexget/plugins/output/pushbullet.py", line 98, in on_task_output
self.send_push(task, apikey, title, body, url, device, 'device_iden')
File "/usr/local/lib/python3.5/site-packages/flexget/plugins/output/pushbullet.py", line 134, in send_push
'Authorization': 'Basic %s' % base64.b64encode(api_key),
File "/usr/local/lib/python3.5/base64.py", line 62, in b64encode
encoded = binascii.b2a_base64(s)[:-1]
TypeError: a bytes-like object is required, not 'str'

# Fixed code
def send_push(
    self, task, api_key, title, body, url=None, destination=None, destination_type=None
):
    if url:
        push_type = "link"
    else:
        push_type = "note"

    data = {"type": push_type, "title": title, "body": body}
    if url:
        data["url"] = url
    if destination:
        data[destination_type] = destination

    # Check for test mode
    if task.options.test:
        log.info("Test mode. Pushbullet notification would be:")
        log.info("    API Key: %s" % api_key)
        log.info("    Type: %s" % push_type)
        log.info("    Title: %s" % title)
        log.info("    Body: %s" % body)
        if destination:
            log.info("    Destination: %s (%s)" % (destination, destination_type))
        if url:
            log.info("    URL: %s" % url)
        log.info("    Raw Data: %s" % json.dumps(data))
        # Test mode.  Skip remainder.
        return

    # Make the request
    headers = {
        "Authorization": "Basic %s" % base64.b64encode(api_key.encode("ascii")),
        "Content-Type": "application/json",
        "Accept": "application/json",
        "User-Agent": "Flexget",
    }
    response = task.requests.post(
        pushbullet_url, headers=headers, data=json.dumps(data), raise_status=False
    )

    # Check if it succeeded
    request_status = response.status_code

    # error codes and messages from Pushbullet API
    if request_status == 200:
        log.debug("Pushbullet notification sent")
    elif request_status == 500:
        log.warning("Pushbullet notification failed, Pushbullet 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("Pushbullet API error: %s" % error["message"])
    else:
        log.error("Unknown error when sending Pushbullet notification")