From f3888aa2ccfffabd0985ad86d1d1c017f62202c7 Mon Sep 17 00:00:00 2001 From: Jens Brauer Date: Thu, 28 Apr 2016 11:51:02 +0200 Subject: [PATCH] Use the new web functions from new Matlab version Using webwrite instead of urlread etc. Matlab is also able to handle json data now. webwrite was introduced in Matlab 2015a. --- Pushbullet.m | 50 ++++++++------ README.md | 7 +- json_parser.m | 184 -------------------------------------------------- 3 files changed, 30 insertions(+), 211 deletions(-) delete mode 100644 json_parser.m diff --git a/Pushbullet.m b/Pushbullet.m index 6df85a7..5ca877f 100644 --- a/Pushbullet.m +++ b/Pushbullet.m @@ -12,7 +12,7 @@ % p.pushFile(device_iden, file_name, file_type, file_url) to push a % file which has already been uploaded % - % Copyright 2014, Jens Brauer, https://github.com/jensb89 + % Copyright 2016, Jens Brauer, https://github.com/jensb89 properties HOST = 'https://api.pushbullet.com/v2' @@ -29,17 +29,23 @@ function self = Pushbullet(apikey) self.ApiKey = apikey; + if verLessThan('MATLAB','8.5') + warning(['You are using a Matlab version prior to 2015a',... + 'Matlab-Pushbullet might not work with this version.',... + 'Update Matlab or use an older version of Matlab-Pushbullet',... + 'from here: https://github.com/jensb89/Matlab-Pushbullet/releases/']); + end end function load_devices(self) % Get a list of devices - output = urlread([self.HOST,self.DEVICES_URL],... - 'Authentication','Basic',... - 'Username',self.ApiKey,'Get',{}); - output_converted = json_parser(output); - self.Devices = output_converted{1}.devices; + options = weboptions('KeyName','Access-Token','KeyValue',self.ApiKey); + output = webread([self.HOST,self.DEVICES_URL],options); + self.Devices = output.devices; for i=1:length(self.Devices) - sprintf('%s : %s', self.Devices{1}.nickname, self.Devices{1}.iden) + if self.Devices{i}.active && isfield(self.Devices{i},'nickname') + sprintf('%s : %s', self.Devices{i}.nickname, self.Devices{i}.iden) + end end end @@ -52,13 +58,13 @@ function load_devices(self) % title -- a title for the note % body -- the body of the note - data = {'type', 'note',... - 'device_iden',device_iden,... - 'title', title,... - 'body', message}; + data = struct('body',message,... + 'device_iden',device_iden,... + 'title',title,... + 'type','note'); if isempty(device_iden) - data(3:4) = []; %delete device_iden in data -> push to all connected devices + data = rmfield(data,'device_iden'); end output = push(self, data); @@ -75,14 +81,14 @@ function load_devices(self) % 'image/png') % file_url -- the url of the file - data = {'type','file',... + data = struct('type','file',... 'device_iden',device_iden,... 'file_name',file_name,... 'file_type',file_type,... - 'file_url',file_url}; + 'file_url',file_url); if isempty(device_iden) - data(3:4) = []; %delete device_iden in data -> push to all connected devices + data = rmfield(data,'device_iden'); %delete device_iden in data -> push to all connected devices end output = push(self, data); @@ -96,14 +102,14 @@ function load_devices(self) % title -- a title for the note % body -- the body of the note - data = {'type', 'note',... + data = struct('type', 'note',... 'device_iden',device_iden,... 'title', title,... 'body', message,... - 'url', url}; + 'url', url); if isempty(device_iden) - data(3:4) = []; %delete device_iden in data -> push to all connected devices + data = rmfield(data,'device_iden'); %delete device_iden in data -> push to all connected devices end output = push(self, data); @@ -111,10 +117,10 @@ function load_devices(self) function output = push(self, data) % Perform the POST Request - output = urlread([self.HOST,self.PUSH_URL],... - 'Authentication','Basic',... - 'Username',self.ApiKey,... - 'Post',data); + options = weboptions('KeyName','Access-Token',... + 'KeyValue',self.ApiKey,... + 'MediaType','application/json'); + output = webwrite([self.HOST,self.PUSH_URL],data,options) end % HELPER FUNCTIONS diff --git a/README.md b/README.md index ad646ec..4b7ebb3 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ In order to use the API you need an API key that can be obtained ![Matlab Pushbullet Notification on iPhone](matlab-pushbullet-ios.png) +Important: The Matlab class now uses the new webwrite and webread functions from Matlab. The webwrite was introduced in Matlab R2015a. If you are using an older matlab version, use the older release of Matlab-Pushbullet [here](https://github.com/jensb89/Matlab-Pushbullet/releases/). #Usage @@ -59,8 +60,4 @@ Furthermore `device_iden` can be an e-mail adress. License ------- -MIT license. See LICENSE for full text. - -Acknowledgement -------- -[JSON-Parser](http://www.mathworks.com/matlabcentral/fileexchange/20565-json-parser) is used to parse the json output. \ No newline at end of file +MIT license. See LICENSE for full text. \ No newline at end of file diff --git a/json_parser.m b/json_parser.m deleted file mode 100644 index e462697..0000000 --- a/json_parser.m +++ /dev/null @@ -1,184 +0,0 @@ -function [data json] = parse_json(json) -% [DATA JSON] = PARSE_JSON(json) -% This function parses a JSON string and returns a cell array with the -% parsed data. JSON objects are converted to structures and JSON arrays are -% converted to cell arrays. -% -% Example: -% google_search = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=matlab'; -% matlab_results = parse_json(urlread(google_search)); -% disp(matlab_results{1}.responseData.results{1}.titleNoFormatting) -% disp(matlab_results{1}.responseData.results{1}.visibleUrl) - - data = cell(0,1); - - while ~isempty(json) - [value json] = parse_value(json); - data{end+1} = value; %#ok - end -end - -function [value json] = parse_value(json) - value = []; - if ~isempty(json) - id = json(1); - json(1) = []; - - json = strtrim(json); - - switch lower(id) - case '"' - [value json] = parse_string(json); - - case '{' - [value json] = parse_object(json); - - case '[' - [value json] = parse_array(json); - - case 't' - value = true; - if (length(json) >= 3) - json(1:3) = []; - else - ME = MException('json:parse_value',['Invalid TRUE identifier: ' id json]); - ME.throw; - end - - case 'f' - value = false; - if (length(json) >= 4) - json(1:4) = []; - else - ME = MException('json:parse_value',['Invalid FALSE identifier: ' id json]); - ME.throw; - end - - case 'n' - value = []; - if (length(json) >= 3) - json(1:3) = []; - else - ME = MException('json:parse_value',['Invalid NULL identifier: ' id json]); - ME.throw; - end - - otherwise - [value json] = parse_number([id json]); % Need to put the id back on the string - end - end -end - -function [data json] = parse_array(json) - data = cell(0,1); - while ~isempty(json) - if strcmp(json(1),']') % Check if the array is closed - json(1) = []; - return - end - - [value json] = parse_value(json); - - if isempty(value) - ME = MException('json:parse_array',['Parsed an empty value: ' json]); - ME.throw; - end - data{end+1} = value; %#ok - - while ~isempty(json) && ~isempty(regexp(json(1),'[\s,]','once')) - json(1) = []; - end - end -end - -function [data json] = parse_object(json) - data = []; - while ~isempty(json) - id = json(1); - json(1) = []; - - switch id - case '"' % Start a name/value pair - [name value remaining_json] = parse_name_value(json); - if isempty(name) - ME = MException('json:parse_object',['Can not have an empty name: ' json]); - ME.throw; - end - data.(name) = value; - json = remaining_json; - - case '}' % End of object, so exit the function - return - - otherwise % Ignore other characters - end - end -end - -function [name value json] = parse_name_value(json) - name = []; - value = []; - if ~isempty(json) - [name json] = parse_string(json); - - % Skip spaces and the : separator - while ~isempty(json) && ~isempty(regexp(json(1),'[\s:]','once')) - json(1) = []; - end - [value json] = parse_value(json); - end -end - -function [string json] = parse_string(json) - string = []; - while ~isempty(json) - letter = json(1); - json(1) = []; - - switch lower(letter) - case '\' % Deal with escaped characters - if ~isempty(json) - code = json(1); - json(1) = []; - switch lower(code) - case '"' - new_char = '"'; - case '\' - new_char = '\'; - case '/' - new_char = '/'; - case {'b' 'f' 'n' 'r' 't'} - new_char = sprintf('\%c',code); - case 'u' - if length(json) >= 4 - new_char = sprintf('\\u%s',json(1:4)); - json(1:4) = []; - end - otherwise - new_char = []; - end - end - - case '"' % Done with the string - return - - otherwise - new_char = letter; - end - % Append the new character - string = [string new_char]; %#ok - end -end - -function [num json] = parse_number(json) - num = []; - if ~isempty(json) - % Validate the floating point number using a regular expression - [s e] = regexp(json,'^[\w]?[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?[\w]?','once'); - if ~isempty(s) - num_str = json(s:e); - json(s:e) = []; - num = str2double(strtrim(num_str)); - end - end -end \ No newline at end of file