账号模块完成对切片的接受和对应账号视频的发布,未经整理和测试

This commit is contained in:
welsir
2023-09-25 15:44:44 +08:00
parent 6e1e3db348
commit 7a7ca0034e
10 changed files with 75 additions and 95 deletions

View File

@@ -74,5 +74,11 @@
<version>20210307</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.example</groupId>
<artifactId>chopperbot-publish</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>

View File

@@ -2,8 +2,6 @@ package org.example.api;
import org.example.core.account.Impl.AccountOperator;
import org.example.pojo.Account;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
@@ -31,4 +29,8 @@ public class AccountApi {
return accountOperator.getAllUsers(platformId);
}
@PostMapping("/editUser")
public void editUser(@RequestBody Account account){
accountOperator.editUser(account);
}
}

View File

@@ -15,4 +15,5 @@ public interface AccountCenter {
List<Account> getAllUsers(int id);
void editUser(Account account);
}

View File

@@ -5,6 +5,7 @@ import org.example.core.factory.PlatformFactory;
import org.example.core.mapper.AccountMapper;
import org.example.pojo.Account;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import javax.annotation.Resource;
import java.util.List;
@@ -26,6 +27,12 @@ public class AccountOperator implements AccountCenter {
@Override
public List<Account> getAllUsers(int id) {
return accountMapper.getUserByPlatform(id);
return accountMapper.selectUserByPlatform(id);
}
@Override
public void editUser(Account account) {
int i = accountMapper.updateById(account);
Assert.isTrue(i==1,"Update user fail!Please try again or check the wrong!");
}
}

View File

@@ -1,8 +1,13 @@
package org.example.core.exchange;
import org.example.api.BilibiliPublishApi;
import org.example.pojo.VideoQueue;
import java.util.*;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* @Description
@@ -12,7 +17,30 @@ import java.util.*;
public class Exchange {
private final Map<String, List<VideoQueue>> bindings = new HashMap<>();
ScheduledExecutorService task;
public Exchange() {
task = Executors.newScheduledThreadPool(1);
}
public void startListening() {
// 每5秒轮询一次消息队列
// 检查消息队列并提取消息
task.scheduleAtFixedRate(this::checkAndProcessMessages, 0, 5, TimeUnit.SECONDS);
}
private void checkAndProcessMessages() {
// 在这里检查消息队列提取消息并调用publish方法
for (String routingKey : bindings.keySet()) {
if (bindings.containsKey(routingKey)) {
List<VideoQueue> queues = bindings.get(routingKey);
for (VideoQueue queue : queues) {
Object message = queue.dequeue();
if (message != null) {
BilibiliPublishApi.PublishVideo("xxx","xxx",queue.getCookies().toString(),"xxx");
}
}
}
}
}
public void bind(VideoQueue queue, String routingKey) {
@@ -23,10 +51,7 @@ public class Exchange {
public void publish(String routingKey, Object message) {
if (this.bindings.containsKey(routingKey)) {
List<VideoQueue> queues = this.bindings.get(routingKey);
Iterator var4 = queues.iterator();
while(var4.hasNext()) {
VideoQueue queue = (VideoQueue)var4.next();
for (VideoQueue queue : queues) {
queue.enqueue(message);
}
}

View File

@@ -1,30 +1,15 @@
package org.example.core.guard;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.example.pojo.VideoType;
import org.example.core.exchange.Exchange;
import org.example.core.mapper.AccountMapper;
import org.example.core.mapper.AccountTypeMapper;
import org.example.plugin.GuardPlugin;
import org.example.pojo.Account;
import org.example.pojo.AccountType;
import org.example.pojo.Video;
import org.example.pojo.VideoQueue;
import org.example.pojo.*;
import javax.annotation.Resource;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @Description
@@ -34,13 +19,10 @@ import java.util.regex.Pattern;
public class VideoPushGuard extends GuardPlugin {
private Exchange exchange;
private static final String ACCOUNT_PATH = "config/Account/account.json";
private BlockingQueue<Object> receiveVideo;
@Resource
private AccountMapper accountMapper;
@Resource
private AccountTypeMapper accountTypeMapper;
public VideoPushGuard(String module, String pluginName, List<String> needPlugins, boolean isAutoStart) {
@@ -49,45 +31,17 @@ public class VideoPushGuard extends GuardPlugin {
@Override
public boolean init() {
//两件事情 一注册队列;二启动队列监听
exchange = new Exchange();
try {
List<Account> accountList = accountMapper.selectList(null);
for (Account account : accountList) {
List<AccountType> accountTypes = accountTypeMapper.selectList(new QueryWrapper<AccountType>().eq("uid",account.getId()));
List<Account> accountList = accountMapper.selectList(null);
for (Account account : accountList) {
List<AccountType> accountTypes = accountMapper.selectTypeByUid(account.getUid());
for (AccountType accountType : accountTypes) {
exchange.bind(new VideoQueue(PlatformType.getPlatform(account.getPlatformId()) + "-" + account.getUid(), account.isCompleteMatch(),account.getCookie()), accountType.getType());
}
String jsonData = Files.readString(Path.of(ACCOUNT_PATH));
JSONObject jsonObject = JSON.parseObject(jsonData);
for (Map.Entry<String, Object> platformEntry : jsonObject.entrySet()) {
String platform = platformEntry.getKey();
JSONObject platformData = (JSONObject) platformEntry.getValue();
System.out.println("Platform: " + platform);
System.out.println("platformData: " + platformData);
String types = getStringValueOrDefault(platformData, "type", "");
boolean isMatch = getBooleanValueOrDefault(platformData, "isMatch", false);
Pattern pattern = Pattern.compile("\"([^\"]+)\"");
Matcher matcher = pattern.matcher(types);
while (matcher.find()) {
String type = matcher.group(1);
for (String queueName : platformData.keySet()) {
exchange.bind(new VideoQueue(platform + "-" + queueName, isMatch), type);
}
}
}
receiveVideo = new ArrayBlockingQueue<>(1024);
return true;
} catch (IOException e) {
throw new RuntimeException(e);
}
receiveVideo = new ArrayBlockingQueue<>(1024);
return true;
}
@Override
@@ -95,13 +49,12 @@ public class VideoPushGuard extends GuardPlugin {
try {
Object videoMsg = receiveVideo.poll(5, TimeUnit.SECONDS);
if (videoMsg instanceof Video) {
Video video = (Video)videoMsg;
Iterator<VideoType> iterator = video.getVideoType().iterator();
while(iterator.hasNext()) {
VideoType videoType = iterator.next();
Video video = (Video) videoMsg;
for (VideoType videoType : video.getVideoType()) {
exchange.publish(videoType.toString(), video.getMessage());
}
}
exchange.startListening();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
@@ -110,13 +63,4 @@ public class VideoPushGuard extends GuardPlugin {
public void sendVideo(Object msg) {
receiveVideo.offer(msg);
}
private String getStringValueOrDefault(JSONObject jsonObject, String key, String defaultValue) {
Object value = jsonObject.get(key);
return value != null ? value.toString() : defaultValue;
}
private boolean getBooleanValueOrDefault(JSONObject jsonObject, String key, boolean defaultValue) {
Object value = jsonObject.get(key);
return value != null ? (boolean) value : defaultValue;
}
}

View File

@@ -3,6 +3,7 @@ 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 java.util.List;
@@ -14,6 +15,9 @@ import java.util.List;
public interface AccountMapper extends BaseMapper<Account> {
@Select("select * from account a join platform pf on a.platformId=pf.id where pf.id = #{id}")
List<Account> getUserByPlatform(int id);
List<Account> selectUserByPlatform(int id);
@Select("select * from account_type where uid=#{id}")
List<AccountType> selectTypeByUid(Long id);
}

View File

@@ -18,6 +18,6 @@ import java.util.List;
@TableName(value = "account_type")
public class AccountType {
private String uid;
private List<VideoType> videoTypes;
private Long uid;
private String type;
}

View File

@@ -1,35 +1,30 @@
package org.example.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
* @Author welsir
* @Date 2023/9/4 22:09
*/
@Data
public class VideoQueue {
private String name;
private List<Object> messages;
private List<VideoType> type;
private boolean isStrongMatch;
private Set<Cookie> cookies;
public List<VideoType> getType() {
return this.type;
}
public void setType(List<VideoType> type) {
this.type = type;
}
public VideoQueue() {
}
public VideoQueue(String name, boolean isStrongMatch) {
public VideoQueue(String name, boolean isStrongMatch,Set<Cookie> cookies) {
this.name = name;
this.messages = new ArrayList<>();
this.isStrongMatch = isStrongMatch;
this.cookies = cookies;
}
public void enqueue(Object message) {
@@ -44,10 +39,6 @@ public class VideoQueue {
return this.messages.isEmpty();
}
public boolean shouldReceiveMessage(List<VideoType> videoType) {
return (new HashSet(videoType)).equals(new HashSet(this.type));
}
public String toString() {
return "[name:" + this.name + ",message:{" + this.messages + "},isMatch:" + this.isStrongMatch;
}

Binary file not shown.