Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@
import java.util.*;

@Data
@AllArgsConstructor
public class Gift
{
@Getter private static final Set<Gift> gifts = new HashSet<>();
public static final Gift UNDEFINED = new Gift(-1, "undefined", -1, "", null);
public class Gift {
public static final Gift UNDEFINED = new Gift(-1, "undefined", -1, "");

private final int id;

Expand All @@ -23,14 +20,23 @@ public class Gift

private final JsonObject properties;

public Gift(int id, String name, int diamondCost, String pictureLink, JsonObject properties) {
public Gift(int id, String name, int diamondCost, Picture pictureLink, JsonObject properties) {
this.id = id;
this.name = name;
this.diamondCost = diamondCost;
this.picture = new Picture(pictureLink);
this.picture = pictureLink;
this.properties = properties;
}


public Gift(int id, String name, int diamondCost, String pictureLink) {
this(id, name, diamondCost, new Picture(pictureLink), new JsonObject());
}

public Gift(int id, String name, int diamondCost, Picture picture) {
this(id, name, diamondCost, picture, new JsonObject());
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add constructor to pass properties into class.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

public boolean hasDiamondCostRange(int minimalCost, int maximalCost) {
return diamondCost >= minimalCost && diamondCost <= maximalCost;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
*/
package io.github.jwdeveloper.tiktok.data.requests;

import io.github.jwdeveloper.tiktok.data.models.gifts.Gift;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
Expand All @@ -41,16 +42,7 @@ public final class Request
public static final class Response
{
private String json;
private List<GiftModel> gifts;
}

@Data
public static class GiftModel
{
private int id;
private String name;
private int diamondCost;
private String image;
private List<Gift> gifts;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@
@Data
public class LiveClientSettings {


/**
* Determines if gifts data is downloaded before TikTokLive starts,
* when `false` then client.giftManager() does not contain initial gifts
*/
private boolean fetchGifts;

/**
* ISO-Language for Client
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public interface LiveHttpClient
*/
GiftsData.Response fetchGiftsData();


/**
* Returns information about user that is having a livestream
* @param userName name of user
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,43 +25,62 @@
import com.google.gson.JsonObject;
import io.github.jwdeveloper.tiktok.data.models.Picture;
import io.github.jwdeveloper.tiktok.data.models.gifts.*;
import io.github.jwdeveloper.tiktok.exceptions.TikTokLiveException;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;

public interface GiftManager {
public interface GiftsManager {

/**
* In case you can't find your gift in Gift enum. You can register gift
* manually here to make it detected while TikTokGiftEvent
* You can create and attach your own custom gift to manager
*
* @param id gift's id
* @param name gift's name
* @param diamondCost diamond cost
* @return
* @param gift
*/
default Gift registerGift(int id, String name, int diamondCost, Picture picture) {
return registerGift(id, name, diamondCost, picture, null);
}

Gift registerGift(int id, String name, int diamondCost, Picture picture, JsonObject properties);
void attachGift(Gift gift);

/**
* You can create and attach your own custom gift to manager
*
* @param giftId
* @return
* @param gifts
*/
Gift findById(int giftId);
void attachGiftsList(List<Gift> gifts);

/**
* finds gift by name
* When gift not found return Gift.UNDEFINED;
*
* @param giftName
* @return
* @param name gift name
*/
Gift findByName(String giftName);
Gift getByName(String name);

/**
* finds gift by id
* When gift not found return Gift.UNDEFINED;
*
* @return all gifts
* @param giftId giftId
*/
Gift getById(int giftId);


/**
* finds gift by filter
* When gift not found return Gift.UNDEFINED;
*/
Gift getByFilter(Predicate<Gift> filter);

List<Gift> getManyByFilter(Predicate<Gift> filter);

/**
* @return list of all gifts
*/
List<Gift> toList();


/**
* @return list of all map of all gifts where Integer is gift Id
*/
List<Gift> getGifts();
Map<Integer, Gift> toMap();
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public interface LiveClient {
/**
* Get information about gifts
*/
GiftManager getGiftManager();
GiftsManager getGiftManager();

/**
* Gets the current room info from TikTok API including streamer info, room status and statistics.
Expand Down
49 changes: 39 additions & 10 deletions Client/src/main/java/io/github/jwdeveloper/tiktok/TikTokLive.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,19 @@
package io.github.jwdeveloper.tiktok;


import io.github.jwdeveloper.tiktok.gifts.TikTokGiftsManager;
import io.github.jwdeveloper.tiktok.http.LiveHttpClient;
import io.github.jwdeveloper.tiktok.live.GiftsManager;
import io.github.jwdeveloper.tiktok.live.builder.LiveClientBuilder;

import java.util.List;
import java.util.concurrent.CompletableFuture;

public class TikTokLive {

/**
* Example: https://www.tiktok.com/@dostawcavideo - hostName would be 'dostawcavideo'
*
* @param hostName profile name of Tiktok user could be found in profile link
* @return LiveClientBuilder
*/
Expand All @@ -41,42 +45,42 @@ public static LiveClientBuilder newClient(String hostName) {

/**
* Example: https://www.tiktok.com/@dostawcavideo - hostName would be 'dostawcavideo'
*
* @param hostName profile name of Tiktok user could be found in profile link
* @return true if live is Online, false if is offline
*/
public static boolean isLiveOnline(String hostName)
{
public static boolean isLiveOnline(String hostName) {
return requests().fetchLiveUserData(hostName).isLiveOnline();
}

/**
* Example: https://www.tiktok.com/@dostawcavideo - hostName would be 'dostawcavideo'
*
* @param hostName profile name of Tiktok user could be found in profile link
* @return true if live is Online, false if is offline
*/
public static CompletableFuture<Boolean> isLiveOnlineAsync(String hostName)
{
return CompletableFuture.supplyAsync(()-> isLiveOnline(hostName));
public static CompletableFuture<Boolean> isLiveOnlineAsync(String hostName) {
return CompletableFuture.supplyAsync(() -> isLiveOnline(hostName));
}

/**
* Example: https://www.tiktok.com/@dostawcavideo - hostName would be 'dostawcavideo'
*
* @param hostName profile name of Tiktok user could be found in profile link
* @return true is hostName name is valid and exists, false if not
*/
public static boolean isHostNameValid(String hostName)
{
public static boolean isHostNameValid(String hostName) {
return requests().fetchLiveUserData(hostName).isHostNameValid();
}

/**
* Example: https://www.tiktok.com/@dostawcavideo - hostName would be 'dostawcavideo'
*
* @param hostName profile name of Tiktok user could be found in profile link
* @return true is hostName name is valid and exists, false if not
*/
public static CompletableFuture<Boolean> isHostNameValidAsync(String hostName)
{
return CompletableFuture.supplyAsync(()-> isHostNameValid(hostName));
public static CompletableFuture<Boolean> isHostNameValidAsync(String hostName) {
return CompletableFuture.supplyAsync(() -> isHostNameValid(hostName));
}

/**
Expand All @@ -87,4 +91,29 @@ public static CompletableFuture<Boolean> isHostNameValidAsync(String hostName)
public static LiveHttpClient requests() {
return new TikTokLiveHttpClient();
}


//I don't like it, but it is reasonable for now
private static GiftsManager giftsManager;

/**
* Fetch gifts from endpoint and returns GiftManager
*
* @return GiftsManager
*/
public static GiftsManager gifts() {
if (giftsManager != null) {
return giftsManager;
}
synchronized (GiftsManager.class)
{
if (giftsManager == null)
{
return new TikTokGiftsManager(requests().fetchGiftsData().getGifts());
}
}
return giftsManager;
}


}
Loading