김서영

add search.js search browser

...@@ -5,6 +5,11 @@ const machineRead = require('./machineRead'); ...@@ -5,6 +5,11 @@ const machineRead = require('./machineRead');
5 5
6 const entities = new Entities(); 6 const entities = new Entities();
7 7
8 +const searchURL = {
9 + "naver" : "https://search.naver.com/search.naver?",
10 + "google" : "https://www.google.com/search?"
11 +}
12 +
8 const keywordChecking = ( keywordText, $, elem ) => { 13 const keywordChecking = ( keywordText, $, elem ) => {
9 let tempCheck = false; 14 let tempCheck = false;
10 keywordText.split( ' ' ).forEach( ( Word ) => { 15 keywordText.split( ' ' ).forEach( ( Word ) => {
...@@ -19,6 +24,20 @@ const keywordChecking = ( keywordText, $, elem ) => { ...@@ -19,6 +24,20 @@ const keywordChecking = ( keywordText, $, elem ) => {
19 return false; 24 return false;
20 } 25 }
21 26
27 +const google = ( searchResult, $, elem , defaultURL ) => {
28 + searchResult.passage = entities.decode( $( elem ).parent().parent().parent().text()).trim(),
29 + searchResult.url = decodeURIComponent( $( elem ).attr( "href" ) );
30 + searchResult.title = entities.decode( $( elem ).children("div").text() ); // title 캐오기 수정 가능
31 +
32 + if( searchResult.url.indexOf( "/url?q=" ) === 0 ) {
33 + searchResult.url = searchResult.url.replace( "/url?q=", "" );
34 + } else if( searchResult.url.indexOf( "/search?" ) === 0 ) {
35 + searchResult.url = "https://google.com" + searchResult.url;
36 + } else {
37 + searchResult.url = defaultURL;
38 + }
39 +}
40 +
22 const naver = ( searchResult, $, elem , defaultURL ) => { 41 const naver = ( searchResult, $, elem , defaultURL ) => {
23 searchResult.title = $( elem ).parent().attr( "title" ); 42 searchResult.title = $( elem ).parent().attr( "title" );
24 searchResult.passage = entities.decode( $( elem ).parent().parent().parent().text()).trim(), 43 searchResult.passage = entities.decode( $( elem ).parent().parent().parent().text()).trim(),
...@@ -44,6 +63,7 @@ const searchToResult = (searchResult, result, keywordCheck) => { ...@@ -44,6 +63,7 @@ const searchToResult = (searchResult, result, keywordCheck) => {
44 result.push( searchResult ); 63 result.push( searchResult );
45 } 64 }
46 } else if( keywordCheck ) { 65 } else if( keywordCheck ) {
66 +
47 }} 67 }}
48 68
49 const getHtmlMain = ( main, keywordText, html, defaultURL, findSearchResult ) => { 69 const getHtmlMain = ( main, keywordText, html, defaultURL, findSearchResult ) => {
...@@ -66,7 +86,7 @@ search.naver = ( keywordText ) => { ...@@ -66,7 +86,7 @@ search.naver = ( keywordText ) => {
66 return new Promise( async ( resolve, reject ) => { 86 return new Promise( async ( resolve, reject ) => {
67 let naverMain = "#main_pack strong", 87 let naverMain = "#main_pack strong",
68 result = [], 88 result = [],
69 - naverURL = "https://search.naver.com/search.naver?query=" + encodeURI( keywordText ); 89 + naverURL = searchURL.naver + "query=" + encodeURI( keywordText );
70 rp( { 90 rp( {
71 "uri" : naverURL, 91 "uri" : naverURL,
72 } ) 92 } )
...@@ -77,5 +97,20 @@ search.naver = ( keywordText ) => { ...@@ -77,5 +97,20 @@ search.naver = ( keywordText ) => {
77 }) 97 })
78 } 98 }
79 99
100 +search.google = ( keywordText ) => {
101 + return new Promise( ( resolve, reject ) => {
102 + let googleMain = "#main a",
103 + result = [],
104 + googleURL = searchURL.google + "q=" + encodeURI( keywordText )
105 +
106 + rp( {
107 + "uri" : googleURL,
108 + })
109 + .then( ( html ) => {
110 + result = getHtmlMain( googleMain, keywordText, html, googleURL, google );
111 + resolve( result );
112 + })
113 + })
114 +}
80 115
81 -module.exports = search
...\ No newline at end of file ...\ No newline at end of file
116 +module.exports = search;
...\ No newline at end of file ...\ No newline at end of file
......