thetvdb / v4-api

Founded in 2006, TheTVDB is one of the longest-running community-driven TV and Movie databases. With content metadata across hundreds of thousands of TV series and movies, TheTVDB powers many of the largest media centers in the market. Developers across the world build and rely upon TheTVDB's APIs to power their apps, utilities, and projects, generating millions of API calls per day. In our GitHub repository you will find all of the necessary support and v4 API documentation to make use of TheTVDB's metadata. Come and join us!
135 stars 13 forks source link

[noob help] Trying to pull back genre info from show name, but struggling with API v4 #309

Closed joebot57 closed 10 months ago

joebot57 commented 10 months ago

Hi all,

I've been trying to pull back TVDB Series ID and then Genre into a Google sheet, I managed to get ID to Genre working with the legacy API, but couldn't derive the ID from a show name

I've been trying with API v4 but now can't get either,

I'd appreciate any help/support in this

/**
 * Custom function to fetch TV show genres based on TVDB Show ID.
 * Usage: =FETCH_TV_SHOW_GENRE(ShowID, APIKey)
 *
 * @param {number} showId - The TVDB Show ID.
 * @param {string} apiKey - TVDB API key.
 * @return {string} - Comma-separated list of genres.
 */
function FETCH_TV_SHOW_GENRE(showId, apiKey) {
  try {
    const apiUrl = `https://api4.thetvdb.com/v4/series/${showId}`;
    const token = getApiToken(apiKey);

    const headers = {
      'Authorization': `Bearer ${token}`,
      'Accept': 'application/json',
      'Content-Type': 'application/json',
    };

    const response = UrlFetchApp.fetch(apiUrl, { headers: headers });
    const seriesData = JSON.parse(response.getContentText());

    if (seriesData.data && Array.isArray(seriesData.data.genres)) {
      return seriesData.data.genres.join(', ');
    } else {
      return 'Genres not found for this show';
    }
  } catch (error) {
    return 'Error fetching genres';
  }
}

/**
 * Custom function to fetch TVDB show ID based on show name.
 * Usage: =FETCH_TV_SHOW_ID(ShowName, APIKey)
 *
 * @param {string} showName - The TV show name.
 * @param {string} apiKey - TVDB API key.
 * @return {number} - The TVDB show ID.
 */
function FETCH_TV_SHOW_ID(showName, apiKey) {
  try {
    const apiUrl = `https://api4.thetvdb.com/v4/search?query=${encodeURIComponent(showName)}`;
    const token = getApiToken(apiKey);

    const headers = {
      'Authorization': `Bearer ${token}`,
      'Accept': 'application/json',
      'Content-Type': 'application/json',
    };

    const response = UrlFetchApp.fetch(apiUrl, { headers: headers });
    const searchData = JSON.parse(response.getContentText());

    if (searchData.data && searchData.data.length > 0) {
      return searchData.data[0].id;
    } else {
      return 'Show not found';
    }
  } catch (error) {
    return 'Error fetching show ID';
  }
}

/**
 * Helper function to fetch the token for TVDB API v4.
 *
 * @param {string} apiKey - Your TVDB API key.
 * @return {string} - The TVDB API token.
 */
function getApiToken(apiKey) {
  const options = {
    method: 'post',
    contentType: 'application/json',
    payload: JSON.stringify({apikey: apiKey})
  };

  const response = UrlFetchApp.fetch(`https://api4.thetvdb.com/v4/login`, options);
  const data = JSON.parse(response.getContentText());

  return data.token;
}
antheaezzell commented 10 months ago

When retrieving a series on the v4 API you should use the extended series endpoint, which will include genres.

joebot57 commented 10 months ago

mm, thanks, I cant seem to figure out where that should go in the above code? would you be able to give me any pointers?

szsori commented 10 months ago

Append /extended to the url in this line:

const apiUrl = `https://api4.thetvdb.com/v4/series/${showId}`;