From 58681bd7f3aeb1fd222a044e47c3da923d2fe039 Mon Sep 17 00:00:00 2001 From: zm-cttae <114668551+zm-cttae@users.noreply.github.com> Date: Sun, 22 Oct 2023 12:56:51 +0100 Subject: [PATCH 1/5] Adopt official REST API (#84) Replaces HTML parsing from #75 --- mpm.m | 40 ++++++++-------------------------------- 1 file changed, 8 insertions(+), 32 deletions(-) diff --git a/mpm.m b/mpm.m index b2d9fd9..b9f6aa1 100644 --- a/mpm.m +++ b/mpm.m @@ -374,46 +374,22 @@ function listPackages(opts) end % query file exchange - baseUrl = 'http://www.mathworks.com/matlabcentral/fileexchange'; - html = webread(baseUrl, 'q', query); - - % extract all hrefs from '

' - htmlTreePath = fullfile( ... - 'toolbox', 'textanalytics', ... - 'textanalytics', '@htmlTree', 'htmlTree.m' ... - ); - if strcmp(strrep(which('htmlTree'), matlabroot(), ''), ['', filesep, htmlTreePath]) - selector = 'h3 a[href^="/matlabcentral/fileexchange"]'; - subtrees = findElement(htmlTree(html), selector); - href = getAttribute(subtrees, 'href'); - htmlText = extractHTMLText(subtrees); - tokens = cell(0, 0); - for ii = 1:numel(subtrees) - tokens{ii} = { ... - strrep(href{ii}, '/matlabcentral/fileexchange', ''), ... - htmlText{ii} ... - }; - end - else - expr = '

[^<]*([^"]*)'; - tokens = regexp(html, expr, 'tokens'); - end + apiUrl = 'https://api.mathworks.com/community/v1/search'; + response = webread(apiUrl, 'query', query, 'scope', 'file-exchange'); % if any packages contain package name exactly, return that one - for ii = 1:numel(tokens) - curName = lower(strrep(strrep(tokens{ii}{2}, '', ''), '', '')); - if ~isempty(strfind(curName, lower(query))) - url = [baseUrl ... - strrep(tokens{ii}{1}, '/matlabcentral/fileexchange', '') ... - '&download=true']; + for ii = 1:numel(response.items) + item = response.items{ii} + if ~isempty(strfind(lower(query), lower(item.description))) + url = [item.url '?download=true']; return; end end % return first result if ~isempty(tokens) - url = tokens{1}{1}; - url = [baseUrl url '&download=true']; + url = response.items{1}.url; + url = [url '?download=true']; % urlFormat = @(aid, ver) [ ... % 'https://www.mathworks.com/' ... % 'matlabcentral/mlc-downloads/downloads/submissions/' aid ... From 2554b45711402c1368fa2dda646be87159d85fb1 Mon Sep 17 00:00:00 2001 From: Jay H Date: Sun, 22 Oct 2023 14:20:41 -0400 Subject: [PATCH 2/5] Update mpm.m added semicolon at end of line --- mpm.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mpm.m b/mpm.m index b9f6aa1..4c0c847 100644 --- a/mpm.m +++ b/mpm.m @@ -379,7 +379,7 @@ function listPackages(opts) % if any packages contain package name exactly, return that one for ii = 1:numel(response.items) - item = response.items{ii} + item = response.items{ii}; if ~isempty(strfind(lower(query), lower(item.description))) url = [item.url '?download=true']; return; From 99f3e989d3042be0910f4b893db2c59378060bda Mon Sep 17 00:00:00 2001 From: Jay H Date: Sun, 22 Oct 2023 14:23:09 -0400 Subject: [PATCH 3/5] Update mpm.m changing ref tokens to response.items --- mpm.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mpm.m b/mpm.m index 4c0c847..f3ea0ee 100644 --- a/mpm.m +++ b/mpm.m @@ -387,7 +387,7 @@ function listPackages(opts) end % return first result - if ~isempty(tokens) + if ~isempty(response.items) url = response.items{1}.url; url = [url '?download=true']; % urlFormat = @(aid, ver) [ ... From cbc2e5d087314bc3984d4e33eeff3e4c80bf4b86 Mon Sep 17 00:00:00 2001 From: Jay H Date: Sun, 22 Oct 2023 14:33:01 -0400 Subject: [PATCH 4/5] Update mpm.m response.items is struct not cell --- mpm.m | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mpm.m b/mpm.m index f3ea0ee..6811f7f 100644 --- a/mpm.m +++ b/mpm.m @@ -379,8 +379,8 @@ function listPackages(opts) % if any packages contain package name exactly, return that one for ii = 1:numel(response.items) - item = response.items{ii}; - if ~isempty(strfind(lower(query), lower(item.description))) + item = response.items(ii); + if ~isempty(strfind(lower(query), lower(item.title))) url = [item.url '?download=true']; return; end @@ -388,7 +388,7 @@ function listPackages(opts) % return first result if ~isempty(response.items) - url = response.items{1}.url; + url = response.items(1).url; url = [url '?download=true']; % urlFormat = @(aid, ver) [ ... % 'https://www.mathworks.com/' ... From 1df666ca6d10c0e7141b45435a4f4ac597987d09 Mon Sep 17 00:00:00 2001 From: zm-cttae <114668551+zm-cttae@users.noreply.github.com> Date: Mon, 23 Oct 2023 12:58:11 +0100 Subject: [PATCH 5/5] Update search result URL for #85 --- test/test_install.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_install.m b/test/test_install.m index 6f3c946..f86afe1 100644 --- a/test/test_install.m +++ b/test/test_install.m @@ -149,7 +149,7 @@ %%% Test search results = evalc('mpm search export_fig'); assert(contains(results, ... - 'Found url: http://www.mathworks.com/matlabcentral/fileexchange/23629-export_fig?s_tid=srchtitle&download=true')) + 'Found url: https://www.mathworks.com/matlabcentral/fileexchange/23629-export_fig?download=true')) pause(GITHUB_SEARCH_RATELIMIT);