% This buffer is for notes you don't want to save.
% If you want to create a file, visit that file with C-x C-f,
% then enter the text in that file's own buffer.

:- use_module(library('http/http_client')).
:- use_module(library('http/http_open')).

% Meu IP: 200.192.113.249
% Proxy PPGIA: 200.192.112.146

% http_open('http://www.folha.com.br',S,[proxy('200.192.112.146',3128)]),copy_stream_data(S,user_output).
% http_open('http://www.folha.com.br',S,[proxy('200.192.112.146',3128)]),read_stream_to_codes(S,Codes).
% http_open('http://www.google.com.br/search?&q=fabricio+enembreck&hl=en&lr=&ie=UTF-8&start=0&sa=N',S,[proxy('200.192.112.146',3128)]),read_stream_to_codes(S,Codes).

proxy('200.192.112.146',3128).

/*=====================================================
Retira todos os links de uma página-resultado do google
=======================================================*/
/*
Consulta do Google com respostas do Brasil
http://www.google.com.br/search?&q=fabricio+enembreck&hl=pt-BR&lr=&ie=UTF-8&start=0&sa=N

Consulta do Google com respostas de toda Web
http://www.google.com.br/search?&q=fabricio+enembreck&hl=en&lr=&ie=UTF-8&start=0&sa=N

<div class=g><a href="http://www.informatik.uni-trier.de/~ley/db/indices/a-tree/e/Enembreck:Fabr=iacute=cio.html" class=l>

*/

search_google(brasil,Words,N,Links) :-
	join_with_plus(Words,String),
	N1 is N / 10,
	name('http://www.google.com.br/search?&q=',L),
	append(L,String,L1),
	name('&hl=pt-BR&lr=&ie=UTF-8&start=',L2),
	append(L1,L2,L3),
	get_google_sites(L3,0,N1,[],Links),!.

search_google(_,Words,N,Links) :-
	join_with_plus(Words,String),
	N1 is N / 10,
	name('http://www.google.com.br/search?&q=',L),
	append(L,String,L1),
	name('&hl=en&lr=&ie=UTF-8&start=',L2),
	append(L1,L2,L3),
	get_google_sites(L3,0,N1,[],Links).

join_with_plus(List,String) :-
	join_words(List,[],String).

join_words([],String,String).
join_words([X],S,String) :-
	name(X,S1),
	append(S,S1,String).
join_words([X|Y],S,L) :-
	name(X,S1),
	append(S1,[43],S3),
	append(S,S3,S2),
	join_words(Y,S2,L).

get_google_sites(_,N,N,L,L).
get_google_sites(String_Words,N,N2,Link_list,Links2) :-
	N1 is N * 10,
	name(N1,S),
	append(String_Words,S,S1),
	append(S1,"&sa=N",S2),
	name(Url,S2),
	proxy(Proxy,Port),
	http_open(Url,I,[proxy(Proxy,Port)]),
	read_stream_to_codes(I,Codes),
%	get_links_from_google_page(Codes,Links),
	extrai_urls(Codes,Links),
	append(Link_list,Links,Links1),
	close(I),
	N3 is N + 1,
	get_google_sites(String_Words,N3,N2,Links1,Links2).

if(Condition, Then, _) :-
	Condition, !, Then.
if(_,_, Else) :-
	Else.

extrai_urls([],[]).
extrai_urls([Char|Chars],[Url|Urls]):-
	phrase(url_do_google(Url),[Char|Chars],Resto),
        extrai_urls(Resto,Urls),!.
extrai_urls([_|Chars],Urls):-
        extrai_urls(Chars,Urls).

url_do_google(Url)-->
	[118, 32, 99, 108, 97, 115, 115, 61, 103, 62, 60, 97, 32, 104, 114, 101, 102, 61, 34],
	url(Url),
	[34,32, 99, 108, 97, 115, 115, 61, 108, 62].
url(Url) -->
	[104, 116, 116, 112, 58, 47, 47],
	char_sequence(X),
	{atom_concat('http://',X,Url)}.

valid_char(X) -->
	[T],
	{name(X,[T]), X \= '<', X \= '>', X \= ' ', X\='"'}.
char_sequence(Z) -->
	valid_char(X),
	char_sequence(Y),
	{atom_concat(X,Y,Z)}.
char_sequence('') -->
	[].
	

/*=====================================================
	Para Trabalhar com Servidor HTTP
=====================================================*/

:- use_module(library('http/thread_httpd')).
:- use_module(library('http/http_dispatch')).
:- use_module(library('http/html_write')).

server(Port,Workers) :-
        http_server(http_dispatch, [port(Port),workers(Workers)]).

:- http_handler('/', root, []).
:- http_handler('/hello/world', hello_world, []).

root(_Request) :-
        reply_html_page([ title('Demo server')
                        ],
                        [ p(a(href('hello/world'), hello))
                        ]).

hello_world(_Request) :-
        reply_html_page([ title('Hello World')
                        ],
                        [ h1('Hello World'),
                          p('This is my first page')
                        ]).
