-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgoogle.m
142 lines (118 loc) · 4.25 KB
/
google.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
function meanq = google(varargin)
%GOOGLE searches varargin at google
% google.m is a shell interface to initiate Google search and display
% first few results in Matlab Command Window. The function treats varargin
% as the query word(s) and uses regular expression to analyze the text
% returned by Google. One of the fun parts is you can type typos, since
% Google usually knows what is right from wrong.
%
% google.m keeps cache so to prevent bugging Google from same query. it
% displays Cache when it is. Users are allowed to clear the cache.
%
% Examples:
% google mathworks
% google matlab file xechange
% google matlab chaowei chen
%
% Chao-Wei Chen. 3/30/2013
q=google_query(varargin);
if isempty(q),return;end
url=q2url(q);
txt=google_urlread(url,q); % read from google or cached
meanq = google_did_you_mean(txt);
%urls=regexp(txt,'<a href=".*?(http://.*?)&.*?>(.*?)</a>','tokens')';
urls=regexp(txt,'<a href=".*?q=(http://.*?)&.*?>(.*?)</a>.*?<cite>(.*?)</cite>.*?<span class="st">(.*?)</span>','tokens')';
displaylink(urls);
end
function url=q2url(q)
config=load_google_config;
if isempty(config.key),
url=['https://www.google.com/search?q=' q ];
else
url=['https://www.google.com/search?q=' q '&key="' config.key '"'];
end
end
function q = google_did_you_mean(txt)
q=regexp(txt,'<a class="spell" href=".*?q=(.*?)&','tokens');
if ~isempty(q)
q=q{1};q=q{1}; url=q2url(q);
dsp=['Showing results for <a href="matlab:google(''' q ''')">' q '</a>'];
% disp(dsp);
end
end
function txt=google_urlread(url,q)
cache=['googlecache_q=' q '.html'];
loc=which(cache);
path=fileparts(which('google.m'));
if isempty(loc)
loc=fullfile(path,cache);
urlwrite(url,loc); % save cache
else
loc_rm=fullfile(path,'googlecache*.html');
% disp(['---Cache <a href="matlab:delete(''' loc_rm ''')">(clear?)</a>---']);
end
loc=['file:///' strrep(loc,'\','/')]; % Windows style
txt=urlread(loc);
end
function displaylink(urls)
num=0;
for k=1:numel(urls)
url=urls{k};
% has problem in k=1
link=url{1};string=url{2};cite=url{3};description=url{4};
num=num+1;
string=regexprep(string,'</?br?>','');
cite=regexprep(cite,'</?br?>','');
description=regexprep(description,'</?br?>','');
dsp=[num2str(num) ': <a href="matlab:web(''' link ''',''-browser'')">' string '</a>'];
disp(dsp);
if ~isempty(which('cprintf.m')) % for stylish output
pause(0.02);
cprintf('comment',['\t' cite '\n']);pause(0.01);
cprintf('text',['\t' description '\n']);pause(0.01);
disp(' ');
end
end
end
function q=google_query(v)
q=[];
qn=numel(v);
switch(qn)
case 0, fprintf('no query key word\n');
case 1, q=v{1};
otherwise,
while(numel(v)>=1)
q=[q v{1} '+'];
v=v(2:end);
end
q=strrep(q,' ','');
q=q(1:end-1);
end
end
function config=load_google_config
path=fileparts(which('google.m'));
matname=fullfile(path,'google.config.mat');
if exist(matname,'file'),
load(matname,'config');
else
uicontrol('style','text','fontsize',14,...
'units','normalized','position',[0 0.6 1 0.3],'string',{'config google.m for the first time.','(delete google.config.mat to reappear this message)'});
uicontrol('style','pushbutton','fontsize',14,...
'units','normalized','position',[0 0.3 1 0.3],'string','get Google API key, but none is fine currently',...
'callback',@callback_get_google_API_key);
uicontrol('style','pushbutton','fontsize',14,...
'units','normalized','position',[0 0 1 0.3],'string','download cprintf.m for stylish output',...
'callback',@callback_download_cprintf);
config.key='';
save(matname,'config');
end
%%
function callback_get_google_API_key(obj,event)
web('https://developers.google.com/custom-search/v1/getting_started#auth','-browser');
config.key=cell2mat(inputdlg('Google API access key:','Enter your key',1,{'AIzaSyCj1-7uMW-KOD_xTWAm_u_4X561gnRMMMg'}));
save(matname,'config');
end
function callback_download_cprintf(obj,event)
web('http://www.mathworks.com/matlabcentral/fileexchange/24093-cprintf-display-formatted-colored-text-in-the-command-window','-browser');
end
end