Class: YTClient

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/yt_client.rb

Direct Known Subclasses

RubyTube

Constant Summary collapse

UPLOAD_URI =
"http://uploads.gdata.youtube.com/feeds/api/users/default/uploads"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username, password, key, options = {:refresh=>300}) ⇒ YTClient

Returns a new instance of YTClient.



17
18
19
20
21
22
23
24
25
26
# File 'lib/yt_client.rb', line 17

def initialize(username, password, key, options={:refresh=>300})
	@username = username
	@password = password
	@developer_key = key
	@client = GData::Client::YouTube.new
	@client.source = "acer_timeline_contest"
	@client.developer_key = @developer_key
	@token = @client.clientlogin(@username, @password)
	@options = options
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



9
10
11
# File 'lib/yt_client.rb', line 9

def client
  @client
end

#developer_keyObject

Returns the value of attribute developer_key.



9
10
11
# File 'lib/yt_client.rb', line 9

def developer_key
  @developer_key
end

#passwordObject

Returns the value of attribute password.



9
10
11
# File 'lib/yt_client.rb', line 9

def password
  @password
end

#tokenObject

Returns the value of attribute token.



9
10
11
# File 'lib/yt_client.rb', line 9

def token
  @token
end

#usernameObject

Returns the value of attribute username.



9
10
11
# File 'lib/yt_client.rb', line 9

def username
  @username
end

Instance Method Details

#allObject



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/yt_client.rb', line 28

def all
	if @all_vids
		if Time.parse((@all_vids/"updated").text) < (Time.now - @options[:refresh])
			@all_vids = Hpricot.XML(@client.get(self.class.base_uri + "/users/default/uploads").body)
		else
			return @all_vids
		end
	else
		@all_vids = Hpricot.XML(@client.get(self.class.base_uri + "/users/default/uploads").body)
	end
end

#check_video(id) ⇒ Object



44
45
46
# File 'lib/yt_client.rb', line 44

def check_video(id)
	Hpricot.XML(@client.get(self.class.base_uri + "/videos/#{id}").body)
end

#comments(id) ⇒ Object



58
59
60
# File 'lib/yt_client.rb', line 58

def comments(id)
	Hpricot.XML(@client.get(self.class.base_uri + "/videos/#{id}/comments").body)
end

#countObject



40
41
42
# File 'lib/yt_client.rb', line 40

def count
	(@all_vids/"entry").nitems
end

#delete(id) ⇒ Object



111
112
113
# File 'lib/yt_client.rb', line 111

def delete(id)
	response = @client.delete(self.class.base_uri + "/users/default/uploads/#{id}")
end

#ratings(id) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/yt_client.rb', line 48

def ratings(id)
	response = Hpricot.XML(@client.get(self.class.base_uri + "/videos/#{id}").body)
	ratings = (response/"gd:rating")
	if ratings.nitems > 0
		return ratings
	else
		return nil
	end
end

#update(id, xml) ⇒ Object



107
108
109
# File 'lib/yt_client.rb', line 107

def update(id, xml)
	response = @client.put(self.class.base_uri + "/users/default/uploads/#{id}", xml)
end

#upload(file, options = {}) ⇒ Object



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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/yt_client.rb', line 62

def upload(file, options={})
	upload_uri = URI.parse(UPLOAD_URI)
	binary_data = read_file(file)
	request_data = <<-REQDATA
--bbe873dc
Content-Type: application/atom+xml; charset=utf-8

<?xml version="1.0"?>
<entry xmlns="http://www.w3.org/2005/Atom"
xmlns:media="http://search.yahoo.com/mrss/"
xmlns:yt="http://gdata.youtube.com/schemas/2007">
<media:group>
	<media:title type="plain">#{options[:title]}</media:title>
	<media:description type="plain">
		#{options[:description]}
	</media:description>
	<media:category scheme="http://gdata.youtube.com/schemas/2007/categories.cat">
		People
	</media:category>
	<media:keywords>#{options[:keywords]}</media:keywords>
</media:group>
</entry>
--bbe873dc
Content-Type: #{options[:content_type]}
Content-Transfer-Encoding: binary

#{binary_data}
--bbe873dc
REQDATA
	http = Net::HTTP.new(upload_uri.host)
	http.read_timeout = 6000
	headers = {
		'GData-Version' => "2",
		'X-GData-Key' => "key=#{@developer_key}",
		'Slug' => File.basename(file),
		'Authorization' => "GoogleLogin auth=#{@token}",
		'Content-Type' => 'multipart/related; boundary="bbe873dc"',
		'Content-Length' => binary_data.length.to_s,
		'Connection' => 'close'
	}
	res = http.post(upload_uri.path, request_data, headers)
	response = {:code => res.code, :body => Hpricot.XML(res.body)}
	return response
end