mojodna (owner)

Revisions

gist: 108149 Download_button fork
public
Public Clone URL: git://gist.github.com/108149.git
Embed All Files: show embed
netflix.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env ruby -rubygems
 
require 'oauth'
require 'optparse'
 
options = {}
 
option_parser = OptionParser.new do |opts|
  opts.banner = "Usage: #{$0} [options] <query>"
 
  opts.on("--app-name NAME", "Specifies the name of the applications.") do |v|
    options[:app_name] = v
  end
 
  opts.on("--consumer-key KEY", "Specifies the consumer key to use.") do |v|
    options[:consumer_key] = v
  end
 
  opts.on("--consumer-secret SECRET", "Specifies the consumer secret to use.") do |v|
    options[:consumer_secret] = v
  end
 
  opts.on("--token TOKEN", "Specifies the access token to use.") do |v|
    options[:oauth_token] = v
  end
 
  opts.on("--secret SECRET", "Specifies the access token secret to use.") do |v|
    options[:oauth_token_secret] = v
  end
end
 
option_parser.parse!
 
if options[:consumer_key].nil? || options[:consumer_secret].nil? || options[:app_name].nil?
  puts option_parser.help
  exit 1
end
 
# need to define methods: app_name, developer_key, developer_secret
consumer = OAuth::Consumer.new \
  options[:consumer_key],
  options[:consumer_secret],
  :site => "http://api.netflix.com",
  :request_token_url => "http://api.netflix.com/oauth/request_token",
  :access_token_url => "http://api.netflix.com/oauth/access_token",
  :authorize_url => "https://api-user.netflix.com/oauth/login"
 
if options[:oauth_token].nil? || options[:oauth_token_secret].nil?
  # no token and secret were provided, get the user to authorize the app
 
  request_token = consumer.get_request_token
 
  authorize_url = request_token.authorize_url \
    :oauth_consumer_key => options[:consumer_key],
    :application_name => options[:app_name]
 
  puts "Please visit this url to authorize:"
  puts authorize_url
 
  puts "Press return to continue..."
  gets
 
  # TODO this is returning a 401
  access_token = request_token.get_access_token
 
  puts "For future reference:"
  puts "Access token: #{access_token.token}"
  puts "Token secret: #{access_token.secret}"
else
  access_token = OAuth::AccessToken.new \
    consumer,
    options[:oauth_token],
    options[:oauth_token_secret]
end
 
# XML
response = access_token.get("/users/#{access_token.params[:user_id]}")
xml = response.body
 
puts "XML response:"
puts response.body
 
# JSON
response = access_token.get("/users/#{access_token.params[:user_id]}?output=json")
json = response.body
 
puts "JSON response:"
puts response.body