zerodytrash / TikTok-Live-Connector

Node.js library to receive live stream events (comments, gifts, etc.) in realtime from TikTok LIVE.
https://discord.gg/2c6kX6g3Pa
MIT License
1.21k stars 263 forks source link

#TikTok #155

Open Delightai6881 opened 7 months ago

Delightai6881 commented 7 months ago

// TikTok API wrapper for Java import java.io.; import java.net.; import java.util.; import org.json.;

public class TikTok {

// API base URL
private static final String BASE_URL = "https://api.tiktok.com/";

// API key
private String apiKey;

// Constructor
public TikTok(String apiKey) {
    this.apiKey = apiKey;
}

// Get trending videos
public List<Video> getTrendingVideos(int limit) throws IOException {
    // Build request URL
    String url = BASE_URL + "video/trending?limit=" + limit + "&key=" + apiKey;

    // Send GET request and parse JSON response
    JSONObject response = sendGetRequest(url);
    JSONArray data = response.getJSONArray("data");

    // Create a list of videos
    List<Video> videos = new ArrayList<>();
    for (int i = 0; i < data.length(); i++) {
        JSONObject video = data.getJSONObject(i);
        videos.add(new Video(video));
    }

    // Return the list of videos
    return videos;
}

// Get videos by hashtag
public List<Video> getVideosByHashtag(String hashtag, int limit) throws IOException {
    // Build request URL
    String url = BASE_URL + "video/hashtag/" + hashtag + "?limit=" + limit + "&key=" + apiKey;

    // Send GET request and parse JSON response
    JSONObject response = sendGetRequest(url);
    JSONArray data = response.getJSONArray("data");

    // Create a list of videos
    List<Video> videos = new ArrayList<>();
    for (int i = 0; i < data.length(); i++) {
        JSONObject video = data.getJSONObject(i);
        videos.add(new Video(video));
    }

    // Return the list of videos
    return videos;
}

// Get videos by user
public List<Video> getVideosByUser(String username, int limit) throws IOException {
    // Build request URL
    String url = BASE_URL + "video/user/" + username + "?limit=" + limit + "&key=" + apiKey;

    // Send GET request and parse JSON response
    JSONObject response = sendGetRequest(url);
    JSONArray data = response.getJSONArray("data");

    // Create a list of videos
    List<Video> videos = new ArrayList<>();
    for (int i = 0; i < data.length(); i++) {
        JSONObject video = data.getJSONObject(i);
        videos.add(new Video(video));
    }

    // Return the list of videos
    return videos;
}

// Get video details by id
public Video getVideoById(String id) throws IOException {
    // Build request URL
    String url = BASE_URL + "video/" + id + "?key=" + apiKey;

    // Send GET request and parse JSON response
    JSONObject response = sendGetRequest(url);
    JSONObject data = response.getJSONObject("data");

    // Create a video object
    Video video = new Video(data);

    // Return the video object
    return video;
}

// Send a GET request and return a JSON object
private JSONObject sendGetRequest(String url) throws IOException {
    // Create a URL object
    URL obj = new URL(url);

    // Open a connection
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();

    // Set request method to GET
    con.setRequestMethod("GET");

    // Get response code
    int responseCode = con.getResponseCode();

    // Check if response code is OK
    if (responseCode == HttpURLConnection.HTTP_OK) {
        // Read response from input stream
        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();

        // Convert response to JSON object
        JSONObject json = new JSONObject(response.toString());

        // Return the JSON object
        return json;
    } else {
        // Throw an exception
        throw new IOException("GET request failed: " + responseCode);
    }
}

}

// Video class class Video {

// Video id
private String id;

// Video title
private String title;

// Video description
private String description;

// Video duration in seconds
private int duration;

// Video cover image URL
private String cover;

// Video play URL
private String play;

// Video download URL
private String download;

// Video share URL
private String share;

// Video likes count
private int likes;

// Video comments count
private int comments;

// Video views count
private int views;

// Video author username
private String author;

// Video author avatar URL
private String avatar;

// Video hashtags
private List<String> hashtags;

// Constructor
public Video(JSONObject video) {
    this.id = video.getString("id");
    this.title = video.getString("title");
    this.description = video.getString("description");
    this.duration = video.getInt("
TimeCodings commented 7 months ago

This has nothing to do with the TikTok Live connector from zerodytrash?