main.js 5.99 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 JASON = require('jason')
var test1 = require('./crawling_samename')
const fs = require('fs')
const test2 = require('./crawling_broadcasting')
const test3 = require('./crawling_br_onair_date')
const test4 = require('./crawling_time')
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 driver = new webdriver.Builder()
.forBrowser('chrome')
.withCapabilities(webdriver.Capabilities.chrome())
.setChromeOptions(Options)
.build();

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

var driver_3 = new webdriver.Builder()
.forBrowser('chrome')
.withCapabilities(webdriver.Capabilities.chrome())
.setChromeOptions(Options)
.build();
var First_Search = function(name) {
    var self=this;
    this.on('input', function() {
        
        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()
            console.log(next_link_Array)
            console.log(img_src_Array)
            
            setTimeout(function() {
                br_search.emit('click')
            },1000)
            
        }
        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(',');
                br_search = new broadcasting_search(next_link_Array,"유재석",0)
                console.log(next_link_Array)
                console.log(img_src_Array)
                driver.quit()
                setTimeout(function() {
                    br_search.emit('click')
                },1000)
            })
        }
    })
}
util.inherits(First_Search,EventEmitter);
first_search1 = new First_Search("유재석")
first_search1.emit('input')

var img_src_br_list = []
var title_list = []
var broadcasting_search = function(next_link,name,num) {
    var self=this;
    this.on('click', function() { 
        if(fs.existsSync("./log/"+name+"_img_src_br.txt")) {
            const img_src_br = fs.readFileSync("./log/"+name+"_img_src_br.txt");
            img_src_br_list = img_src_br.toString().split(',');
            const title_br = fs.readFileSync("./log/"+name+"_title_br.txt");
            title_list = title_br.toString().split(',');
            driver_1.quit()
            console.log(img_src_br_list)
            console.log(title_list)
            is_onair.emit('click_1')
        }
        else {
            test2.search_broadcasting(driver_1,next_link[num].substr(9),name,function() {
                const img_src_br = fs.readFileSync("./log/"+name+"_img_src_br.txt");
                img_src_br_list = img_src_br.toString().split(',');
                const title_br = fs.readFileSync("./log/"+name+"_title_br.txt");
                title_list = title_br.toString().split(',');
                
                driver_1.quit()
                console.log(img_src_br_list)
                console.log(title_list)
                is_onair.emit('click_1')
            })
        }
    })
}
util.inherits(broadcasting_search,EventEmitter);
br_search = new broadcasting_search(next_link_Array,"유재석",0)

br_datalist = []
var isonair = function(num) {
    var self = this
    this.on('click_1',function() {
        if(fs.existsSync("./log/"+title_list[num]+"_br_isonair.txt")) {
            const img_src_br = fs.readFileSync("./log/"+title_list[num]+"_br_isonair.txt");
            br_datalist = img_src_br.toString().split(',');
            br_datalist[0] = br_datalist[0].trim()
            console.log(br_datalist)
            Search_detail_time.emit('click_2')
        }

        else {
            test3.search_onairanddate(driver_2,title_list[num],function() {
                const img_src_br = fs.readFileSync("./log/"+title_list[num]+"_br_isonair.txt");
                br_datalist = img_src_br.toString().split(',');
                br_datalist[0] = br_datalist[0].trim()
                console.log(br_datalist)
                Search_detail_time.emit('click_2')
            })
        }
    })
}

util.inherits(isonair,EventEmitter);
is_onair = new isonair(11)

var detail_time_list = []
var search_detail_time = function(num) {
    var self = this;
    this.on('click_2',function() {
        if(br_datalist[1] == "방영중") {
            if(fs.existsSync("./log/"+title_list[num]+"_time_detail.txt")) {
                const detail = fs.readFileSync("./log/"+title_list[num]+"_time_detail.txt");
                detail_time_list = detail.toString().split(',');
                console.log(detail_time_list)
            }
            else {
                test4.search_broadcasting_time(driver_3,br_datalist[0],br_datalist[2],title_list[num],function(){
                        const detail = fs.readFileSync("./log/"+title_list[num]+"_time_detail.txt");
                        detail_time_list = detail.toString().split(',');
                        console.log(detail_time_list)
                })

            }
        }
    })
}

util.inherits(search_detail_time,EventEmitter);
Search_detail_time = new search_detail_time(11);