Netflix OData Episodes

require 'csv'
require 'rubygems'
require 'ruby_odata'

IDS_PATTERN = /http:\/\/api.netflix.com\/catalog\/titles\/programs\/(?\d+)\/(?\d+)/
NAMES       = [
    'Arrested Development',
    'Hemlock Grove',
    'House of Cards',
    'Lilyhammer'
]

service = OData::Service.new 'http://odata.netflix.com/Catalog/'
results = []

NAMES.each do |name|
  service.Titles.filter("Name eq '#{name}' and Type eq 'Series'").expand('Episodes')
  titles = service.execute
  if titles and titles.count > 0 and titles[0].respond_to?(:Episodes)
    titles[0].Episodes.each do |episode|
      ids = IDS_PATTERN.match(episode.NetflixApiId)
      results << {
          id:         episode.Id,
          movie_id:   ids[:movie_id],
          episode_id: ids[:episode_id],
          name:       episode.Name,
          short_name: episode.ShortName,
          runtime:    episode.Runtime,
          rating:     episode.Rating
      }
    end
  end
end

# Save output
filename = File.expand_path('../output/netflix.tsv', __FILE__)
CSV.open(filename, 'wt', {col_sep: "\t"}) do |csv|
  csv << %w(id movie_id episode_id name short_name runtime rating)
  results.each { |result| csv << result.values }
end
main.rb

Sliding Sidebar