我正在尝试创建第一个解析TMDB API数据的应用程序。
当我尝试加载和解析一个URL时,一切正常:
uri = URI.parse("http://api.themoviedb.org/3/tv/#URI.escape(@id)}?api_key=#{@api_key}")
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Get.new(uri.request_uri)
response = http.request(request)
@results = JSON.load(response.body)
但现在我想在以下位置解析JSON:
/tv/{tv_id}/season/{season_number}/episode/{episode_number}
其中:{tv_id}
为@id = params[:id]
{season_number}
和{episode_number}
是第一个网址产生的季节和剧集。
我尝试了多种方法,但每次都会出现随机错误。
完整代码位于Github。 工作应用是TOMDB App on Heroku。
我正在尝试在与Serie相同的页面中显示季节标题和剧集。 例如,当我解析时:
https://api.themoviedb.org/3/tv/2432?api_key=**********
我明白了:
{
"backdrop_path":"/hl9mC6fc2adfeGpI1ijKCfQ0KzI.jpg",
"name":"Taken",
"number_of_episodes":10,
"number_of_seasons":1
}
所以我有一集有10集。对于每集我都有一个URL,将剧集编号{1..5..10}
替换为:
https://api.themoviedb.org/3/tv/8681/season/1/episode/*1*?api_key=********
我想为每个剧集显示剧集名称,因此一集的JSON结果如下:
{
"name":"Beyond the Sky","overview":"......",
"id":183273,
"vote_average":0.0,
"vote_count":0
}
答案 0 :(得分:1)
您需要对该请求使用https,请参阅我用#**标记的需要更改的部分。备注使用Net :: HTTP.start而不是Net :: HTTP.new。 顺便说一句不错的应用程序。
require 'net/http'
require 'net/https' #*add*
require 'json'
require 'open-uri'
url = 'https://api.themoviedb.org/3/tv/1418/season/1/episode/1?api_key=**************************&language=en-US'
uri = URI.parse(url)
# *adapt following line*
http = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https', :verify_mode => OpenSSL::SSL::VERIFY_NONE)
request = Net::HTTP::Get.new(uri.request_uri)
response = http.request(request)
json = JSON.load(response.body)
# {"air_date"=>"2007-09-24", "crew"=>[{"id"=>157437, "credit_id"=>"5256cfee19c2956ff60a280c", "name"=>"James Burrows", "department"=>"Directing", "job"=>"Director", "profile_path"=>"/lTcRumFOm6HkfOyPuUElV4l4n4r.jpg"}, {"id"=>160172, "credit_id"=>"5256cfbc19c2956ff60a0483", "name"=>"Chuck Lorre", "department"=>"Writing", "job"=>"Writer", "profile_path"=>"/btpYlMV71sjQXrV142I9kogEINI.jpg"}, {"id"=>163528, "credit_id"=>"5256cfbd19c2956ff60a04f0", "name"=>"Bill Prady", "department"=>"Writing", "job"=>"Writer", "profile_path"=>"/duXUvo8JtivQR0BHiXHGQwoNYB4.jpg"}, {"id"=>1480308, "credit_id"=>"55881d379251416818001c2b", "name"=>"Janice Zoladz", "department"=>"Costume & Make-Up", "job"=>"Hairstylist", "profile_path"=>nil}, {"id"=>1480289, "credit_id"=>"558819ffc3a36836ea006145", "name"=>"Joe Bella", "department"=>"Production", "job"=>"Co-Producer", "profile_path"=>nil}, {"id"=>1232374, "credit_id"=>"55881a1492514179f6003f6e", "name"=>"Michael Collier", "department"=>"Production", "job"=>"Producer", "profile_path"=>nil}, {"id"=>1480291, "credit_id"=>"55881a269251411efc000f6e", "name"=>"Toti Levine", "department"=>"Production", "job"=>"Associate Producer", "profile_path"=>nil}], "episode_number"=>1, "guest_stars"=>[{"id"=>1118085, "name"=>"Brian Patrick Wade", "credit_id"=>"5256cfc919c2956ff60a0c8f", "character"=>"Kurt", "order"=>0, "profile_path"=>"/6y0Hd1xLC5Nitedg2GQ90DtxxDb.jpg"}, {"id"=>157085, "name"=>"Vernee Watson-Johnson", "credit_id"=>"5256cfd719c2956ff60a1747", "character"=>"", "order"=>1, "profile_path"=>"/dRXXPoeAAbl1PhURCJfKCDoZiUn.jpg"}], "name"=>"Pilot", "overview"=>"Brilliant physicist roommates Leonard and Sheldon meet their new neighbor Penny, who begins showing them that as much as they know about science, they know little about actual living.", "id"=>64766, "production_code"=>nil, "season_number"=>1, "still_path"=>"/rxWlBXZhGWhumbLB8gAHyyW3ITD.jpg", "vote_average"=>7.39285714285714, "vote_count"=>14}
编辑:要获得一个季节的所有剧集,你只需要从1到剧集的数量,并组装并请求新的网址。 你必须做同样的事情来列举一个系列的所有季节,但我给你留下了一些超过8的工作:)成功!
require 'net/http'
require 'net/https'
require 'json'
require 'open-uri'
require 'pp'
# we have to do this more than once, so let's make it DRY
def get_json url
uri = URI.parse(url)
http = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https', :verify_mode => OpenSSL::SSL::VERIFY_NONE) #**
request = Net::HTTP::Get.new(uri.request_uri)
response = http.request(request)
JSON.load(response.body)
end
API_KEY = '****************************'
url = "https://api.themoviedb.org/3/tv/2432?api_key=#{API_KEY}"
season = get_json url
season['number_of_episodes'].to_i.times do |episode|
episode += 1 # times starts from 0 , so we add 1
url = "https://api.themoviedb.org/3/tv/8681/season/1/episode/#{episode}?api_key=#{API_KEY}"
episode = get_json url
# pp episode
puts episode['name']
end
gives
# Great White Shark: The True Story of Jaws
# Polar Bear: The Arctic Warrior
# Crocodile: The Smiling Predator
# Leopard: The Agent of Darkness
# Eagle: The Master of the Skies
# Humpback Whale: The Giant of the Oceans
# Wolf: The Legendary Outlaw
# Tiger: The Elusive Princess
# Lions: Spy in the Den
# Grizzly: Face to Face