通过Python向Android发送PushBullet通知
大家好,
我在reddit上看到一个关于用Python和Pushbullet的讨论,觉得这个功能挺实用的,所以我想用Python通过Pushbullet发送通知,但遇到了一些问题。
1) 我找不到每个设备ID对应的是哪个设备……
2) 我无法向任何设备发送内容,因为出现了一个错误:AttributeError: 'NoneType' object has no attribute 'push_note'
PushBullet.py的链接是:https://pypi.python.org/pypi/pushbullet.py/0.4.1
项目的Github地址是:https://github.com/randomchars/pushbullet.py
我运行了以下代码:
from pushbullet import PushBullet
pb = PushBullet("ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890")
print pb.devices
phone = pb.get("1234567890")
print phone
push = phone.push_note("This is the title", "This is the body")
print(push.status_code)
返回结果是:
[Device('ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890', 1234567890), Device('ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890', 0000000000), Device('ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890', 1111111111), Device('ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890', 2222222222)]
None
Traceback (most recent call last):
File "C:\Users\Admin\Desktop\PushBullet_Test.py", line 9, in <module>
push = phone.push_note("This is the title", "This is the body")
AttributeError: 'NoneType' object has no attribute 'push_note'
请注意,他们的文档在push_note的示例中有一个语法错误,结束括号前不应该有句号。
我到处找都找不到这个问题的解决办法,甚至找不到有相同问题的人 :(
2 个回答
我又做了一个用Python写的命令行工具,它可以让你快速方便地发送电子邮件、Pushover和Pushbullet通知。
你可以在这里找到源代码和文档:
https://github.com/ltpitt/python-simple-notifications
为了简单起见,我在这里也放一些相关信息:
简单通知
简单通知是一个跨平台的命令行工具,可以轻松发送电子邮件(也可以附带附件)和推送通知(使用 Pushover 或 Pushbullet)
要求
- Python:如果你使用的是Linux或Mac,那就没问题,可以直接跳到下一步。如果你在Windows上,并且喜欢简单方便,可以通过几次点击在这里安装Python:http://ninite.com
- Python Pip:这里有 安装说明。
如何安装
安装好Python和Python Pip后:
$ git clone https://github.com/ltpitt/python-simple-notifications.git
$ cd python-simple-notifications
$ pip install .
然后根据需要的电子邮件/Pushbullet/Pushover配置数据,定制simple_notifications_config.py文件。如果你阅读simple_notifications_config.py中的注释,会很容易理解。
Windows 10上使用Python 2.7的simple_notification_config.py文件路径示例:
C:\Python27\Lib\site-packages\simple_notifications\simple_notifications_config.py
Windows 10上使用Python 3.7的simple_notification_config.py文件路径示例:
C:\Users\YOUR_USER\AppData\Local\Programs\Python\Python37\Lib\site-packages\simple_notifications
Linux上simple_notification_config.py文件路径示例:
/usr/local/lib/python2.7/dist-packages/simple_notifications/simple_notifications_config.py
最后一步,记得把simple_notification_config.py文件的权限设置为只有运行脚本的用户可以读取。
在Windows上,右键点击文件,选择属性,然后在权限选项卡中进行设置,具体说明可以参考这里:
https://msdn.microsoft.com/en-us/library/bb727008.aspx
在Linux上:
$ chmod 400 /usr/local/lib/python2.7/dist-packages/simple_notifications/simple_notifications_config.py
使用方法
以下是如何显示帮助信息:
$ simple-notifications --help
输出:
Usage: simple-notifications [OPTIONS] COMMAND [ARGS]...
Simple Notifications sends out email and push notifications from your
applications (using Pushbullet or Pushover)
Options:
--help Show this message and exit.
Commands:
email Send a notification using Email
pushbullet Send a notification using Pushbullet
pushover Send a notification using Pushover
今天我也遇到了同样的问题。
你可以使用这个Azelphur开发的库:
https://github.com/Azelphur/pyPushBullet
效果非常好。
把导入的部分改成:
"from pushbullet2 import PushBullet"
把这个库的文件名保存为"pushbullet2.py",这样可以避免和"pushbullet.py"冲突(记得把新库放在同一个文件夹里)。确保你下载所需的依赖项,所有依赖都可以通过pip获取,具体在Read Me里有说明。
from pushbullet2 import PushBullet
pb = PushBullet("ABCDEFGHIJKLMOPQRSTUVWXYZ")
devices = pb.getDevices()
phone = devices[0]["iden"] #change number to change device
#print devices
print phone
push = pb.pushNote(phone,"This is title", "This is the body")