same_name_search.js 2.36 KB
var async = require('async');
var webdriver = require('selenium-webdriver');
//var options = {desiredCapabilities: {browserName: 'chrome'}};
var chrome = require('selenium-webdriver/chrome')
var Options = new chrome.Options();
Options.addArguments('headless')
Options.addArguments('disable-gpu')
var test1 = require('./crawling_samename')
const fs = require('fs')
var util = require('util')
var EventEmitter = require('events').EventEmitter;

var next_link_Array  = []
var img_src_Array = []

const {Builder, By, Key, until} = require('selenium-webdriver');

var result_arr = []
module.exports = {
    First_Search :function(name) {

        var driver = new webdriver.Builder()
        .forBrowser('chrome')
        .withCapabilities(webdriver.Capabilities.chrome())
        //.setChromeOptions(Options)
        .build();

        return new Promise(function(resolve,reject) {
            if(fs.existsSync("./log/"+name+"_next_link.txt")) {
                const next_link = fs.readFileSync("./log/"+name+"_next_link.txt");
                const img_src = fs.readFileSync("./log/"+name+"_img_src.txt");
                next_link_Array = next_link.toString().split(',');
                img_src_Array = img_src.toString().split(',');
                //driver.quit()
                result_arr.push(next_link_Array)
                result_arr.push(img_src_Array)

                resolve(result_arr)
            }
            else {
                test1.first_search(driver,name,function() {
                    const next_link = fs.readFileSync("./log/"+name+"_next_link.txt");
                    const img_src = fs.readFileSync("./log/"+name+"_img_src.txt");
                    next_link_Array = next_link.toString().split(',');
                    img_src_Array = img_src.toString().split(',');
                    result_arr.push(next_link_Array)
                    result_arr.push(img_src_Array)
                    //driver.quit()
                    resolve(result_arr)
                })
            }

        })

    }
//util.inherits(First_Search,EventEmitter);
//first_search1 = new First_Search("유재석")
//first_search1.emit('input')
}



//다른 파일에서 호출하는 방법
//var a=require("./sane_name_search")

//var arr = []

//a.First_Search("(검색어 입력)").then(aa => {
//    arr=a
//    console.log(arr)
//})