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() {@Overridepublic 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() {@Overridepublic 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() {@Overridepublic void mouseClicked(MouseEvent e) {System.exit(0);}});// 设置大小button_stop.setBounds(318, 93, 93, 23);// 添加到paneframe.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();//等待2sThread.sleep(2000);}private void sentStart() throws InterruptedException {//点击星光driver.findElement(By.cssSelector("a.free-gift")).click();//点击1driver.findElement(By.xpath("//li[3]/a/strong")).click();//把星光关掉,为了下次再点driver.findElement(By.cssSelector("a.free-gift")).click();//等待5sThread.sleep(5000);}public void sentMsg() throws InterruptedException {//得到房间titleString 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分钟点一个星光,然后送一个大白。
③ 周而复始…