使用nodejs+puppeteer+mysql+electron+vue等解决自动化弹幕之虎牙直播

核心代码:

1、发送消息代码:

const startHySendOne = async function(href,contsOne) {
    stateData.href = href;
    stateData.contsOne = contsOne;
    window.scHy = 0;
    async.series(
        [
            async function(callback) {
                console.log("---getSetBeanSet---"+sd.format(new Date(), 'YYYY-MM-DD HH:mm'));
                await mysql.pool.query('SELECT * from beanSet', function(err, rows, fields) {
                    if (err) throw err;
                    stateData.beanSet= rows[0];
                    callback(null,"getSetBeanSet");
                });
            },
            async function(callback) {
                console.log("---getSetuserinfos---"+sd.format(new Date(), 'YYYY-MM-DD HH:mm'));
                await mysql.pool.query('SELECT * from userinfos where ptKey="虎牙" limit 10;',async function(err, rows, fields) {
                    if (err) throw err;
                    var jsonstr =await eval(rows);
                    stateData.userinfos = jsonstr;
                    callback(null,"getSetuserinfos");
                });
            },
            async function(callback) {
                console.log("---getCookies---"+sd.format(new Date(), 'YYYY-MM-DD HH:mm'));
                try {
                    await mysql.pool.query('SELECT * from cookies where ptKey="虎牙"',async function(err, rows, fields) {
                        if (err) throw err;
                        if(rows[0]){
                            var jsonstr = await eval(rows[0].cookies);
                            stateData.cookies = jsonstr;
                            callback(null,"getCookies");
                        }else{
                            var jsonstr ="";
                            stateData.cookies = jsonstr;
                            callback(null,"getCookies");
                        }
                        //mysql.pool.releaseConnection();
                    });        
                } catch (error) {
                    console.log("no cookies in db;");
                }
            },
            async function(callback) {
                console.log("---getSetcontexts---"+sd.format(new Date(), 'YYYY-MM-DD HH:mm'));
                await mysql.pool.query('SELECT * from contexts where ptKey="虎牙" limit 10;',async function(err, rows, fields) {
                    if (err) throw err;
                    stateData.contexts = rows;
                    callback(null,"getSetcontexts");
                    //mysql.pool.releaseConnection();
                });
            },
            // sendOne(href,contsOne)
            async function(callback) {
                var href = stateData.href;
                var contsOne = stateData.contsOne;

                if(contsOne){
                    alert("确认发送房间链接:"+href+",发送内容:"+contsOne+",发送次数:"+stateData.beanSet.sendNum);
                }else{
                    alert("确认发送房间链接:"+href+",发送内容:默认列表,发送次数:"+stateData.beanSet.sendNum);
                }
            
                console.log("---sendOne---"+sd.format(new Date(), 'YYYY-MM-DD HH:mm'));
                (async () => {
                    const browser = await puppeteer.launch({
                        // headless: false,
                        headless: window.headlessFlag,
                        executablePath: window.chromeExe,
                    });
                        var page = await browser.newPage();
                        if(stateData.cookies){
                            stateData.loginMode = 'C';
                            console.log("cookies 登录");
                            stateData.cookies.forEach(cookie => {
                                page.setCookie(cookie);                    
                            });
                        }else{
                        } 
                        await page.goto(href);   
                        await page.setViewport({
                            width:1024,
                            height:768
                        });        
                        //进入room
                        if(!contsOne) {
                            var i = 0;
                            for (const context of stateData.contexts) {
                                i= i+1;
                                var index = 1;
                                await console.log(index);
                                // sendCountHy == index*i*5;
                                console.log(index*i+":index*i;");
                                console.log(context.conts+"->发送中");
                                var  logsAddSql = 'INSERT INTO logs(ptKey,num,contx,href) VALUES("虎牙",?,?,?)';
                                var  logsAddSql_Params = [stateData.log.num, stateData.log.contx ,stateData.log.href];
                                await mysql.pool.query(logsAddSql,logsAddSql_Params, function(err, rows, fields) {
                                    if (err) throw err;
                                });
                                await page.waitFor(6000); 
                                var sn = [];
                                for(var i=1;i<=stateData.beanSet.sendNum;i++){
                                    sn.push(i);
                                }                    
                                // var sn = ['1','2','3','4','5'];
                                for (const s of sn) {
                                    window.scHy = window.scHy+1;                        
                                    console.log("hy:"+window.scHy);
                                    if(window.parseSendState == "parse"){
                                        window.parseSend = setTimeout(function(){
                                            window.parseSendState = "start"
                                        },24*60*1000);  
                                    }
                                    if(window.parseSendState == "stop"){
                                        return ;
                                    }
                                    await page.type('#pub_msg_input',context.conts);
                                    await page.waitFor(3000);
                                    console.log("4"+new Date());
                                    await page.click('#msg_send_bt');
                                    await page.waitFor(3000);
                                }
                                await page.waitFor(30000);           
                                await page.close();
                            }
                        }else{
                            var i = 0;
                            // for (const context of stateData.contexts) {
                                i= i+1;
                                var index = 1;
                                await console.log(index);
                                // sendCountHy == index*i*5;
                                console.log(index*i+":index*i;");
                                console.log(contsOne+"->发送中");
                                var  logsAddSql = 'INSERT INTO logs(ptKey,num,contx,href) VALUES("虎牙",?,?,?)';
                                var  logsAddSql_Params = [stateData.log.num, contsOne ,page.url()];
                                await mysql.pool.query(logsAddSql,logsAddSql_Params, function(err, rows, fields) {
                                    if (err) throw err;
                                });
                                await page.waitFor(6000); 
                                var sn = [];
                                for(var i=1;i<=stateData.beanSet.sendNum;i++){
                                    sn.push(i);
                                }                    
                                // var sn = ['1','2','3','4','5'];
                                for (const s of sn) {
                                    window.scHy = window.scHy+1;                        
                                    console.log("hy:"+window.scHy);
                                    if(window.parseSendState == "parse"){
                                        window.parseSend = setTimeout(function(){
                                            window.parseSendState = "start"
                                        },24*60*1000);  
                                    }
                                    if(window.parseSendState == "stop"){
                                        return ;
                                    }
                                    await page.type('#pub_msg_input',contsOne);
                                    await page.waitFor(3000);
                                    console.log("4"+new Date());
                                    await page.click('#msg_send_bt');
                                    await page.waitFor(3000);
                                }
                                await page.waitFor(30000);           
                                await page.close();
                            // }
                        }
                        await browser.close().then(callback(null,"sendone"));
                })();
            }
        ], function(err,results) {
            console.log("results:"+results);
            console.log("Nasa over and out");
        });    
}

2、mysql模块代码:

var mysql = require('mysql');

//创建连接池
var pool  = mysql.createPool({
    // host: 'localhost',
    host: '47.99.89.71',    
    user: 'root',
    password: '123456',
    database: 'danmu'
});

//监听connection事件
pool.on('connection', function(connection) {  
    connection.query('SET SESSION auto_increment_increment=1'); 
    console.log("mysql连接成功...IP:" + connection.config.host);
});

exports.pool = pool;

效果:

 

演示视频:

4.1 指定房间多线程发送消息-虎牙直播:

https://pan.baidu.com/s/1248rsbMOGu5rMsBVIFZgbw

4.2 指定房间多线程发送消息-斗鱼直播:

https://pan.baidu.com/s/1ZNZj7Ua23NyKwl-7Gp9qYw

4.3 指定房间多线程发送消息-熊猫直播:

https://pan.baidu.com/s/1oQfspfqvjNzl5DAjPSuwWA

4.4 指定房间多线程发送消息-龙珠直播:

https://pan.baidu.com/s/1wrQ6hBzXWwCZosOTY-SfTA

4.5 指定房间多线程发送消息-六房间:

coding.....

                        

                                                                             土豪光荣榜

 

Logo

前往低代码交流专区

更多推荐