按语言获取 TMDB API
Fetch TMDB API by lang
我正在尝试从电影数据库中获取多种语言的连续剧。
我的目标是更新由 API 编辑的对象 return 以具有以下键:
name_en, name_fr, overview_en, overview_fr
我有这段代码:
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
哪个return(对于一个系列):
[["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"]]
我希望得到以下结果:
[["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"]]
我在 rails 5.2
上使用 ruby
所以,我找到了自己解决问题的方法,这里是相关的修改
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
我正在尝试从电影数据库中获取多种语言的连续剧。
我的目标是更新由 API 编辑的对象 return 以具有以下键:
name_en, name_fr, overview_en, overview_fr
我有这段代码:
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
哪个return(对于一个系列):
[["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"]]
我希望得到以下结果:
[["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"]]
我在 rails 5.2
上使用 ruby所以,我找到了自己解决问题的方法,这里是相关的修改
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