账号模块b站登录修正,架构优化

This commit is contained in:
welsir
2023-10-18 21:45:06 +08:00
parent 8507e24d3c
commit 7224b6d875
21 changed files with 84 additions and 90 deletions
+4
View File
@@ -121,5 +121,9 @@
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-support</artifactId>
</dependency>
</dependencies>
</project>
@@ -1,5 +1,6 @@
package org.example.api;
import org.example.core.account.AccountOperateCenter;
import org.example.core.account.Impl.AccountOperator;
import org.example.pojo.Account;
import org.example.pojo.AccountVO;
@@ -18,7 +19,7 @@ import java.util.List;
public class AccountApi {
@Resource
private AccountOperator accountOperator;
AccountOperateCenter accountOperator;
/*
* 第一次登陆调用此方法将账号对应cookie存入数据库中
@@ -27,8 +28,8 @@ public class AccountApi {
* 抖音登录默认为验证码登录
* b站登录默认为账号密码登录
*/
public void addAccountSaveCookie(int platformId, @RequestBody String username, @RequestBody String password){
accountOperator.insertAccount(platformId,username,password);
public void addAccountSaveCookie(int platformId, String username){
accountOperator.insertAccount(platformId,username);
}
/*
@@ -13,7 +13,7 @@ import java.util.List;
*/
public interface AccountOperateCenter {
void insertAccount(int platformId,String username,String password);
void insertAccount(int platformId,String username);
List<AccountVO> getAllUsers();
@@ -1,6 +1,7 @@
package org.example.core.account.Impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.core.account.AccountOperateCenter;
import org.example.core.constpool.ConstPool;
import org.example.core.factory.PlatformFactory;
@@ -11,6 +12,8 @@ import org.example.mapper.AccountTypeMapper;
import org.example.pojo.Account;
import org.example.pojo.AccountType;
import org.example.pojo.AccountVO;
import org.openqa.selenium.Cookie;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
@@ -18,6 +21,7 @@ import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
/**
@@ -26,16 +30,21 @@ import java.util.stream.Collectors;
* @Date 2023/9/23 14:04
*/
@Service
public class AccountOperator implements AccountOperateCenter {
public class AccountOperator extends ServiceImpl<AccountMapper,Account> implements AccountOperateCenter {
@Resource
private AccountMapper accountMapper;
AccountMapper accountMapper;
@Resource
AccountTypeMapper accountTypeMapper;
@Override
public void insertAccount(int platformId,String username,String password) {
public void insertAccount(int platformId,String username) {
PlatformOperation platformOperation = PlatformFactory.createPlatformOperation(platformId);
platformOperation.login(platformId,username,password);
Set<Cookie> cookies = platformOperation.login(platformId, username);
Account account = new Account();
account.setPlatform_id(platformId);
account.setCookies(cookies.toString());
account.setUsername(username);
accountMapper.insert(account);
}
@Override
@@ -69,8 +78,7 @@ public class AccountOperator implements AccountOperateCenter {
accountVO.setTypeList(types);
accountVO.setUid(account.getId());
accountVO.setUsername(account.getUsername());
accountVO.setPassword(account.getPassword());
accountVO.setPlatform(ConstPool.AccountPlatForm.fromId(account.getPlatformId()));
accountVO.setPlatform(ConstPool.AccountPlatForm.fromId(account.getPlatform_id()));
accountVOList.add(accountVO);
}
}
@@ -16,7 +16,6 @@ public class PlatformFactory {
switch (PlatformType.getPlatform(platformId)){
case BILIBILI:
return new Bilibili();
case DOUYU:
case DOUYIN:
return new Douyin();
default:
@@ -1,5 +1,9 @@
package org.example.core.factory;
import org.openqa.selenium.Cookie;
import java.util.Set;
/**
* @Description
* @Author welsir
@@ -8,6 +12,6 @@ package org.example.core.factory;
public interface PlatformOperation {
void login(int id,String account,String password);
Set<Cookie> login(int id, String account);
}
@@ -1,5 +1,6 @@
package org.example.core.guard;
import org.example.core.account.AccountOperateCenter;
import org.example.core.exchange.Exchange;
import org.example.mapper.AccountMapper;
import org.example.plugin.GuardPlugin;
@@ -23,7 +24,8 @@ public class VideoPushGuard extends GuardPlugin {
@Resource
private AccountMapper accountMapper;
@Resource
AccountOperateCenter operateCenter;
public VideoPushGuard(String module, String pluginName, List<String> needPlugins, boolean isAutoStart) {
super(module, pluginName, needPlugins, isAutoStart);
@@ -37,7 +39,7 @@ public class VideoPushGuard extends GuardPlugin {
for (Account account : accountList) {
List<AccountType> accountTypes = accountMapper.selectTypeByUid(account.getId());
for (AccountType accountType : accountTypes) {
exchange.bind(new VideoQueue(PlatformType.getPlatform(account.getPlatformId()) + "-" + account.getId(), account.isCompleteMatch(),account.getCookies()), accountType.getType());
exchange.bind(new VideoQueue(PlatformType.getPlatform(account.getPlatform_id()) + "-" + account.getId(), account.is_complete_match(),account.getCookies()), accountType.getType());
}
}
receiveVideo = new ArrayBlockingQueue<>(1024);
@@ -3,14 +3,22 @@ package org.example.core.platform;
import org.example.core.factory.PlatformOperation;
import org.example.mapper.AccountMapper;
import org.example.pojo.Account;
import org.openqa.selenium.By;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.TimeUnit;
/**
@@ -18,16 +26,13 @@ import java.util.*;
* @Author welsir
* @Date 2023/9/24 21:18
*/
@Component
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;
@Override
public void login(int id,String username,String password) {
public Set<Cookie> login(int id,String username) {
try {
System.setProperty("webdriver.chrome.driver", "D:\\downLoad\\chromedriver_win32\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
@@ -38,24 +43,22 @@ public class Bilibili implements PlatformOperation {
Thread.sleep(30000L);
Set<Cookie> cookies = loginwebDriver.manage().getCookies();
loginwebDriver.quit();
ChromeDriver confirmLogin = new ChromeDriver(options);
confirmLogin.get(URL);
confirmLogin.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
confirmLogin.manage().deleteAllCookies();
for (Cookie cookie : cookies) {
loginwebDriver.manage().addCookie(cookie);
confirmLogin.manage().addCookie(cookie);
}
confirmLogin.navigate().refresh();
Thread.sleep(3000L);
WebElement avator = confirmLogin.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(cookies.toString());
// user.setPlatformId(1);
// user.setUsername(username);
// user.setPassword(password);
// accountMapper.insert(user);
System.out.println("登陆成功!");
confirmLogin.quit();
return cookies;
}else{
throw new RuntimeException("登陆失败!");
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
@@ -10,10 +10,7 @@ import org.openqa.selenium.Cookie;
import javax.annotation.Resource;
import java.io.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.*;
import static org.example.utils.GetScriptPath.getScriptPath;
@@ -31,7 +28,7 @@ public class Douyin implements PlatformOperation {
@Resource
AccountMapper accountMapper;
@Override
public void login(int id,String username,String password) {
public Set<Cookie> login(int id, String username) {
try {
List<String> command = new ArrayList<>();
command.add("python"); // Python 解释器
@@ -62,11 +59,7 @@ public class Douyin implements PlatformOperation {
int exitCode = process1.waitFor();
if (exitCode == 0) {
System.out.println("抖音登录成功!");
Account account = new Account();
account.setUsername(username);
account.setCookies(loadCookiesFromFile(FILE_PATH).toString());
account.setPlatformId(2);
accountMapper.insert(account);
}
}else{
throw new RuntimeException("登陆失败!");
@@ -74,6 +67,7 @@ public class Douyin implements PlatformOperation {
} catch (IOException | InterruptedException e) {
throw new RuntimeException(e);
}
return null;
}
//从本地文件获取cookie
@@ -13,7 +13,6 @@ 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}")
@@ -10,6 +10,5 @@ import org.springframework.stereotype.Component;
* @Author welsir
* @Date 2023/9/24 16:08
*/
@Component
public interface AccountTypeMapper extends BaseMapper<AccountType> {
}
@@ -20,12 +20,11 @@ import java.io.Serializable;
@NoArgsConstructor
public class Account implements Serializable {
@TableId(value = "uid", type = IdType.ASSIGN_UUID)
@TableId(value = "uid", type = IdType.ASSIGN_ID)
private Long id;
private String username;
private String password;
private String cookies;
private boolean isCompleteMatch;
private int platformId;
private boolean is_complete_match;
private int platform_id;
}
@@ -17,7 +17,6 @@ import java.util.List;
public class AccountVO {
private Long uid;
private String username;
private String password;
private String platform;
List<AccountType> typeList;
}
@@ -7,9 +7,8 @@ package org.example.pojo;
*/
public enum PlatformType {
DOUYU(1),
BILIBILI(2),
DOUYIN(3);
BILIBILI(1),
DOUYIN(2);
private final int index;
public int getId() {
@@ -26,24 +26,24 @@ public class AccountController {
@GetMapping(value = "/getUser/{platformId}")
public Result getAllUser(@PathVariable int platformId){
List<AccountVO> list = accountService.getAllUser(platformId);
List<AccountVO> list = accountService.accountPlugin().getAllUsers(platformId);
return Result.success(list);
}
@GetMapping(value = "/getUser")
public Result getAllUser(){
return Result.success(accountService.getAllUser());
return Result.success(accountService.accountPlugin().getAllUsers());
}
@PostMapping(value = "/login/{platformId}")
public Result login(@RequestBody String username, @RequestBody String password, @PathVariable int platformId){
accountService.login(platformId,username,password);
public Result login(@RequestParam String username, @PathVariable int platformId){
accountService.accountPlugin().addAccountSaveCookie(platformId,username);
return Result.success();
}
@PostMapping(value = "/edit")
public Result edit(@RequestBody Account account){
accountService.edit(account);
accountService.accountPlugin().editUser(account);
return Result.success();
}
@@ -2,6 +2,7 @@ package org.example.service;
import org.example.api.AccountApi;
import org.example.api.GPTApi;
import org.example.pojo.Account;
import org.example.pojo.AccountVO;
@@ -15,13 +16,7 @@ import java.util.List;
*/
public interface AccountService {
List<AccountVO> getAllUser(int id);
List<AccountVO> getAllUser();
void login(int platformId,String username,String password);
void edit(Account account);
AccountApi accountPlugin();
GPTApi chatGptPlugin();
}
@@ -24,24 +24,10 @@ public class AccountServiceImpl implements AccountService {
@Resource
GPTApi gptApi;
@Override
public List<AccountVO> getAllUser(int id) {
return accountApi.getAllUsers(id);
}
@Override
public List<AccountVO> getAllUser() {
return accountApi.getAllUsers();
}
@Override
public void login(int platformId,String username, String password) {
accountApi.addAccountSaveCookie(platformId,username,password);
}
@Override
public void edit(Account account) {
accountApi.editUser(account);
public AccountApi accountPlugin() {
return accountApi;
}
@Override
@@ -1,18 +1,8 @@
package org.example.core.creeper.loadtask;
import org.checkerframework.checker.units.qual.A;
import org.example.constpool.PluginName;
import org.example.core.VideoTaskMonitor;
import org.example.core.creeper.loadconfig.BilibiliLiveOnlineConfig;
import org.example.core.loadtask.CommonLoadTask;
import org.example.core.manager.LiveDownloadManager;
import org.example.core.taskmonitor.Monitor;
import org.example.log.ChopperLogFactory;
import org.example.log.LoggerType;
import org.example.plugin.PluginCheckAndDo;
import org.slf4j.Logger;
import java.util.concurrent.atomic.AtomicReference;
/**
* @author Genius
@@ -1,5 +1,6 @@
package org.example.account;
import org.junit.Test;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
@@ -18,7 +19,8 @@ import java.util.concurrent.TimeUnit;
*/
public class bilibiliTest {
public static void getcookies() throws Exception {
@Test
public void getcookies() throws Exception {
System.setProperty("webdriver.chrome.driver", "D:\\downLoad\\chromedriver_win32\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.setBinary("C:\\Program Files (x86)\\Chromebrowser\\Chrome.exe");
@@ -32,8 +34,15 @@ public class bilibiliTest {
scanner.next();
// 获取cookie,再通过baocun函数进行序列化操作
Set<Cookie> cookies = webDriver.manage().getCookies();
System.out.println(cookies);
baocun(cookies);
webDriver.quit();
ChromeDriver chromeDriver = new ChromeDriver(options);
chromeDriver.get(url);
chromeDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
chromeDriver.manage().deleteAllCookies();
for (Cookie cookie : cookies) {
chromeDriver.manage().addCookie(cookie);
}
chromeDriver.navigate().refresh();
}
//把登录后得到的cookie序列化到硬盘中
@@ -62,7 +71,6 @@ public class bilibiliTest {
// // 与浏览器同步非常重要,必须等待浏览器加载完毕
// webDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// webDriver.navigate().refresh();
getcookies();
}
//读取硬盘cookie的操作
+1 -1
View File
@@ -34,5 +34,5 @@
"CreeperConfig":true
}
},
"updateTime":"2023-10-18 14:32:00"
"updateTime":"2023-10-18 21:43:00"
}
+5
View File
@@ -153,6 +153,11 @@
<version>${spring-boot-test.version}</version>
</dependency>
<!-- selenium-->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>21.0</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-remote-driver</artifactId>