예제 #1
0
    def __init__(self, pb_token=None, JSON=None):
        self.devices = []
        self.sendlist = []

        if JSON is not None:
            self.pushbulletToken = JSON["pushbulletToken"]
            for deviceJSON in JSON["devices"]:
                self.devices += [
                    Pushbullet.PushbulletDevice(JSON=deviceJSON, Account=self)
                ]
                log(1,
                    "Added device to new instance: " + str(self.devices[-1]))
            self.sendlist = JSON["sendlist"]
        elif pb_token is not None:
            self.pushbulletToken = pb_token
        else:
            raise ValueError("Cannot initialize a Pushbullet object with \
                no Token or no JSON data")

        # Session object must always be initialized
        self.curSession = requests.session()
        self.curSession.headers.update(self.JSON_HEADER)
        authParam = "Bearer " + self.pushbulletToken
        self.curSession.headers.update({"Authorization": authParam})
        if not self.isTokenValid():
            raise ValueError("Invalid token (\"{}\")".format(
                self.pushbulletToken))
예제 #2
0
    def __init__(self, pb_token=None, JSON=None):
        self.devices = []
        self.sendlist = []

        if JSON is not None:
            self.pushbulletToken = JSON["pushbulletToken"]
            for deviceJSON in JSON["devices"]:
                self.devices += [
                    Pushbullet.PushbulletDevice(JSON=deviceJSON, Account=self)]
                log(1, "Added device to new instance: " +
                    str(self.devices[-1]))
            self.sendlist = JSON["sendlist"]
        elif pb_token is not None:
            self.pushbulletToken = pb_token
        else:
            raise ValueError("Cannot initialize a Pushbullet object with \
                no Token or no JSON data")

        # Session object must always be initialized
        self.curSession = requests.session()
        self.curSession.headers.update(self.JSON_HEADER)
        authParam = "Bearer " + self.pushbulletToken
        self.curSession.headers.update({"Authorization": authParam})
        if not self.isTokenValid():
            raise ValueError("Invalid token (\"{}\")".format(self.pushbulletToken))
예제 #3
0
 def getDevices(self, forceRefresh=False):
     if len(self.devices) == 0 or forceRefresh:
         try:
             response = self.curSession.get(self.PB_URL_DEVICES)
             if response.status_code == 401:
                 # Token is not a valid Pushbullet token
                 log(
                     1, "Error: invalid token ({})".format(
                         self.pushbulletToken))
                 return -3
             elif response.status_code != 200:
                 log(
                     1, "Error during getDevices request - HTTP " +
                     str(response.status_code))
                 return -2
             else:
                 for device in response.json()['devices']:
                     self.devices += [
                         self.PushbulletDevice(device, Account=self)
                     ]
                     return None
                 # return response.json()['devices'] # Really necessary ?
         except Exception as e:
             log(0, e.args)
             return -1
예제 #4
0
 def getDevices(self, forceRefresh=False):
     if len(self.devices) == 0 or forceRefresh:
         try:
             response = self.curSession.get(self.PB_URL_DEVICES)
             if response.status_code == 401:
                 # Token is not a valid Pushbullet token
                 log(1, "Error: invalid token ({})".format(self.pushbulletToken))
                 return -3
             elif response.status_code != 200:
                 log(1, "Error during getDevices request - HTTP " +
                     str(response.status_code))
                 return -2
             else:
                 for device in response.json()['devices']:
                     self.devices += [
                         self.PushbulletDevice(device, Account=self)]
                     return None
                 # return response.json()['devices'] # Really necessary ?
         except Exception as e:
             log(0, e.args)
             return -1