Class: GrandCloud::Video

Inherits:
Object
  • 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
# 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(JSON.parse(creation.response)) }

  creation.errback do 
    GrandCloud.logger.error('requesting error...')
  end
end

#destory(id) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/grand_cloud/video.rb', line 111

def destory id
  json = common_request do 
    Base.send_request({
      :method => 'delete',
      :uri => "/videos/#{id}"
    })
  end
  json['returnValue']
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['video'])
rescue Error::ResponseError => e
  GrandCloud.logger.error(e)
  return nil
end

#get_default_program_idObject

15622



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

def get_default_program_id
  programs = get_programs 
  unless programs.empty?
    result = programs['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



168
169
170
171
172
173
174
175
176
177
178
# File 'lib/grand_cloud/video.rb', line 168

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



134
135
136
137
138
139
140
141
142
143
144
# File 'lib/grand_cloud/video.rb', line 134

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

#listObject



124
125
126
127
128
129
130
131
132
# File 'lib/grand_cloud/video.rb', line 124

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

#publish(id, programId) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/grand_cloud/video.rb', line 146

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



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/grand_cloud/video.rb', line 67

def pull_by_vms title, download_url, &block 
  self.create(title) do |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
      },
      :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



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/grand_cloud/video.rb', line 91

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['returnValue']
rescue Error::ResponseError => e
  GrandCloud.logger.error(e)
  return false
end

#upload(title, file, pass_encoding = nil, &block) ⇒ Object

upload is an async method



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

def upload title, file, pass_encoding=nil, &block
  pn_file = Pathname(file)
  self.create(title.empty? ? pn_file.basename(pn_file.extname) : title, pass_encoding) do |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 => pn_file.extname
      },
      :timeout => {
        :inactivity_timeout => 0
      }

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