账号模块b站登录、抖音登录,抖音视频自动化发布

This commit is contained in:
welsir
2023-10-13 01:30:01 +08:00
parent 99dde3e038
commit 9927cfa40e
18 changed files with 287 additions and 83 deletions
+1 -1
View File
@@ -38,7 +38,7 @@
</component>
<component name="JavacSettings">
<option name="ADDITIONAL_OPTIONS_OVERRIDE">
<module name="ChopperBot" options="" />
<module name="ChopperBot" options="-parameters" />
<module name="chopperbot-account" options="-parameters" />
<module name="chopperbot-barrage" options="-parameters" />
<module name="chopperbot-common" options="-parameters" />
@@ -2,7 +2,9 @@ package org.example.api;
import org.example.core.account.Impl.AccountOperator;
import org.example.pojo.Account;
import org.springframework.web.bind.annotation.*;
import org.example.pojo.AccountVO;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;
import javax.annotation.Resource;
import java.util.List;
@@ -12,25 +14,42 @@ import java.util.List;
* @Author welsir
* @Date 2023/9/22 14:11
*/
@RestController
@RequestMapping("account")
@Component
public class AccountApi {
@Resource
private AccountOperator accountOperator;
@PostMapping("/addUser/{uid}")
public void addAccount(@PathVariable int uid){
accountOperator.insertAccount(uid);
/*
* 第一次登陆调用此方法将账号对应cookie存入数据库中
* 目前仅支持两个平台的账号保管(抖音、b站)
* 目前只支持以下方式登录
* 抖音登录默认为验证码登录
* b站登录默认为账号密码登录
*/
public void addAccountSaveCookie(int platformId, @RequestBody String username, @RequestBody String password){
accountOperator.insertAccount(platformId,username,password);
}
@GetMapping("/getUser/{platformId}")
public List<Account> getAllUsers(@PathVariable int platformId){
/*
* 根据平台id获取用户集合
*/
public List<AccountVO> getAllUsers(int platformId){
return accountOperator.getAllUsers(platformId);
}
@PostMapping("/editUser")
public void editUser(@RequestBody Account account){
public List<AccountVO> getAllUsers(){
return accountOperator.getAllUsers();
}
/*
* 修改用户属性
*/
public void editUser(Account account){
accountOperator.editUser(account);
}
public void deleteUser(int uid){
accountOperator.deleteAccount(uid);
}
}
@@ -1,19 +0,0 @@
package org.example.core.account;
import org.example.pojo.Account;
import java.util.List;
/**
* @Description
* @Author welsir
* @Date 2023/9/23 14:02
*/
public interface AccountCenter {
void insertAccount(int platformId);
List<Account> getAllUsers(int id);
void editUser(Account account);
}
@@ -0,0 +1,24 @@
package org.example.core.account;
import org.example.pojo.Account;
import org.example.pojo.AccountVO;
import java.util.List;
/**
* @Description
* @Author welsir
* @Date 2023/9/23 14:02
*/
public interface AccountOperateCenter {
void insertAccount(int platformId,String username,String password);
List<AccountVO> getAllUsers();
List<AccountVO> getAllUsers(int id);
void deleteAccount(int uid);
void editUser(Account account);
}
@@ -1,33 +1,54 @@
package org.example.core.account.Impl;
import org.example.core.account.AccountCenter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.example.core.account.AccountOperateCenter;
import org.example.core.constpool.ConstPool;
import org.example.core.factory.PlatformFactory;
import org.example.mapper.AccountMapper;
import org.example.core.factory.PlatformOperation;
import org.example.core.mapper.AccountMapper;
import org.example.core.mapper.AccountTypeMapper;
import org.example.pojo.Account;
import org.springframework.stereotype.Component;
import org.example.pojo.AccountType;
import org.example.pojo.AccountVO;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* @Description
* @Author welsir
* @Date 2023/9/23 14:04
*/
@Component
public class AccountOperator implements AccountCenter {
@Service
public class AccountOperator implements AccountOperateCenter {
@Resource
private AccountMapper accountMapper;
@Resource
AccountTypeMapper accountTypeMapper;
@Override
public void insertAccount(int platformId) {
PlatformFactory.createPlatformOperation(platformId);
public void insertAccount(int platformId,String username,String password) {
PlatformOperation platformOperation = PlatformFactory.createPlatformOperation(platformId);
platformOperation.login(platformId,username,password);
}
@Override
public List<Account> getAllUsers(int id) {
return accountMapper.selectUserByPlatform(id);
public List<AccountVO> getAllUsers() {
List<Account> accountList = accountMapper.selectList(null);
List<AccountType> accountTypes = accountTypeMapper.selectList(null);
return addTypeToAccount(accountTypes,accountList);
}
@Override
public List<AccountVO> getAllUsers(int id) {
List<Account> accountList = accountMapper.selectUserByPlatform(id);
List<AccountType> accountTypes = accountTypeMapper.selectList(null);
return addTypeToAccount(accountTypes,accountList);
}
@Override
@@ -35,4 +56,31 @@ public class AccountOperator implements AccountCenter {
int i = accountMapper.updateById(account);
Assert.isTrue(i==1,"Update user fail!Please try again or check the wrong!");
}
public List<AccountVO> addTypeToAccount(List<AccountType> accountTypes,List<Account> accountList){
Map<Long, List<AccountType>> typeMap = accountTypes.stream()
.collect(Collectors.groupingBy(AccountType::getUid));
List<AccountVO> accountVOList = new ArrayList<>();
for (Account account : accountList) {
AccountVO accountVO = new AccountVO();
List<AccountType> types = typeMap.get(account.getId());
if (types != null) {
accountVO.setTypeList(types);
accountVO.setUid(account.getId());
accountVO.setUsername(account.getUsername());
accountVO.setPassword(account.getPassword());
accountVO.setPlatform(ConstPool.AccountPlatForm.fromId(account.getPlatformId()));
accountVOList.add(accountVO);
}
}
return accountVOList;
}
@Override
public void deleteAccount(int uid){
accountMapper.deleteById(uid);
QueryWrapper<AccountType> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("uid",uid);
accountTypeMapper.delete(queryWrapper);
}
}
@@ -0,0 +1,35 @@
package org.example.core.constpool;
/**
* @Description
* @Author welsir
* @Date 2023/10/12 20:47
*/
public class ConstPool {
//账号平台
public enum AccountPlatForm{
BILIBILI(1),
DOUYIN(2);
private int id;
AccountPlatForm(int id){
this.id = id;
}
public static int getPlatFormId (int id){
return id;
}
public static String fromId(int id) {
for (AccountPlatForm platform : values()) {
if (platform.id == id) {
return platform.toString();
}
}
throw new IllegalArgumentException("Invalid id: " + id);
}
}
}
@@ -1,9 +1,10 @@
package org.example.mapper;
package org.example.core.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Select;
import org.example.pojo.Account;
import org.example.pojo.AccountType;
import org.springframework.stereotype.Component;
import java.util.List;
@@ -12,6 +13,7 @@ import java.util.List;
* @Author welsir
* @Date 2023/9/23 14:15
*/
@Component
public interface AccountMapper extends BaseMapper<Account> {
@Select("select * from account a join platform pf on a.platformId=pf.id where pf.id = #{id}")
@@ -20,4 +22,5 @@ public interface AccountMapper extends BaseMapper<Account> {
@Select("select * from account_type where uid=#{id}")
List<AccountType> selectTypeByUid(Long id);
}
@@ -1,4 +1,4 @@
package org.example.mapper;
package org.example.core.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.example.pojo.AccountType;
@@ -1,16 +1,23 @@
package org.example.core.platform;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.example.core.factory.PlatformOperation;
import org.example.mapper.AccountMapper;
import org.example.core.mapper.AccountMapper;
import org.example.pojo.Account;
import org.openqa.selenium.By;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import javax.annotation.Resource;
import java.util.Scanner;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
/**
* @Description
@@ -18,36 +25,84 @@ import java.util.concurrent.TimeUnit;
* @Date 2023/9/24 21:18
*/
public class Bilibili implements PlatformOperation {
//cookie本地读取路径
private static final String FILE_PATH = "D:\\Douyincookies.txt";
private static final String URL = "https://www.bilibili.com/";
@Resource
private AccountMapper accountMapper;
Set<Cookie> cookies;
@Override
public void operation(int id) {
public void login(int id,String account,String password) {
try {
//设置driver驱动,<-记得更改为自己的本地路径!!->
// 读取Python脚本的输出
List<String> command = new ArrayList<>();
command.add("python"); // Python 解释器
command.add("D:\\code\\gitHubProject\\Text_select_captcha\\bilbil.py"); // 要运行的 Python 脚本文件名
//第一次登录需要提供账号密码
command.add(account); // 第一个参数
command.add(password); // 第二个参数 账号
ProcessBuilder processBuilder = new ProcessBuilder(command);
Process process = processBuilder.start();
// 等待Python脚本执行完成
int exitCode = process.waitFor();
if (exitCode == 0) {
System.out.println("Python脚本执行成功!");
} else {
System.err.println("Python脚本执行失败!");
}
System.setProperty("webdriver.chrome.driver", "D:\\downLoad\\chromedriver_win32\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
//同理
options.addArguments("--headless");
options.setBinary("C:\\Program Files (x86)\\Chromebrowser\\Chrome.exe");
ChromeDriver webDriver = new ChromeDriver(options);
//b站网址
String url = "https://www.bilibili.com/";
webDriver.get(url);
//与浏览器同步非常重要,必须等待浏览器加载完毕
webDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//中间完成登录操作
Scanner scanner = new Scanner(System.in);
scanner.next();
//获取cookie
cookies = webDriver.manage().getCookies();
Account account = new Account();
account.setCookie(cookies);
account.setPlatformId(id);
accountMapper.insert(account);
} catch (Exception e) {
Set<Cookie> read = readCookiesByLocal();
webDriver.get(URL); //
webDriver.manage().deleteAllCookies();
for (Cookie cookie : read) {
webDriver.manage().addCookie(cookie);
}
webDriver.navigate().refresh();
Thread.sleep(3000L);
WebElement avator = webDriver.findElement(By.xpath("/html/body/div[2]/div[2]/div[1]/div[1]/ul[2]/li[1]"));
if(avator!=null){
Account user = new Account();
user.setCookies(read.toString());
user.setPlatformId(1);
user.setUsername("13078050925");
user.setPassword("wjy20020225");
accountMapper.insert(user);
}
} catch (IOException | InterruptedException e) {
throw new RuntimeException(e);
}
}
private Set<Cookie> readCookiesByLocal() throws IOException {
Set<Cookie> cookies = new HashSet<>();
String jsonContent = new String(Files.readAllBytes(Paths.get(FILE_PATH)));
JSONArray jsonArray = JSON.parseArray(jsonContent);
// 遍历 JSONArray 并解析为 Cookie 对象
for (int i = 0; i < jsonArray.size(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String path = jsonObject.getString("path");
String domain = jsonObject.getString("domain");
String sameSite = jsonObject.getString("sameSite");
String name = jsonObject.getString("name");
String expiry = jsonObject.getString("expiry");
String httpOnly = jsonObject.getString("httpOnly");
String secure = jsonObject.getString("secure");
String value = jsonObject.getString("value");
// 其他 Cookie 属性的解析
long timestamp = Long.parseLong(expiry);
// 将 Unix 时间戳转换为 Date 对象
Date date = new Date(timestamp * 1000);
Cookie cookie = new Cookie.Builder(name,value).domain(domain).path(path).expiresOn(date).sameSite(sameSite).isHttpOnly(Boolean.parseBoolean(httpOnly)).isSecure(Boolean.parseBoolean(secure)).build();
cookies.add(cookie);
}
return cookies;
}
}
@@ -0,0 +1,17 @@
package org.example.core.platform;
import org.example.core.factory.PlatformOperation;
/**
* @Description
* @Author welsir
* @Date 2023/10/9 17:09
*/
public class Douyin implements PlatformOperation {
@Override
public void login(int id,String account,String password) {
}
}
@@ -1,4 +1,4 @@
package org.example.pojo;
package org.example.core.pojo;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
@@ -6,9 +6,8 @@ import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.openqa.selenium.Cookie;
import java.util.Set;
import java.io.Serializable;
/**
* @Description
@@ -19,11 +18,13 @@ import java.util.Set;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Account {
public class Account implements Serializable {
@TableId(value = "id", type = IdType.ASSIGN_ID)
private Long uid;
private Set<Cookie> cookie;
@TableId(value = "uid", type = IdType.ASSIGN_UUID)
private Long id;
private String username;
private String password;
private String cookies;
private boolean isCompleteMatch;
private int platformId;
@@ -1,12 +1,10 @@
package org.example.pojo;
package org.example.core.pojo;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
/**
* @Description
* @Author welsir
@@ -0,0 +1,24 @@
package org.example.core.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.example.pojo.AccountType;
import java.util.List;
/**
* @Description
* @Author welsir
* @Date 2023/10/12 21:03
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class AccountVO {
private Long uid;
private String username;
private String password;
private String platform;
List<AccountType> typeList;
}
@@ -1,4 +1,4 @@
package org.example.pojo;
package org.example.core.pojo;
/**
* @Description
@@ -1,4 +1,6 @@
package org.example.pojo;
package org.example.core.pojo;
import org.example.pojo.VideoType;
import java.util.List;
@@ -1,12 +1,9 @@
package org.example.pojo;
package org.example.core.pojo;
import lombok.Data;
import org.openqa.selenium.Cookie;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* @Description
@@ -18,9 +15,9 @@ public class VideoQueue {
private String name;
private List<Object> messages;
private boolean isStrongMatch;
private Set<Cookie> cookies;
private String cookies;
public VideoQueue(String name, boolean isStrongMatch,Set<Cookie> cookies) {
public VideoQueue(String name, boolean isStrongMatch,String cookies) {
this.name = name;
this.messages = new ArrayList<>();
this.isStrongMatch = isStrongMatch;
@@ -1,4 +1,4 @@
package org.example.pojo;
package org.example.core.pojo;
/**
* @Description
Binary file not shown.