Newer
Older
global nQueryCount nCacheCount strServer
if isempty(strServer)
strServer='wikipedia'
end
idx=strfind(term,' ');
term(idx)='+';
%check cache
global QueryCache
cc=strcmp(QueryCache.Queries,term);
if ~isempty(cc) && any(cc)
idx=find(cc);
f=QueryCache.Count(idx);
nCacheCount=nCacheCount+1;
return
end
if strcmp(strServer,'pubmed')
f=doQueryPubmed(term);
elseif strcmp(strServer,'amazon')
f=doQueryAmazon(term);
else
f=doQueryWikipedia(term);
end
nQueryCount=nQueryCount+1;
QueryCache.Queries=[QueryCache.Queries {term}];
QueryCache.Count=[QueryCache.Count f];
end
function f=doQueryWikipedia(term)
URL=['http://en.wikipedia.org/w/index.php?title=Special%3ASearch&profile=default&search=' term '+&fulltext=Search'];
idx=strfind(str,'div class="results-info"');
idx=idx(1);
str2=str(idx:end);
idx2=strfind(str2,'of <b>');
str3=str2(idx2+6:end);
idx3=strfind(str3,'</b>');
idx3=idx3(1);
nx=str3(1:idx3-1);
f=str2double(nx);
catch
f=0;
end
function f=doQueryPubmed(term)
URL = [ 'http://www.ncbi.nlm.nih.gov/gquery/?term=' term];
str = urlread(URL);
try
key = '<h2>About ';
idx1=strfind(str,key);
idx1=idx1+length(key);
idx2=strfind(str(idx1:end), 'search results for');
nx=str(idx1:idx1+idx2-2);
f=str2double(nx);
catch
f=0;
end
end
function f=doQueryAmazon(term)
URL=['http://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Daps&field-keywords=' term '&rh=i%3Aaps%2Ck%3A' term];
for i=1:5
str = urlread(URL);
try
%amazon
idx=idx(1)-2;
idxStart=idx;
chPrev = str(idxStart-1);
while(isNumeric(chPrev))
idxStart=idxStart-1;
end
nx=str(idxStart:idx);
f=str2double(nx);
break;
catch
f=0;
pause(1);
end
end
end
function bNumber = isNumeric(ch)
bNumber=1;
if ~isnan(str2double(ch))
return
end
if ch==','
return
end
bNumber=0;
end