Class: GrandCloud::Video

Inherits:
Object show all
Defined in:
lib/grand_cloud/video.rb

Instance Method Summary collapse

Instance Method Details

#create(title, pass_encoding = false, &block) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/grand_cloud/video.rb', line 23

def create title, pass_encoding=false, &block
  creation = Base.send_request({
    :method => 'post',
    :uri => '/video',
    :additional_params => {
      :Title => title,
      :BypassEncoding => pass_encoding
    }
  })
  creation.callback { block.call(GrandCloud.nori.parse(creation.response)['CreateVideoResponse']) }

  creation.errback do 
    GrandCloud.logger.error("Error is: #{creation.error}, requesting error...")
    block.call(nil)
  end
end

#destory(id) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/grand_cloud/video.rb', line 126

def destory id
  json = common_request do 
    Base.send_request({
      :method => 'delete',
      :uri => "/videos/#{id}"
    })
  end
  json['RemoveVideosResponse']['return']
rescue Error::ResponseError => e
  GrandCloud.logger.error(e)
  return false
end

#get(id) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/grand_cloud/video.rb', line 8

def get id
  json = common_request do
    Base.send_request({:method => 'get', :uri => "/video/#{id}"})
  end
  wrap_object(json['DetailVideoResponse']['video'])
rescue Error::ResponseError => e
  GrandCloud.logger.error(e)
  return nil
end

#get_default_program_idObject

15622



196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/grand_cloud/video.rb', line 196

def get_default_program_id
  programs = get_programs 
  unless programs.empty?
    result = programs['ListProgramsResponse']['programSet'].select{|a| a['programName'] == '默认方案'}
    result.empty? ? nil : result[0]['programId']
  else
    nil
  end
rescue Error::ResponseError => e
  GrandCloud.logger.error(e)
  return nil
end

#get_programsObject



183
184
185
186
187
188
189
190
191
192
193
# File 'lib/grand_cloud/video.rb', line 183

def get_programs
  common_request do
    Base.send_request({
      :method => 'get',
      :uri => '/programs'
    })
  end
rescue Error::ResponseError => e
  GrandCloud.logger.error(e)
  return []
end

#import_ku6(ku6vid) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
# File 'lib/grand_cloud/video.rb', line 149

def import_ku6 ku6vid
  common_request do
    Base.send_request({
      :method => 'get',
      :uri => '/videos/importation/ku6',
      :additional_params => {
        :Ku6vids => ku6vid
      }
    })
  end
end

#listObject



139
140
141
142
143
144
145
146
147
# File 'lib/grand_cloud/video.rb', line 139

def list
  json = common_request do
    Base.send_request({:method => 'get', :uri => "/videos"})
  end
  json['ListVideoResponse']['videoSet']['item'].map{|v| wrap_object(v) }
rescue Error::ResponseError => e
  GrandCloud.logger.error(e)
  return []
end

#publish(id, programId) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/grand_cloud/video.rb', line 161

def publish id, programId
  publish_attributes = %w(vid ku6vid publishedJavaScript publishedHtml publishedSwf)

  json = common_request do 
    Base.send_request({
      :method => 'get',
      :uri => "/video/#{id}/publication",
      :additional_params => {
        :ProgramId => programId
      }
    })
  end

  json = json['publication'].select {|k, v| publish_attributes.include?(k) }.inject({}) do |r, (k, v)|
    r.merge!(k => (v.is_a?(Hash) ? v['value'] : v))
  end
  wrap_object(json)
rescue Error::ResponseError => e
  GrandCloud.logger.error(e)
  return nil
end

#pull_by_vms(title, download_url, &block) ⇒ Object



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
# File 'lib/grand_cloud/video.rb', line 77

def pull_by_vms title, download_url, &block 
  return EM.stop unless block_given?

  self.create(title) do |rep|

    return block.call(nil) unless rep

    GrandCloud.logger.warn(rep)

    req = Base.send_request({
      :method => 'post',
      :uri => '/video/urlupload',
      :additional_params => {
        :sid => rep['sid'],
        :videoUrl => CGI::escape(download_url),
        :uploadUrl => rep['uploadUrl'],
        :accessKey => Base.snda_access_key_id,
        :secretKey => Base.secret_access_key,
        :oneway => true
      },
      :timeout => {
        :inactivity_timeout => 0
      }

    })
    callback(req, block.to_proc, rep.select{|k, v| %W{sid vid}.include?(k)})
  end
end

#runObject

u must start an event loop when u uploading an media



19
20
21
# File 'lib/grand_cloud/video.rb', line 19

def run
  EM.run { yield }
end

#update(id, title, desc) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/grand_cloud/video.rb', line 106

def update id, title, desc
  json = common_request do
    Base.send_request({
      :method => 'put',
      :uri => "/video/#{id}",
      :request_params => {
        :body => {
          :Vid => id,
          :Title => title,
          :Description => desc
        }
      }
    })
  end
  json['ModifyVideoResponse']['return']
rescue Error::ResponseError => e
  GrandCloud.logger.error(e)
  return false
end

#upload(title, file, options = {}, pass_encoding = false, &block) ⇒ Object

upload is an async method you should pass an original_filename on options when you uploading a temp file



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
# File 'lib/grand_cloud/video.rb', line 42

def upload title, file, options={}, pass_encoding=false, &block
  return EM.stop if ((file.class == Tempfile) && (!options[:original_filename])) || !block_given?

  pn_file = Pathname(file)

  title = get_video_title(title, options[:original_filename], pn_file)
  extname = get_video_extname(options[:original_filename], pn_file)

  self.create(title, pass_encoding) do |rep| 

    return block.call(nil) unless rep

    GrandCloud.logger.warn(rep)

    req = Base.file_upload({
      :method => 'post',
      :uri => '/vmsUpload.htm',
      :url => rep['uploadUrl'],
      :pn_file => pn_file,
      :host => rep['uploadUrl'].gsub(/^http:\/\/(.+)\/.+/, '\1'),
      :request_params => {
        :sid => rep['sid'],
        :cfrom => 'client',
        :filesize => pn_file.size,
        :ext => extname
      },
      :timeout => {
        :inactivity_timeout => 0
      }

    })
    callback(req, block.to_proc, rep.select{|k, v| %W{sid vid}.include?(k)})
  end
end