All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.echobox.api.tiktok.model.TikTokShare Maven / Gradle / Ivy

The newest version!
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.echobox.api.tiktok.model;

import com.echobox.api.tiktok.util.URLUtils;
import com.google.gson.annotations.SerializedName;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * Model object for a TikTok video share
 *
 * @author eddspencer
 */
@ToString
@EqualsAndHashCode
@Getter
@Setter
public class TikTokShare {
  
  @SerializedName("item_id")
  private String itemId;
  @SerializedName("create_time")
  private String createTime;
  @SerializedName("thumbnail_url")
  private String thumbnailURL;
  private Long thumbnailURLExpireUnixTime;
  @SerializedName("embed_url")
  private String embedURL;
  @SerializedName("share_url")
  private String shareURL;
  private String caption;
  @SerializedName("video_views")
  private Integer videoViews;
  @SerializedName("video_duration")
  private Float videoDuration;
  private Integer likes;
  private Integer comments;
  private Integer shares;
  private Integer reach;
  @SerializedName("full_video_watched_rate")
  private Float fullVideoWatchedRate;
  @SerializedName("total_time_watched")
  private Float totalTimeWatched;
  @SerializedName("average_time_watched")
  private Float averageTimeWatched;
  @SerializedName("impression_sources")
  private List impressionSources;
  @SerializedName("audience_countries")
  private List audienceCountries;
  
  /**
   * Gets create unix time.
   *
   * @return the create unix time
   */
  public long getCreateUnixTime() {
    return Optional.ofNullable(createTime).map(Long::parseLong).orElse(0L);
  }
  
  /**
   * Initialise all transient fields
   */
  public void initialiseTransientFields() {
    this.setThumbnailURLExpireUnixTime(parseThumbnailURL(thumbnailURL));
  }
  
  /**
   * Parses the thumbnailURL to retrieve the x-expires query param and transform it to a long
   * value if found
   *
   * @param thumbnailURL the URL of the thumbnail
   * @return the expire time as a long value
   */
  private Long parseThumbnailURL(String thumbnailURL) {
    if (!StringUtils.isEmpty(thumbnailURL)) {
      Map> queryParams = URLUtils.extractParametersFromUrl(thumbnailURL);
      if (queryParams.containsKey("x-expires")) {
        try {
          return Optional.ofNullable(queryParams.get("x-expires").get(0))
              .map(Long::parseLong).orElse(null);
        } catch (NumberFormatException exception) {
          // If the x-expires query param is incorrectly formatted when retrieved (e.g.
          // ?x-expires or ?x-expires=invalid then we want to return null instead of throwing an
          // exception.
          return null;
        }
      }
    }
  
    return null;
  }
  
  /**
   * Gets the expire unix time of the thumbnail URL
   *
   * @return the expire unix time of the thumbnail URL
   */
  public Long getThumbnailURLExpireUnixTime() {
    if (thumbnailURLExpireUnixTime != null) {
      return thumbnailURLExpireUnixTime;
    } else {
      return parseThumbnailURL(thumbnailURL);
    }
  }
  
  /**
   * Sets the thumbnailURL as well as the thumbnailURLExpireUnixTime
   *
   * @param thumbnailURL the new URL
   */
  public void setThumbnailURL(String thumbnailURL) {
    this.thumbnailURL = thumbnailURL;
    this.thumbnailURLExpireUnixTime = parseThumbnailURL(thumbnailURL);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy