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

This commit is contained in:
welsir
2023-10-13 01:47:59 +08:00
parent 9927cfa40e
commit 86607f4806
13 changed files with 238 additions and 75 deletions

View File

@@ -67,6 +67,17 @@
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-chrome-driver</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-api</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-remote-driver</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>org.json</groupId>

View File

@@ -1,8 +1,8 @@
package org.example.api;
import org.example.core.account.Impl.AccountOperator;
import org.example.pojo.Account;
import org.example.pojo.AccountVO;
import org.example.core.pojo.Account;
import org.example.core.pojo.AccountVO;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;

View File

@@ -1,7 +1,8 @@
package org.example.core.account;
import org.example.pojo.Account;
import org.example.pojo.AccountVO;
import org.example.core.pojo.Account;
import org.example.core.pojo.AccountVO;
import java.util.List;

View File

@@ -5,11 +5,12 @@ import org.example.core.account.AccountOperateCenter;
import org.example.core.constpool.ConstPool;
import org.example.core.factory.PlatformFactory;
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.example.pojo.AccountType;
import org.example.pojo.AccountVO;
import org.example.core.pojo.Account;
import org.example.core.pojo.AccountType;
import org.example.core.pojo.AccountVO;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

View File

@@ -1,7 +1,9 @@
package org.example.core.factory;
import org.example.core.platform.Bilibili;
import org.example.pojo.PlatformType;
import org.example.core.platform.Douyin;
import org.example.core.pojo.PlatformType;
/**
* @Description
@@ -10,14 +12,13 @@ import org.example.pojo.PlatformType;
*/
public class PlatformFactory {
public static void createPlatformOperation(int platformId) {
public static PlatformOperation createPlatformOperation(int platformId) {
switch (PlatformType.getPlatform(platformId)){
case BILIBILI:
new Bilibili();
return;
return new Bilibili();
case DOUYU:
case DOUYIN:
return;
return new Douyin();
default:
throw new RuntimeException();
}

View File

@@ -8,6 +8,6 @@ package org.example.core.factory;
public interface PlatformOperation {
void operation(int id);
void login(int id,String account,String password);
}

View File

@@ -2,8 +2,8 @@ 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.example.core.pojo.Account;
import org.example.core.pojo.AccountType;
import org.springframework.stereotype.Component;
import java.util.List;

View File

@@ -1,7 +1,8 @@
package org.example.core.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.example.pojo.AccountType;
import org.example.core.pojo.AccountType;
/**
* @Description

View File

@@ -5,7 +5,8 @@ import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.example.core.factory.PlatformOperation;
import org.example.core.mapper.AccountMapper;
import org.example.pojo.Account;
import org.example.core.pojo.Account;
import org.openqa.selenium.By;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebElement;
@@ -33,76 +34,37 @@ public class Bilibili implements PlatformOperation {
private AccountMapper accountMapper;
@Override
public void login(int id,String account,String password) {
public void login(int id,String username,String password) {
try {
// 读取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);
Set<Cookie> read = readCookiesByLocal();
ChromeDriver loginwebDriver = new ChromeDriver(options);
loginwebDriver.get(URL); //
loginwebDriver.manage().deleteAllCookies();
Thread.sleep(30000L);
Set<Cookie> cookies = loginwebDriver.manage().getCookies();
loginwebDriver.quit();
webDriver.get(URL); //
webDriver.manage().deleteAllCookies();
for (Cookie cookie : read) {
webDriver.manage().addCookie(cookie);
ChromeDriver confirmLogin = new ChromeDriver(options);
confirmLogin.get(URL);
confirmLogin.manage().deleteAllCookies();
for (Cookie cookie : cookies) {
loginwebDriver.manage().addCookie(cookie);
}
webDriver.navigate().refresh();
confirmLogin.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]"));
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(read.toString());
user.setCookies(cookies.toString());
user.setPlatformId(1);
user.setUsername("13078050925");
user.setPassword("wjy20020225");
user.setUsername(username);
user.setPassword(password);
accountMapper.insert(user);
}
} catch (IOException | InterruptedException e) {
} catch (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;
}
}

View File

@@ -1,6 +1,19 @@
package org.example.core.platform;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONReader;
import org.example.core.factory.PlatformOperation;
import org.example.core.mapper.AccountMapper;
import org.example.core.pojo.Account;
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;
/**
* @Description
@@ -9,9 +22,68 @@ import org.example.core.factory.PlatformOperation;
*/
public class Douyin implements PlatformOperation {
final String FILE_PATH = "D:\\Douyincookies.txt";
@Resource
AccountMapper accountMapper;
@Override
public void login(int id,String account,String password) {
public void login(int id,String username,String password) {
try {
List<String> command = new ArrayList<>();
command.add("python"); // Python 解释器
command.add("D:\\code\\gitHubProject\\Text_select_captcha\\DouyinLogin.py"); // 要运行的 Python 脚本文件名
ProcessBuilder processBuilder = new ProcessBuilder(command);
Process process = processBuilder.start();
List<String> command1 = new ArrayList<>();
command1.add("python"); // Python 解释器
command1.add("D:\\code\\gitHubProject\\Text_select_captcha\\Douyin.py");
ProcessBuilder processBuilder1 = new ProcessBuilder(command1);
Process process1 = processBuilder1.start();
// 获取Python脚本的标准输出流
InputStream inputStream1 = process1.getInputStream();
BufferedReader reader1 = new BufferedReader(new InputStreamReader(inputStream1));
String line1;
StringBuilder callBack = new StringBuilder();
// 读取Python脚本的输出
while ((line1 = reader1.readLine()) != null) {
callBack.append(line1).append("\n");
}
System.out.println("验证flag:" + callBack);
// 等待Python脚本执行完成
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);
}
} catch (IOException | InterruptedException e) {
throw new RuntimeException(e);
}
}
public HashSet<Cookie> loadCookiesFromFile (String filePath) throws IOException {
HashSet<Cookie> cookies = new HashSet<>();
try (FileReader fileReader = new FileReader(filePath)) {
JSONReader reader = new JSONReader(fileReader);
JSONArray jsonArray = reader.readObject(JSONArray.class);
for (int i = 0; i < jsonArray.size(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String name = jsonObject.getString("name");
String value = jsonObject.getString("value");
String domain = jsonObject.getString("domain");
String path = jsonObject.getString("path");
String sameSite = jsonObject.getString("sameSite");
Date expiry = jsonObject.getDate("expiry");
boolean secure = jsonObject.getBoolean("secure");
boolean httpOnly = jsonObject.getBoolean("httpOnly");
Cookie cookie = new Cookie(name, value, domain, path, expiry, secure, httpOnly, sameSite);
cookies.add(cookie);
}
}
return cookies;
}
}

View File

@@ -0,0 +1,47 @@
package org.example.controller;
import org.example.core.pojo.Account;
import org.example.core.pojo.AccountVO;
import org.example.service.AccountService;
import org.example.util.Result;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
/**
* @Description
* @Author welsir
* @Date 2023/10/12 22:26
*/
@RestController
@RequestMapping("/account")
public class AccountController {
@Resource
AccountService accountService;
@GetMapping(value = "/getUser/{platformId}")
public Result getAllUser(@PathVariable int platformId){
List<AccountVO> list = accountService.getAllUser(platformId);
return Result.success(list);
}
@GetMapping(value = "/getUser")
public Result getAllUser(){
return Result.success(accountService.getAllUser());
}
@PostMapping(value = "/login/{platformId}")
public Result login(@RequestBody String username, @RequestBody String password, @PathVariable int platformId){
accountService.login(platformId,username,password);
return Result.success();
}
@PostMapping(value = "/edit")
public Result edit(@RequestBody Account account){
accountService.edit(account);
return Result.success();
}
}

View File

@@ -0,0 +1,25 @@
package org.example.service;
import org.example.core.pojo.Account;
import org.example.core.pojo.AccountVO;
import java.util.List;
/**
* @Description
* @Author welsir
* @Date 2023/10/12 22:26
*/
public interface AccountService {
List<AccountVO> getAllUser(int id);
List<AccountVO> getAllUser();
void login(int platformId,String username,String password);
void edit(Account account);
}

View File

@@ -0,0 +1,42 @@
package org.example.service.impl;
import org.example.api.AccountApi;
import org.example.core.pojo.Account;
import org.example.core.pojo.AccountVO;
import org.example.service.AccountService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
/**
* @Description
* @Author welsir
* @Date 2023/10/12 22:39
*/
@Service
public class AccountServiceImpl implements AccountService {
@Resource
AccountApi accountApi;
@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);
}
}