김서영

add search.js

1 +const rp = require("request-promise");
2 +const cheerio = require("cheerio");
3 +const Entities = require('html-entities').XmlEntities;
4 +const machineRead = require('./machineRead');
5 +
6 +const entities = new Entities();
7 +
8 +const keywordChecking = ( keywordText, $, elem ) => {
9 + let tempCheck = false;
10 + keywordText.split( ' ' ).forEach( ( Word ) => {
11 + if( $( elem ).text().indexOf( Word ) !== -1 ) {
12 + tempCheck = true;
13 + }
14 + });
15 +
16 + if( tempCheck ) {
17 + return true;
18 + }
19 + return false;
20 +}
21 +
22 +const naver = ( searchResult, $, elem , defaultURL ) => {
23 + searchResult.title = $( elem ).parent().attr( "title" );
24 + searchResult.passage = entities.decode( $( elem ).parent().parent().parent().text()).trim(),
25 + searchResult.url = $( elem ).parent().attr( "href" );
26 +
27 + if( searchResult.url === undefined ) {
28 + searchResult.url = defaultURL;
29 + }
30 +}
31 +
32 +const searchToResult = (searchResult, result, keywordCheck) => {
33 + searchResult.passage = searchResult.passage.replace( /(http(s)?:\/\/)([a-z0-9\w]+\.*)+[a-z0-9]{2,4}/gi, ' ' ).replace( /\s{1,}|\s{1,}|\r\n|\r|\n/g, ' ' ).trim();
34 +
35 + if( searchResult.title === undefined || !searchResult.title.length ) {
36 + searchResult.title = searchResult.passage.split(' ').slice( 0, 3 ).toString().replace(/,/g,' ') + "..";
37 + } else {
38 + searchResult.title = searchResult.title.replace( /(http(s)?:\/\/)([a-z0-9\w]+\.*)+[a-z0-9]{2,4}/gi, ' ' ).replace( /\s{1,}|\s{1,}|\r\n|\r|\n/g, ' ' ).trim();
39 + searchResult.passage = searchResult.passage.replace( searchResult.title, '' );
40 + }
41 +
42 + if( !result.length ) {
43 + if( keywordCheck ) {
44 + result.push( searchResult );
45 + }
46 + } else if( keywordCheck ) {
47 +}}
48 +
49 +const getHtmlMain = ( main, keywordText, html, defaultURL, findSearchResult ) => {
50 + const $ = cheerio.load( html );
51 + let result = [];
52 + $( main ).each( (i, elem ) => {
53 + let keywordCheck = keywordChecking( keywordText, $, elem );
54 + if( keywordCheck ) {
55 + let searchResult = {};
56 + findSearchResult( searchResult, $, elem , defaultURL );
57 + searchToResult( searchResult, result, keywordCheck );
58 + }
59 + });
60 + return result;
61 +}
62 +
63 +const search = {};
64 +
65 +search.naver = ( keywordText ) => {
66 + return new Promise( async ( resolve, reject ) => {
67 + let naverMain = "#main_pack strong",
68 + result = [],
69 + naverURL = "https://search.naver.com/search.naver?query=" + encodeURI( keywordText );
70 + rp( {
71 + "uri" : naverURL,
72 + } )
73 + .then( ( html ) => {
74 + result = getHtmlMain( naverMain, keywordText, html, naverURL, naver );
75 + resolve( result );
76 + })
77 + })
78 +}
79 +
80 +
81 +module.exports = search
...\ No newline at end of file ...\ No newline at end of file