如何获取许多 JSON 个 URL 并解析动态内容

How to get many JSON URLs and parse for dynamic content

我正在尝试创建第一个解析来自 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} 是第一个 URL 产生的季节和剧集。 我尝试了多种方法,但每次都会出现随机错误。

完整代码在 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 集。对于每一集,我通过将剧集编号 {1..5..10} 替换为:

来获得 URL
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
}

您需要为该请求使用 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 数到剧集数和 assemble 并请求新的 url。 你必须做同样的事情来列举一个系列的所有季节,但我给你留了一些超过 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