crawling_broadcasting.js 8.19 KB
var async = require('async');
var webdriver = require('selenium-webdriver');
var options = {desiredCapabilities: {browserName: 'chrome'}};
var onair = require('./crawling_br_onair_date')
const {Builder, By, Key, until} = require('selenium-webdriver');
const fs = require('fs')
const screen = {
    width: 640,
    height: 480
};
var a=8000
var img_src_list = []
var text_list = []
var next_link_list = []
var testStartRunTime = 0;
var testruntime = 0;
var time = 0;
module.exports = {
    search_broadcasting : function(driver,url,name,callback) {
        function testRunTimer() {
            var today = new Date(); // 현재시간 얻기
            var runTime = today.getTime(); // 밀리세컨드 ( 1970/01/01 부터 현재까지의 시간을 밀리세컨드로 나타냄 )
            var rtn = 0;
        
            if (testStartRunTime == 0) {
                      testStartRunTime = runTime;
            } else {
                      rtn = (runTime - testStartRunTime) / 1000;
                      testStartRunTime = 0;
            }
            return rtn;
        } 
        driver
            .get("http://"+url)
            .then(() => {
                driver.findElement(webdriver.By.id('pagination_76'))
                .then ( paginationBtn => {
                    paginationBtn.findElements(webdriver.By.className('bt_next'))
                    .then(Btn_next => {
                        driver.findElement(webdriver.By.id('listUI_76'))
                        .then(contentsUI => {
                            clickandget(function() {
                                Btn_next[0].click()
                                setTimeout(() => {
                                    clickandget(function() {
                                        Btn_next[0].click()
                                        setTimeout(() => {
                                            clickandget(function() {
                                                Btn_next[0].click()
                                                setTimeout(() => {
                                                    clickandget(function(){
                                                        Btn_next[0].click()
                                                        setTimeout(() => {
                                                            clickandget(function(){
                                                                fs.writeFileSync("./log/"+name+"_img_src_br.txt", '\ufeff' + img_src_list, {encoding: 'utf8'});
                                                                fs.writeFileSync("./log/"+name+"_title_br.txt", '\ufeff' + text_list, {encoding: 'utf8'});
                                                                setTimeout(() => {
                                                                    callback()
                                                                }, 500);
                                                            })
                                                        }, testruntime*1000+800); 
                                                    })
                                                }, testruntime*1000+800);

                                            },testruntime*1000+800)
                                        }, testruntime*1000+800);
                                    })
                                }, testruntime*1000+800);
                            })
                            function clickandget(_callback) {
                                testRunTimer()
                                Btn_next[0].isDisplayed().then(function(state) {
                                    if(state) {
                                        contentsUI.findElements(webdriver.By.tagName('li'))
                                        .then(elems => {
                                            elems.forEach(elem => {
                                                elem.findElement(webdriver.By.className('thmb'))
                                                .then(thmb => {
                                                    thmb.getAttribute('href')
                                                    .then(next_link => {
                                                        thmb.findElements(webdriver.By.tagName('img'))
                                                        .then(img => {
                                                            img[0].getAttribute('src')
                                                            .then(img_src => {
                                                                elem.findElements(webdriver.By.className('tit'))
                                                                .then (titles => {
                                                                    titles.forEach(title => {
                                                                        title.getText()
                                                                        .then(text => {
                                                                            text_list.push(text)
                                                                            img_src_list.push(img_src)
                                                                            next_link_list.push(next_link)
                                                                            if(text_list.length % 5 == 0) {
                                                                                var testruntime = testRunTimer();
                                                                                time = time+testruntime*1000
                                                                                _callback()
                                                                            }
                                                                        })
                                                                    })
                                                                })
                                                            })
                                                        })
                                                    })
                                                })
                                            })
                                        })
                                    }
                                    else {
                                        fs.writeFileSync("./log/"+name+"_img_src_br.txt", '\ufeff' + img_src_list, {encoding: 'utf8'});
                                        fs.writeFileSync("./log/"+name+"_title_br.txt", '\ufeff' + text_list, {encoding: 'utf8'});
                                        fs.writeFileSync("./log/"+name+"_next_link_br.txt", '\ufeff' + next_link_list, {encoding: 'utf8'});
                                        setTimeout(() => {
                                            callback()
                                        }, 500);
                                    }
                                },function(err) {
                                    fs.writeFileSync("./log/"+name+"_img_src_br.txt", '\ufeff' + img_src_list, {encoding: 'utf8'});
                                    fs.writeFileSync("./log/"+name+"_title_br.txt", '\ufeff' + text_list, {encoding: 'utf8'});
                                    fs.writeFileSync("./log/"+name+"_next_link_br.txt", '\ufeff' + next_link_list, {encoding: 'utf8'});
                                    setTimeout(() => {
                                        callback()
                                    }, 500);
                                })
                            }
                        })
                    })
                })
            })
        function sleep (time) {
            return new Promise((resolve) => setTimeout(resolve, time));
        }
    }
}

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

//search_broadcasting(driver,"people.search.naver.com/search.naver?where=nexearch&sm=tab_ppn&query=유재석&os=94702&ie=utf8&key=PeopleService","유재석",function() {
//    console.log("B")
//})