Search code examples
ruby-on-railsthemoviedb-api

Fetch TMDB API by lang


I am trying to get series from the movie database in multiple languages. My goal is to update the object returned by the API to have the following keys : name_en, name_fr, overview_en, overview_fr

I have this piece of code :

require 'httparty'
class GetSeriesJob < ApplicationJob
    queue_as :default

    API_URL = 'https://api.themoviedb.org/3/'
    LANGS   = {'fr' => '&language=fr-FR', 'en' => '&language=en-EN'}

    def perform
        get_series
    end

    private

    def get_series
        seriesArray = []
        LANGS.each do |lang|
            series = HTTParty.get(API_URL + 'tv/popular?api_key=' + ENV['API_KEY'] + lang[1])
            tmp = JSON.parse(series.body)['results']
            tmp.each do |t|
                if seriesArray.detect{ |s| s['id'] == t['id'] }
                    t['name_' + lang[0]] = t['name']
                    t['overview_' + lang[0]] = t['overview']
                else
                    t['name_' + lang[0]] = t.delete t['name']
                    t['overview_' + lang[0]] = t.delete t['overview']
                end
            end
            seriesArray = tmp
        end
        puts seriesArray
        return seriesArray
    end
end

Which return (for one series) :

[["original_name", "The Expanse"], ["name_en", "The Expanse"], ["popularity", "66.679301"], ["origin_country", "[\"US\"]"], ["vote_count", 564], ["first_air_date", "2015-12-14"], ["backdrop_path", "/beIjmWr3OBOtcWK4tKMObOIDJ1C.jpg"], ["original_language", "en"], ["vote_average", 7.5], ["overview_en", "A thriller set two hundred years in the future following the case of a missing young woman who brings a hardened detective and a rogue ship's captain together in a race across the solar system to expose the greatest conspiracy in human history."], ["poster_path", "/prJFWxJ0x8tBPTliMjj51wgYnSK.jpg"], ["episode_run_time", "[43]"], ["number_of_seasons", 3], ["external_id", 63639], ["created_at", "2018-06-24 13:40:16.143952"], ["updated_at", "2018-06-24 13:40:16.143952"]]

And I would expect the following result :

[["original_name", "The Expanse"], ["name_en", "The Expanse"], ["name_fr", "The Expanse"]["popularity", "66.679301"], ["origin_country", "[\"US\"]"], ["vote_count", 564], ["first_air_date", "2015-12-14"], ["backdrop_path", "/beIjmWr3OBOtcWK4tKMObOIDJ1C.jpg"], ["original_language", "en"], ["vote_average", 7.5], ["overview_en", "A thriller set two hundred years in the future following the case of a missing young woman who brings a hardened detective and a rogue ship's captain together in a race across the solar system to expose the greatest conspiracy in human history."], ["overview_fr", "overview in French."] ["poster_path", "/prJFWxJ0x8tBPTliMjj51wgYnSK.jpg"], ["episode_run_time", "[43]"], ["number_of_seasons", 3], ["external_id", 63639], ["created_at", "2018-06-24 13:40:16.143952"], ["updated_at", "2018-06-24 13:40:16.143952"]]

I am using ruby on rails 5.2


Solution

  • So, I found myself a way to solve my issue, here are the relevant modifications

    def get_series
        seriesArray = []
        LANGS.each do |lang|
            series = HTTParty.get(API_URL + 'tv/popular?api_key=' + ENV['API_KEY'] + lang[1])
            tmp = JSON.parse(series.body)['results']
            tmp.each do |t|
                if serie = seriesArray.detect{ |s| s['id'] == t['id'] }
                    serie['name_' + lang[0]] = t['name']
                    serie['overview_' + lang[0]] = t['overview']
                    serie['genre_' + lang[0]] = t['genre_ids']
                    serie.delete('name')
                    serie.delete('overview')
                    serie.delete('genre_ids')
                else
                    t['name_' + lang[0]] =  t['name']
                    t['overview_' + lang[0]] = t['overview']
                    t['genre_' + lang[0]] = t['genre_ids']
                end
            end
            if seriesArray.count == 0
                seriesArray = tmp
            end
        end
        return seriesArray
    end