【小demo】——直播平臺自動發言

1. 背景

直播平臺火熱的現在,好多人已經開始直播致富了,但是很多直播新人因爲人氣等相關原因,就很難在直播平臺爆火,有的人想到了買號,刷人氣之類的,現在這款小demo就是配套的組件。

2. 前期準備

jar包

這裏寫圖片描述

3. 代碼

pps_ tv _view

package com.pochi.selenium;

import java.awt.EventQueue;

import javax.swing.JFrame;
import java.awt.Toolkit;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import java.awt.Color;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.SystemColor;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class PPS_TV_view {
    //這部分是最底下那個Frame框架
    private JFrame frame;
    private JTextField text_url;

    /**
     * Launch the application.
     */
    //這個main都是windowBuilder自己弄的,不用管
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    PPS_TV_view window = new PPS_TV_view();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     * 初始化
     */
    public PPS_TV_view() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     * 初始化frame
     */
    private void initialize() {
        frame = new JFrame();
        //使得窗口鎖死,不能最大化
        frame.setResizable(false);
        //窗口上的那個奇秀的圖標
        frame.setIconImage(Toolkit.getDefaultToolkit().getImage("E:\\Eclipse\\Documents\\Pro_PPS\\87258PICzud_1024.jpg"));
        //奇秀掛機精靈
        frame.setTitle("\u5947\u79C0\u6302\u673A\u7CBE\u7075");
        //窗口大小
        frame.setBounds(100, 100, 450, 188);
        //點右上角的叉可以關閉
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //沒有設layout,裏面的組件可以隨意拖動
        frame.getContentPane().setLayout(null);

        //創建一個文本框,用來輸入主播房間的地址
        text_url = new JTextField();
        //每次點到這個文本框,就把裏面的內容先清空一下
        text_url.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                text_url.setText("");
            }
        });
        //這個文本框的大小設定
        text_url.setBounds(196, 22, 228, 36);
        //將這個文本框添加到窗口上原有的那個Pane上
        frame.getContentPane().add(text_url);
        //設置文本框的長度
        text_url.setColumns(10);

        //創建一個button按鈕,上面寫“開始掛機”
        JButton button_start = new JButton("\u5F00\u59CB\u6302\u673A");
        //添加監聽器
        button_start.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                //1. 拿到別人填的主播房間的地址
                String anchor_url=text_url.getText();
                //2. 先健壯性判斷下有沒有人就沒寫
                if(anchor_url==null||anchor_url.equals("")){
                    //沒寫就在文本框提示不能爲空,然後這個方法就結束
                    text_url.setText("主播房間地址不能爲空!");
                    return;
                }
                //3. 創建PPS_sent這個類,把獲得的主播地址傳進去
                PPS_sent pps_sent = new PPS_sent(anchor_url);
                //4. 因爲這個類是實現了Runnable接口的,所以打開多線程,運行
                Thread t1=new Thread(pps_sent);
                t1.start();
            }
        });
        // 設置其大小
        button_start.setBounds(196, 93, 93, 23);
        // 將這個按鈕添加到pane上
        frame.getContentPane().add(button_start);

        // 另一個停止掛機的按鈕
        JButton button_stop = new JButton("\u505C\u6B62");
        // 監聽到點擊就直接退出
        button_stop.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                System.exit(0);
            }
        });
        // 設置大小
        button_stop.setBounds(318, 93, 93, 23);
        // 添加到pane
        frame.getContentPane().add(button_stop);

        //這部分是寫字的請輸入主播房間地址
        JTextPane textPane = new JTextPane();
        textPane.setBackground(SystemColor.control);
        textPane.setFont(new Font("宋體", Font.PLAIN, 14));
        textPane.setForeground(Color.BLACK);
        textPane.setText("\u8BF7\u8F93\u5165\u4E3B\u64AD\u623F\u95F4\u5730\u5740");
        textPane.setBounds(19, 30, 167, 48);
        frame.getContentPane().add(textPane);
    }
}

pps_sent

package com.pochi.selenium;

import java.util.Random;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class PPS_sent implements Runnable {
    //webdriver是Selenium有用
    private WebDriver driver;
    //anchor_url是get地址有用
    private String anchor_url;

    public PPS_sent(String anchor_url) {
        //在初始化的時候就判斷一下,地址是直接輸的數字還是,完整url,亦或是亂輸
        try {
            Integer.parseInt(anchor_url);
            this.anchor_url = "http://x.pps.tv/room/" + anchor_url;
        } catch (Exception e) {
            if (anchor_url.startsWith("http://x.pps.tv/room/")) {
                this.anchor_url = anchor_url;
            } else {
                throw new RuntimeException("別瞎jb亂寫");
            }
        }
    }

    public void run() {
        //由於我的firefox不是放在指定的位置的,所以這個也得要設置一下
        System.setProperty("webdriver.firefox.bin",
                "F:\\火狐瀏覽器 Firefox 20.0.1 獨木成林增強版\\火狐瀏覽器 Firefox 20.0.1 獨木成林增強版\\Mozilla\\Firefox\\firefox.exe");
        //打開瀏覽器
        driver = new FirefoxDriver();
        //輸入網址
        driver.get(anchor_url);
        //這條其實沒有人會看到
        System.out.println("開始登陸……限時40秒……");
        try {
            //等待40s,讓別人輸賬號、密碼登錄
            Thread.sleep(40000);
            while (true) {
                // 發消息
                sentMsg();
                // 送星光
                sentStart();
                // 送禮物
                sentGift();
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private void sentGift() throws InterruptedException {
        //點擊大白
        driver.findElement(By.cssSelector("img[alt=\"大白白\"]")).click();
        //點擊贈送
        driver.findElement(By.linkText("贈送")).click();
        //等待2s
        Thread.sleep(2000);
    }

    private void sentStart() throws InterruptedException {
        //點擊星光
        driver.findElement(By.cssSelector("a.free-gift")).click();
        //點擊1
        driver.findElement(By.xpath("//li[3]/a/strong")).click();
        //把星光關掉,爲了下次再點
        driver.findElement(By.cssSelector("a.free-gift")).click();
        //等待5s
        Thread.sleep(5000);
    }

    public void sentMsg() throws InterruptedException {
        //得到房間title
        String title = driver.getTitle();
        //這部分本來是要按照不同的主播名字,然後說話的,現在也沒這個需求了
        int start_index = title.indexOf("直播間");
        String anchorName = title.substring(start_index - 3, start_index);
        //這個也是調試代碼,真正的是看不到的
        System.out.println("開始發送信息...");
        //從這個數組裏面隨機拿出東西發送
        String[] str_arr = { "小龜最棒!", "麼麼噠!", "小龜好~", "吃飯沒?", "我很好!", "[開心]", "[色色]", "[呆]", "[哭]", "[大笑]", "[傻樂]",
                "[可憐]", "[無語]", "[汗]", "[抓狂]", "[親親]", "[落寞]", "[囧]", "[摳鼻]", "[閉嘴]", "[挑眉]", "[驚恐]", "[暈]", "[驚訝]",
                "[再見]" };
        //弄一個隨機對象
        Random random = new Random();
        //這兩個變量是爲了前後兩句話不一致
        int old_num = 0;
        int new_num = 0;

        for (int i = 0; i < 16; i++) {
            //得到一個新的隨機腳標
            new_num = random.nextInt(str_arr.length);
            //如果這個腳標和老的一樣的話就重新選一個
            while (old_num == new_num) {
                new_num = random.nextInt(str_arr.length);
            }
            //不一樣的話,把新的賦給老的
            old_num = new_num;
            // 把奇秀對話框的東西刪掉
            driver.findElement(By.cssSelector("div.say-input > input[type=\"text\"]")).clear();
            // 帶名字的發送語句
            // driver.findElement(By.cssSelector("div.say-input >
            // input[type=\"text\"]"))
            // .sendKeys(anchorName + str_arr[old_num]);
            // 不帶主播名字發送語句
            driver.findElement(By.cssSelector("div.say-input > input[type=\"text\"]")).sendKeys(str_arr[old_num]);
            // 發送消息
            driver.findElement(By.linkText("發言")).click();

            Thread.sleep(20000);
            // driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        }
    }

    public PPS_sent() {
        super();
    }
}

4.結果

這裏寫圖片描述

4.1 步驟

① 輸入房間號或地址皆可,點擊開始掛機。

② 打開如下頁面,在40s內完成登錄。

這裏寫圖片描述

③ 任由其掛機即可。

4.2 掛機內容

① 每20s,隨機發言。

② 每5分鐘點一個星光,然後送一個大白。

③ 周而復始…

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章