Skip to main content

A simple python client for pushbullet.com

Project description

https://img.shields.io/travis/randomchars/pushbullet.py.svg?style=flat-square https://img.shields.io/coveralls/randomchars/pushbullet.py.svg?style=flat-square https://img.shields.io/pypi/dm/pushbullet.py.svg?style=flat-square:targethttps://pypi.python.org/pypi?name=pushbullet.py&:action=display https://img.shields.io/pypi/v/pushbullet.py.svg?style=flat-square:targethttps://pypi.python.org/pypi?name=pushbullet.py&:action=display https://img.shields.io/pypi/l/pushbullet.py.svg

This is a python library for the wonderful Pushbullet service. It allows you to send push notifications to Android and iOS devices.

In order to use the API you need an API key that can be obtained here. This is user specific and is used instead of passwords.

Installation

The easiest way is to just open your favorite terminal and type

pip install pushbullet.py

Alternatively you can clone this repo and install it with

python setup.py install

Requirements

  • The wonderful requests library.

  • The magical python-magic library.

Usage

Authentication

from pushbullet import Pushbullet

pb = Pushbullet(api_key)

If your key is invalid (that is, the Pushbullet API returns a 401), an InvalidKeyError is raised.

Pushing things

Pushing a text note

push = pb.push_note("This is the title", "This is the body".)

push is a dictionary containing the data returned by the Pushbullet API.

Pushing an address

address = " 25 E 85th St, 10028 New York, NY"
push = pb.push_address("home", address)

Pushing a list

to_buy = ["milk", "bread", "cider"]
push = phone.push_list("Shopping list", to_buy)

Pushing a file

Pushing files is a two part process. First you need to upload the file, and after that you can push it like you would anything else.

with open("my_cool_picture.jpg", "rb") as pic:
    file_data = pb.upload_file(pic, "picture.jpg")

push = pb.push_file(**file_data)

upload_file returns a dictionary containing file_type, file_url and file_name keys. These are the same parameters that push_file take.

The advantage of this is that if you already have a file uploaded somewhere, you can use that instead of uploading again. For example:

push = pb.push_file(file_url="https://i.imgur.com/IAYZ20i.jpg", file_name="cat.jpg", file_type="image/jpeg")

Working with pushes

You can also view all previous pushes:

pushes = pb.get_pushes()

Pushes is a list containig dictionaries that have push data. You can use this data to dismiss notifications or delete pushes.

latest = pushes[0]

# We already read it, so let's dismiss it
pb.dismiss_push(latest.get("iden"))

# Now delete it
pb.delete_push(latest.get("iden"))

Both of these raise PushbulletError if there’s an error.

Pushing to specific devices

So far all our pushes went to all connected devices, but there’s a way to limit that.

First we need to get hold of some devices.

# Get all devices that the current user has access to.
print(pb.devices)
# [Device('Motorola Moto G'), Device('N7'), Device('Chrome')]

motog = pb.devices[0]

Now we can use the device objects like we did with pb:

push = motog.push_note("Hello world!", "We're using the api.")

Alternatively we can pass the device to push methods:

push = pb.push_note("Hello world!", "We're using the api.", device=motog)

Creating new devices

Creating a new device is easy too, you only need to specify a name for it.

listener = pb.new_device("Listener")

Now you can use it like any other device.

Editing devices

You can change the nickname, the manufacturer and the model of the device:

listener = pb.edit_device(listener, make="Python", model="3.4.1")
motog = pb.edit_device(motog, nickname="My MotoG")

Deleting devices

Of course, you can also delete devices, even those not added by you.

pb.remove_device(listener)

A PushbulletError is raised on error.

Channels

You can also send pushes to channels. First, create a channel on the Pushbullet website (also make sure to subscribe to that channel). All channels which belong to the current user can be retrieved as follows:

# Get all channels created by the current user
print(pb.channels)
# [Channel('My Channel' 'channel_identifier')]

my_channel = pb.channels[0]

Then you can send a push to all subscribers of this channel like so:

push = my_channel.push_note("Hello Channel!", "Hello My Channel")

Note that you can only push to channels which have been created by the current user.

Contacts

Contacts work just like devices:

# Get all contacts the user has
print(pb.contacts)
# [Contact('Peter' <peter@gmail.com>), Contact('Sophie' <sophie@gmail.com>]

sophie = pb.contacts[1]

Now we can use the contact objects like we did with pb or with the devices.:

push = sophie.push_note("Hello world!", "We're using the api.")

# Or:
push = pb.push_note("Hello world!", "We're using the api.", contact=sophie)

Adding new contacts

bob = pb.new_contact("Bob", "bob@gmail.com")

Editing contacts

You can change the name of any contact:

bob = pb.edit_contact(bob, "bobby")

Deleting contacts

pb.remove_contact(bob)

Sending SMS messages

This is untested and uses an undocumented API. Use with caution.

..code:: python

device = pb.devices[0] push = pb.push_sms(device, “+3612345678”, “Wowza!”)

Error checking

If the Pushbullet api returns an error code a PushError an __ InvalidKeyError or a PushbulletError is raised. The first __ two are both subclasses of PushbulletError

The pushbullet api documetation contains a list of possible status codes.

TODO

  • More tests. Write them all.

License

MIT license. See LICENSE for full text.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pushbullet.py-0.8.1.tar.gz (8.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pushbullet.py-0.8.1-py2.py3-none-any.whl (8.6 kB view details)

Uploaded Python 2Python 3

File details

Details for the file pushbullet.py-0.8.1.tar.gz.

File metadata

  • Download URL: pushbullet.py-0.8.1.tar.gz
  • Upload date:
  • Size: 8.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for pushbullet.py-0.8.1.tar.gz
Algorithm Hash digest
SHA256 2f6c915f447850ba19b9de95fef6adaaa8746f8856a331e02954fca1a0886c97
MD5 1e8eccd95062df199830d92a29c57bdf
BLAKE2b-256 a57c106aea74854454257fd9a98e87137bcb4d4b659c1a62e3ebfcfa1ad1c73c

See more details on using hashes here.

File details

Details for the file pushbullet.py-0.8.1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for pushbullet.py-0.8.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 4eceaebed50061487fe72a84344d04b80489c7eb525040fcfa42c5558813510c
MD5 8f25da4f267d3bc2ae38ec5b2fa37706
BLAKE2b-256 4e0ced912e53b807a34ae67ffec30c23381fc29b3d59fe79a07fbc5630c50c5b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page