本文整理汇总了Python中pushbullet.PushBullet.new_device方法的典型用法代码示例。如果您正苦于以下问题:Python PushBullet.new_device方法的具体用法?Python PushBullet.new_device怎么用?Python PushBullet.new_device使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pushbullet.PushBullet
的用法示例。
在下文中一共展示了PushBullet.new_device方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Mirrorer
# 需要导入模块: from pushbullet import PushBullet [as 别名]
# 或者: from pushbullet.PushBullet import new_device [as 别名]
class Mirrorer(object):
def __init__(self, auth_key, temp_folder, device_name, last_push = time.time(), device_iden=None):
self.temp_folder = temp_folder
if not os.path.exists(self.temp_folder):
os.makedirs(temp_folder)
self._auth_key = auth_key
self.pb = PushBullet(self._auth_key)
self.listener = Listener(self.pb, self.watcher)
self.last_push = last_push
self.device = None
if device_iden:
results = [d for d in self.pb.devices if d.device_iden == device_iden and d.active]
self.device = results[0] if results else None
if not self.device:
try:
device = self.pb.new_device(device_name)
print("Created new device:",device_name,"iden:",device.device_iden)
self.device = device
except:
print("Error: Unable to create device")
raise
self.check_pushes()
def save_icon(self, b64_asset):
hash = hashlib.md5(b64_asset.encode()).hexdigest()
path = os.path.join(self.temp_folder, hash)
if os.path.exists(path):
return path
else:
decoded = base64.b64decode(b64_asset)
with open(path, "wb") as image:
image.write(decoded)
return path
def check_pushes(self):
pushes = self.pb.get_pushes(self.last_push)
for push in pushes:
if not isinstance(push,dict):
# not a push object
continue
if ((push.get("target_device_iden", self.device.device_iden) == self.device.device_iden) and not (push.get("dismissed", True))):
self.notify(push.get("title", ""), push.get("body", ""))
self.pb.dismiss_push(push.get("iden"))
self.last_push = max(self.last_push, push.get("created"))
def watcher(self, push):
if push["type"] == "push" and push["push"]["type"] == "mirror":
print("MIRROR")
image_path = self.save_icon(push["push"]["icon"])
self.notify(push["push"]["title"],
push["push"]["body"], image_path)
elif push["type"] == "tickle":
print("TICKLE")
self.check_pushes()
def notify(self, title, body, image=None):
subprocess.Popen(["notify-send", title, body, "-i", image or ""])
print(title)
print(body)
def dump_config(self, path):
config = {"temp_folder": self.temp_folder,
"auth_key": self._auth_key,
"device_name": self.device.nickname,
"device_iden": self.device.device_iden}
with open(path, "w") as conf:
json.dump(config, conf)
def run(self):
try:
self.listener.run_forever()
except KeyboardInterrupt:
self.listener.close()
示例2: Downloader
# 需要导入模块: from pushbullet import PushBullet [as 别名]
# 或者: from pushbullet.PushBullet import new_device [as 别名]
class Downloader(object):
def __init__(self, auth_key, device_name, last_push = time.time(), device_iden=None):
self._auth_key = auth_key
self.pb = PushBullet(self._auth_key)
self.listener = Listener(self.pb, self.download_link)
self.last_push = last_push
self.device = None
if device_iden:
results = [d for d in self.pb.devices if d.device_iden == device_iden and d.active]
self.device = results[0] if results else None
if not self.device:
try:
device = self.pb.new_device(device_name)
print("Created new device:",device_name,"iden:",device.device_iden)
self.device = device
except:
print("Error: Unable to create device")
raise
def get_unique_filename(self, filename, downloads_dir):
unique_filename = filename
i = 0
while os.path.isfile(downloads_dir+"/"+unique_filename):
i+=1
unique_filename=filename+" (%i)" % i
return unique_filename
def get_filename_from_url(self, url):
if url.rfind('/') != -1:
title = url[url.rfind('/')+1:]
if len(title) < 1:
try:
response = requests.get(url)
parsed_body = html.fromstring(response.text)
titles = parsed_body.xpath('//title/text()')
if len(titleArray) == 0:
title="tmptitle"
else:
title=titles[0]
except:
title="tmptitle"
return title
def download_link(self, push):
pushes = self.pb.get_pushes(self.last_push)[1]
if (len(pushes) != 0):
last_push = pushes[len(pushes)-2]
if last_push.get("url"):
url = last_push.get("url")
filename = self.get_filename_from_url(url)
downloads_dir = glib.get_user_special_dir(glib.USER_DIRECTORY_DOWNLOAD)
unique_filename = self.get_unique_filename(filename, downloads_dir)
unique_filename_full_path = downloads_dir+"/"+unique_filename
urllib.urlretrieve (url, unique_filename_full_path)
print "URL: %s Downloaded: %s" % (url, unique_filename_full_path)
self.last_push = max(self.last_push, last_push.get("created"))
def run(self):
try:
self.listener.run_forever()
except KeyboardInterrupt:
self.listener.close()
def dump_config(self, path):
config = {"auth_key": self._auth_key,
"device_name": self.device.nickname,
"device_iden": self.device.device_iden}
with open(path, "w") as conf:
json.dump(config, conf)