mirror of
https://github.com/jwdeveloper/TikTokLiveJava.git
synced 2026-03-02 02:09:40 -05:00
Changes:
onCustomEvent() <- registering custom events onMapping() <- custom mappings check out 'CustomMappingExample' more gifs has been added exceptions are more explicit
This commit is contained in:
@@ -74,7 +74,7 @@ public class TikTokGenericEventMapper {
|
||||
}
|
||||
}
|
||||
|
||||
private Method getParsingMethod(Class<?> input) throws NoSuchMethodException {
|
||||
public Method getParsingMethod(Class<?> input) throws NoSuchMethodException {
|
||||
if (methodCache.containsKey(input)) {
|
||||
return methodCache.get(input);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
package io.github.jwdeveloper.tiktok.mappers;
|
||||
|
||||
import com.google.protobuf.GeneratedMessageV3;
|
||||
import io.github.jwdeveloper.tiktok.data.events.common.TikTokEvent;
|
||||
import io.github.jwdeveloper.tiktok.exceptions.TikTokMessageMappingException;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class TikTokLiveMapper implements TikTokMapper {
|
||||
private final Map<String, Function<byte[], List<TikTokEvent>>> mappers;
|
||||
private final TikTokGenericEventMapper genericMapper;
|
||||
|
||||
public TikTokLiveMapper(TikTokGenericEventMapper genericMapper) {
|
||||
this.mappers = new HashMap<>();
|
||||
this.genericMapper = genericMapper;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void bytesToEvent(String messageName, Function<byte[], TikTokEvent> onMapping) {
|
||||
mappers.put(messageName, messagePayload -> List.of(onMapping.apply(messagePayload)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void bytesToEvents(String messageName, Function<byte[], List<TikTokEvent>> onMapping) {
|
||||
mappers.put(messageName, onMapping::apply);
|
||||
}
|
||||
|
||||
public void bytesToEvent(Class<? extends GeneratedMessageV3> clazz, Function<byte[], TikTokEvent> onMapping) {
|
||||
mappers.put(clazz.getSimpleName(), messagePayload -> List.of(onMapping.apply(messagePayload)));
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void bytesToEvents(Class<? extends GeneratedMessageV3> clazz, Function<byte[], List<TikTokEvent>> onMapping) {
|
||||
mappers.put(clazz.getSimpleName(), onMapping::apply);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void webcastObjectToConstructor(Class<? extends GeneratedMessageV3> sourceClass, Class<? extends TikTokEvent> outputClass) {
|
||||
bytesToEvent(sourceClass, (e) -> genericMapper.mapToEvent(sourceClass, outputClass, e));
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T extends GeneratedMessageV3> void webcastObjectToEvent(Class<T> source, Function<T, TikTokEvent> onMapping) {
|
||||
bytesToEvent(source, (bytes) ->
|
||||
{
|
||||
try {
|
||||
var parsingMethod = genericMapper.getParsingMethod(source);
|
||||
var sourceObject = parsingMethod.invoke(null, bytes);
|
||||
var event = onMapping.apply((T) sourceObject);
|
||||
return event;
|
||||
} catch (Exception e) {
|
||||
throw new TikTokMessageMappingException(source, "can't find parsing method", e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T extends GeneratedMessageV3> void webcastObjectToEvents(Class<T> source, Function<T, List<TikTokEvent>> onMapping) {
|
||||
bytesToEvents(source, (bytes) ->
|
||||
{
|
||||
try {
|
||||
var parsingMethod = genericMapper.getParsingMethod(source);
|
||||
var sourceObject = parsingMethod.invoke(null, bytes);
|
||||
var event = onMapping.apply((T) sourceObject);
|
||||
return event;
|
||||
} catch (Exception e) {
|
||||
throw new TikTokMessageMappingException(source, "can't find parsing method", e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public boolean isRegistered(String input) {
|
||||
return mappers.containsKey(input);
|
||||
}
|
||||
|
||||
public List<TikTokEvent> handleMapping(String input, byte[] bytes) {
|
||||
if (!isRegistered(input)) {
|
||||
return List.of();
|
||||
}
|
||||
var mapper = mappers.get(input);
|
||||
var events = mapper.apply(bytes);
|
||||
return events;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* Copyright (c) 2023-2023 jwdeveloper jacekwoln@gmail.com
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
package io.github.jwdeveloper.tiktok.mappers.events;
|
||||
|
||||
public class TikTokChestEventHandler {
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package io.github.jwdeveloper.tiktok.mappers.events;
|
||||
|
||||
import io.github.jwdeveloper.tiktok.data.events.*;
|
||||
import io.github.jwdeveloper.tiktok.data.events.common.TikTokEvent;
|
||||
import io.github.jwdeveloper.tiktok.data.events.envelop.TikTokChestEvent;
|
||||
import io.github.jwdeveloper.tiktok.data.events.poll.TikTokPollEndEvent;
|
||||
import io.github.jwdeveloper.tiktok.data.events.poll.TikTokPollEvent;
|
||||
import io.github.jwdeveloper.tiktok.data.events.poll.TikTokPollStartEvent;
|
||||
import io.github.jwdeveloper.tiktok.data.events.poll.TikTokPollUpdateEvent;
|
||||
import io.github.jwdeveloper.tiktok.data.events.room.TikTokRoomPinEvent;
|
||||
import io.github.jwdeveloper.tiktok.data.models.chest.Chest;
|
||||
import io.github.jwdeveloper.tiktok.messages.enums.EnvelopeDisplay;
|
||||
import io.github.jwdeveloper.tiktok.messages.webcast.*;
|
||||
import lombok.SneakyThrows;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class TikTokCommonEventHandler
|
||||
{
|
||||
|
||||
@SneakyThrows
|
||||
public TikTokEvent handleWebcastControlMessage(byte[] msg) {
|
||||
var message = WebcastControlMessage.parseFrom(msg);
|
||||
return switch (message.getAction()) {
|
||||
case STREAM_PAUSED -> new TikTokLivePausedEvent();
|
||||
case STREAM_ENDED -> new TikTokLiveEndedEvent();
|
||||
case STREAM_UNPAUSED -> new TikTokLiveUnpausedEvent();
|
||||
default -> new TikTokUnhandledControlEvent(message);
|
||||
};
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
public TikTokEvent handlePinMessage(byte[] msg) {
|
||||
var pinMessage = WebcastRoomPinMessage.parseFrom(msg);
|
||||
var chatMessage = WebcastChatMessage.parseFrom(pinMessage.getPinnedMessage());
|
||||
var chatEvent = new TikTokCommentEvent(chatMessage);
|
||||
return new TikTokRoomPinEvent(pinMessage, chatEvent);
|
||||
}
|
||||
|
||||
//TODO Probably not working
|
||||
@SneakyThrows
|
||||
public TikTokEvent handlePollEvent(byte[] msg) {
|
||||
var poolMessage = WebcastPollMessage.parseFrom(msg);
|
||||
return switch (poolMessage.getMessageType()) {
|
||||
case 0 -> new TikTokPollStartEvent(poolMessage);
|
||||
case 1 -> new TikTokPollEndEvent(poolMessage);
|
||||
case 2 -> new TikTokPollUpdateEvent(poolMessage);
|
||||
default -> new TikTokPollEvent(poolMessage);
|
||||
};
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
public List<TikTokEvent> handleEnvelop(byte[] data) {
|
||||
var msg = WebcastEnvelopeMessage.parseFrom(data);
|
||||
if (msg.getDisplay() != EnvelopeDisplay.EnvelopeDisplayNew) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
var totalDiamonds = msg.getEnvelopeInfo().getDiamondCount();
|
||||
var totalUsers = msg.getEnvelopeInfo().getPeopleCount();
|
||||
var chest = new Chest(totalDiamonds, totalUsers);
|
||||
|
||||
return List.of(new TikTokChestEvent(chest, msg));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
/*
|
||||
* Copyright (c) 2023-2023 jwdeveloper jacekwoln@gmail.com
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
package io.github.jwdeveloper.tiktok.mappers.events;
|
||||
|
||||
import io.github.jwdeveloper.tiktok.data.events.common.TikTokEvent;
|
||||
import io.github.jwdeveloper.tiktok.data.events.gift.TikTokGiftComboEvent;
|
||||
import io.github.jwdeveloper.tiktok.data.events.gift.TikTokGiftEvent;
|
||||
import io.github.jwdeveloper.tiktok.data.models.Picture;
|
||||
import io.github.jwdeveloper.tiktok.data.models.gifts.Gift;
|
||||
import io.github.jwdeveloper.tiktok.data.models.gifts.GiftSendType;
|
||||
import io.github.jwdeveloper.tiktok.exceptions.TikTokLiveException;
|
||||
import io.github.jwdeveloper.tiktok.live.GiftManager;
|
||||
import io.github.jwdeveloper.tiktok.messages.webcast.WebcastGiftMessage;
|
||||
import lombok.SneakyThrows;
|
||||
import sun.misc.Unsafe;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class TikTokGiftEventHandler {
|
||||
private final GiftManager giftManager;
|
||||
private final Map<Long, WebcastGiftMessage> giftsMessages;
|
||||
|
||||
public TikTokGiftEventHandler(GiftManager giftManager) {
|
||||
this.giftManager = giftManager;
|
||||
giftsMessages = new HashMap<>();
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
public List<TikTokEvent> handleGift(byte[] msg) {
|
||||
var currentMessage = WebcastGiftMessage.parseFrom(msg);
|
||||
return handleGift(currentMessage);
|
||||
}
|
||||
|
||||
public List<TikTokEvent> handleGift(WebcastGiftMessage currentMessage) {
|
||||
var userId = currentMessage.getUser().getId();
|
||||
var currentType = GiftSendType.fromNumber(currentMessage.getSendType());
|
||||
var containsPreviousMessage = giftsMessages.containsKey(userId);
|
||||
|
||||
|
||||
//If gift is not streakable just return onGift event
|
||||
if (currentMessage.getGift().getType() != 1) {
|
||||
var comboEvent = getGiftComboEvent(currentMessage, GiftSendType.Finished);
|
||||
var giftEvent = getGiftEvent(currentMessage);
|
||||
return List.of(comboEvent, giftEvent);
|
||||
}
|
||||
|
||||
if (!containsPreviousMessage) {
|
||||
if (currentType == GiftSendType.Finished) {
|
||||
return List.of(getGiftEvent(currentMessage));
|
||||
} else {
|
||||
giftsMessages.put(userId, currentMessage);
|
||||
return List.of(getGiftComboEvent(currentMessage, GiftSendType.Begin));
|
||||
}
|
||||
}
|
||||
|
||||
var previousMessage = giftsMessages.get(userId);
|
||||
var previousType = GiftSendType.fromNumber(previousMessage.getSendType());
|
||||
if (currentType == GiftSendType.Active &&
|
||||
previousType == GiftSendType.Active) {
|
||||
giftsMessages.put(userId, currentMessage);
|
||||
return List.of(getGiftComboEvent(currentMessage, GiftSendType.Active));
|
||||
}
|
||||
|
||||
|
||||
if (currentType == GiftSendType.Finished &&
|
||||
previousType == GiftSendType.Active) {
|
||||
giftsMessages.clear();
|
||||
return List.of(
|
||||
getGiftComboEvent(currentMessage, GiftSendType.Finished),
|
||||
getGiftEvent(currentMessage));
|
||||
}
|
||||
|
||||
return List.of();
|
||||
}
|
||||
|
||||
|
||||
private TikTokGiftEvent getGiftEvent(WebcastGiftMessage message) {
|
||||
var gift = getGiftObject(message);
|
||||
return new TikTokGiftEvent(gift, message);
|
||||
}
|
||||
|
||||
private TikTokGiftEvent getGiftComboEvent(WebcastGiftMessage message, GiftSendType state) {
|
||||
var gift = getGiftObject(message);
|
||||
return new TikTokGiftComboEvent(gift, message, state);
|
||||
}
|
||||
|
||||
private Gift getGiftObject(WebcastGiftMessage giftMessage) {
|
||||
var giftId = (int) giftMessage.getGiftId();
|
||||
var gift = giftManager.findById(giftId);
|
||||
if (gift == Gift.UNDEFINED) {
|
||||
gift = giftManager.findByName(giftMessage.getGift().getName());
|
||||
}
|
||||
if (gift == Gift.UNDEFINED) {
|
||||
gift = giftManager.registerGift(
|
||||
giftId,
|
||||
giftMessage.getGift().getName(),
|
||||
giftMessage.getGift().getDiamondCount(),
|
||||
Picture.map(giftMessage.getGift().getImage()));
|
||||
}
|
||||
|
||||
if (gift.getPicture().getLink().endsWith(".webp")) {
|
||||
updatePicture(gift, giftMessage);
|
||||
}
|
||||
return gift;
|
||||
}
|
||||
|
||||
|
||||
private void updatePicture(Gift gift, WebcastGiftMessage webcastGiftMessage) {
|
||||
try {
|
||||
var picture = Picture.map(webcastGiftMessage.getGift().getImage());
|
||||
var constructor = Unsafe.class.getDeclaredConstructors()[0];
|
||||
constructor.setAccessible(true);
|
||||
var field = Gift.class.getDeclaredField("picture");
|
||||
field.setAccessible(true);
|
||||
field.set(gift, picture);
|
||||
} catch (Exception e) {
|
||||
throw new TikTokLiveException("Unable to update picture in gift: " + gift.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
/*
|
||||
* Copyright (c) 2023-2023 jwdeveloper jacekwoln@gmail.com
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
package io.github.jwdeveloper.tiktok.mappers.events;
|
||||
|
||||
import io.github.jwdeveloper.tiktok.TikTokRoomInfo;
|
||||
import io.github.jwdeveloper.tiktok.data.events.TikTokSubscribeEvent;
|
||||
import io.github.jwdeveloper.tiktok.data.events.TikTokUnhandledMemberEvent;
|
||||
import io.github.jwdeveloper.tiktok.data.events.common.TikTokEvent;
|
||||
import io.github.jwdeveloper.tiktok.data.events.room.TikTokRoomInfoEvent;
|
||||
import io.github.jwdeveloper.tiktok.data.events.social.TikTokJoinEvent;
|
||||
import io.github.jwdeveloper.tiktok.data.events.social.TikTokLikeEvent;
|
||||
import io.github.jwdeveloper.tiktok.data.models.RankingUser;
|
||||
import io.github.jwdeveloper.tiktok.data.models.users.User;
|
||||
import io.github.jwdeveloper.tiktok.messages.webcast.WebcastLikeMessage;
|
||||
import io.github.jwdeveloper.tiktok.messages.webcast.WebcastLiveIntroMessage;
|
||||
import io.github.jwdeveloper.tiktok.messages.webcast.WebcastMemberMessage;
|
||||
import io.github.jwdeveloper.tiktok.messages.webcast.WebcastRoomUserSeqMessage;
|
||||
import lombok.SneakyThrows;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class TikTokRoomInfoEventHandler {
|
||||
private final TikTokRoomInfo roomInfo;
|
||||
|
||||
public TikTokRoomInfoEventHandler(TikTokRoomInfo roomInfo) {
|
||||
this.roomInfo = roomInfo;
|
||||
}
|
||||
|
||||
public TikTokEvent handleRoomInfo(Consumer<TikTokRoomInfo> consumer) {
|
||||
consumer.accept(roomInfo);
|
||||
return new TikTokRoomInfoEvent(roomInfo);
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
public TikTokEvent handleUserRanking(byte[] msg) {
|
||||
var message = WebcastRoomUserSeqMessage.parseFrom(msg);
|
||||
var totalUsers = message.getTotalUser();
|
||||
var userRanking = message.getRanksListList().stream().map(RankingUser::new)
|
||||
.sorted((ru1, ru2) -> Integer.compare(ru2.getScore(), ru1.getScore()))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
return handleRoomInfo(tikTokRoomInfo ->
|
||||
{
|
||||
tikTokRoomInfo.setTotalViewersCount(totalUsers);
|
||||
tikTokRoomInfo.updateRanking(userRanking);
|
||||
});
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
public TikTokEvent handleIntro(byte[] msg) {
|
||||
var message = WebcastLiveIntroMessage.parseFrom(msg);
|
||||
var hostUser = User.map(message.getHost());
|
||||
var language = message.getLanguage();
|
||||
|
||||
return handleRoomInfo(tikTokRoomInfo ->
|
||||
{
|
||||
if(tikTokRoomInfo.getHost() == null)
|
||||
{
|
||||
tikTokRoomInfo.setHost(hostUser);
|
||||
}
|
||||
tikTokRoomInfo.setLanguage(language);
|
||||
});
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
public List<TikTokEvent> handleMemberMessage(byte[] msg) {
|
||||
var message = WebcastMemberMessage.parseFrom(msg);
|
||||
|
||||
var event = switch (message.getAction()) {
|
||||
case JOINED -> new TikTokJoinEvent(message);
|
||||
case SUBSCRIBED -> new TikTokSubscribeEvent(message);
|
||||
default -> new TikTokUnhandledMemberEvent(message);
|
||||
};
|
||||
|
||||
var roomInfoEvent = this.handleRoomInfo(tikTokRoomInfo ->
|
||||
{
|
||||
tikTokRoomInfo.setViewersCount(message.getMemberCount());
|
||||
});
|
||||
|
||||
return List.of(event, roomInfoEvent);
|
||||
}
|
||||
@SneakyThrows
|
||||
public List<TikTokEvent> handleLike(byte[] msg)
|
||||
{
|
||||
var message = WebcastLikeMessage.parseFrom(msg);
|
||||
var event = new TikTokLikeEvent(message);
|
||||
var roomInfoEvent = this.handleRoomInfo(tikTokRoomInfo ->
|
||||
{
|
||||
tikTokRoomInfo.setLikesCount(event.getTotalLikes());
|
||||
});
|
||||
return List.of(event, roomInfoEvent);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
* Copyright (c) 2023-2023 jwdeveloper jacekwoln@gmail.com
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
package io.github.jwdeveloper.tiktok.mappers.events;
|
||||
|
||||
import io.github.jwdeveloper.tiktok.TikTokRoomInfo;
|
||||
import io.github.jwdeveloper.tiktok.data.events.TikTokUnhandledSocialEvent;
|
||||
import io.github.jwdeveloper.tiktok.data.events.common.TikTokEvent;
|
||||
import io.github.jwdeveloper.tiktok.data.events.social.TikTokFollowEvent;
|
||||
import io.github.jwdeveloper.tiktok.data.events.social.TikTokJoinEvent;
|
||||
import io.github.jwdeveloper.tiktok.data.events.social.TikTokLikeEvent;
|
||||
import io.github.jwdeveloper.tiktok.data.events.social.TikTokShareEvent;
|
||||
import io.github.jwdeveloper.tiktok.data.models.Text;
|
||||
import io.github.jwdeveloper.tiktok.messages.webcast.WebcastSocialMessage;
|
||||
import io.github.jwdeveloper.tiktok.models.SocialTypes;
|
||||
import lombok.SneakyThrows;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class TikTokSocialMediaEventHandler
|
||||
{
|
||||
private final TikTokRoomInfo roomInfo;
|
||||
private final Pattern socialMediaPattern = Pattern.compile("pm_mt_guidance_viewer_([0-9]+)_share");
|
||||
|
||||
public TikTokSocialMediaEventHandler(TikTokRoomInfo roomInfo) {
|
||||
this.roomInfo = roomInfo;
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
public TikTokEvent handle(byte[] msg)
|
||||
{
|
||||
var message = WebcastSocialMessage.parseFrom(msg);
|
||||
|
||||
var socialType = Text.map(message.getCommon().getDisplayText()).getKey();
|
||||
var matcher = socialMediaPattern.matcher(socialType);
|
||||
|
||||
if (matcher.find()) {
|
||||
var value = matcher.group(1);
|
||||
var number = Integer.parseInt(value);
|
||||
return new TikTokShareEvent(message, number);
|
||||
}
|
||||
|
||||
return switch (socialType) {
|
||||
case SocialTypes.LikeType -> new TikTokLikeEvent(message, roomInfo.getLikesCount());
|
||||
case SocialTypes.FollowType -> new TikTokFollowEvent(message);
|
||||
case SocialTypes.ShareType -> new TikTokShareEvent(message);
|
||||
case SocialTypes.JoinType -> new TikTokJoinEvent(message, roomInfo.getViewersCount());
|
||||
default -> new TikTokUnhandledSocialEvent(message);
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user