김서영

add search.js search browser

......@@ -5,6 +5,11 @@ const machineRead = require('./machineRead');
const entities = new Entities();
const searchURL = {
"naver" : "https://search.naver.com/search.naver?",
"google" : "https://www.google.com/search?"
}
const keywordChecking = ( keywordText, $, elem ) => {
let tempCheck = false;
keywordText.split( ' ' ).forEach( ( Word ) => {
......@@ -19,6 +24,20 @@ const keywordChecking = ( keywordText, $, elem ) => {
return false;
}
const google = ( searchResult, $, elem , defaultURL ) => {
searchResult.passage = entities.decode( $( elem ).parent().parent().parent().text()).trim(),
searchResult.url = decodeURIComponent( $( elem ).attr( "href" ) );
searchResult.title = entities.decode( $( elem ).children("div").text() ); // title 캐오기 수정 가능
if( searchResult.url.indexOf( "/url?q=" ) === 0 ) {
searchResult.url = searchResult.url.replace( "/url?q=", "" );
} else if( searchResult.url.indexOf( "/search?" ) === 0 ) {
searchResult.url = "https://google.com" + searchResult.url;
} else {
searchResult.url = defaultURL;
}
}
const naver = ( searchResult, $, elem , defaultURL ) => {
searchResult.title = $( elem ).parent().attr( "title" );
searchResult.passage = entities.decode( $( elem ).parent().parent().parent().text()).trim(),
......@@ -44,6 +63,7 @@ const searchToResult = (searchResult, result, keywordCheck) => {
result.push( searchResult );
}
} else if( keywordCheck ) {
}}
const getHtmlMain = ( main, keywordText, html, defaultURL, findSearchResult ) => {
......@@ -66,7 +86,7 @@ search.naver = ( keywordText ) => {
return new Promise( async ( resolve, reject ) => {
let naverMain = "#main_pack strong",
result = [],
naverURL = "https://search.naver.com/search.naver?query=" + encodeURI( keywordText );
naverURL = searchURL.naver + "query=" + encodeURI( keywordText );
rp( {
"uri" : naverURL,
} )
......@@ -77,5 +97,20 @@ search.naver = ( keywordText ) => {
})
}
search.google = ( keywordText ) => {
return new Promise( ( resolve, reject ) => {
let googleMain = "#main a",
result = [],
googleURL = searchURL.google + "q=" + encodeURI( keywordText )
rp( {
"uri" : googleURL,
})
.then( ( html ) => {
result = getHtmlMain( googleMain, keywordText, html, googleURL, google );
resolve( result );
})
})
}
module.exports = search
\ No newline at end of file
module.exports = search;
\ No newline at end of file
......