#### KEWEGO
module Kewego
def prepare_access_token(oauth_token, oauth_token_secret)
consumer = OAuth::Consumer.new("APIKey", "APISecret",
{ :site => 'http://login.kewego.com/api/getAuthToken',
})
token_hash = { :oauth_token => oauth_token,
}
access_token = OAuth::AccessToken.from_hash(consumer, token_hash )
return access_token
end
def format_flux_kewe(title_var,descpt_var,meta_var,categ_var,file_2_upload_var)
#file_xml_out =File.open("data/poubelle/datas.xml",'w')
meta = meta_var.split(/,/)
doc = REXML::Document.new
#xm = doc.add_element("?xml")
#xm.add_attribute("version", "1.0")
#xm.add_attribute("encoding", "UTF-8")
doc.add_element("kewego_call")
doc.root.add_element("params")
vidz = doc.root.elements[1]
title = REXML::Element.new("title")
title.text = title_var
vidz.elements << title
desc = REXML::Element.new("description")
desc.text = descpt_var
vidz.elements << desc
cat = REXML::Element.new("categories")
cat.add_element("category")
cat.elements["category"].text = categ_var
vidz.elements << cat
key = REXML::Element.new("keywords")
meta.each{ |keywrd|
sous_key = key.add_element("keyword")
sous_key.text = keywrd
}
vidz.elements << key
lang = REXML::Element.new("language")
lang.text = "fr"
vidz.elements << lang
#file_xml_out.write doc
#file_xml_out.close
return doc
end
def query_to_multipart(boundary, attributes={}) #:nodoc:
file = attributes.delete(:file)
params = [file_to_multipart("file", File.basename(file),
"video/mp4", File.open(file))]
attributes.each_pair{|k,v| params << text_to_multipart(k.to_s, v.to_s)}
params.map do |p|
'--' + boundary + "\r\n" + p
end.join('') + "--" + boundary + "--\r\n"
end
def text_to_multipart(key,value)
text = ""
value = [value] if !value.instance_of? Array
value.map{|v| text += "Content-Disposition: form-data; name=\"#{key}\"\r\n\r\n#{v}\r\n"}
text
end
def file_to_multipart(key,filename,mime_type,content)
mem_buf = content.read
return "Content-Disposition: form-data; name=\"#{key}\"; filename=\"#{filename}\"\r\n" +
"Content-Type: #{mime_type}\r\n" +
"\r\n" +
"#{mem_buf}\r\n"
#"FILE_DATA_GOES_HERE\r\n"
end
def kewego_load(title_var,descpt_var,meta_var,file_vid, categ_var, subcat, username, password, api_key)
#puts "enter the KEWEGO loader \r\n"
#puts file_2_upload_var
##############################################
get_token_url = 'http://api.kewego.com/app/getToken/' # app_token
get_auth_token_url = 'http://login.kewego.com/api/getAuthToken/' #auth_token
upload_file_url = 'http://api.kewego.com/upload/uploadFile/index.php'
upload_video_url = "http://upload.kewego.com/api/uploadVideo/index.php"
get_related_page_url = "http://api.kewego.com/video/getRelatedPageUrl/"
## APP TOKEN
@ap_token = api_post(get_token_url, {'appKey' => api_key}, "kewego_response/message/appToken")
## AUTH TOKEN
@auth_token = api_post(get_auth_token_url, {'username' => username, 'password'=> password, 'appToken' => @ap_token}, "kewego_response/message/token")
## FORMAT XML
xml_data3 = ''
file_xml_out = format_flux_kewe(title_var,descpt_var,meta_var,categ_var,file_vid)
## PREPARE UPLOAD
s_md5 = Digest::MD5.hexdigest(file_vid)
file_vid_z = File.open(file_vid,'r')
# uploadFile
boundary = Digest::MD5.hexdigest(DateTime.now.to_s)
query = query_to_multipart(boundary, {:file => file_vid, 'xml_data' => file_xml_out, 'appToken' => @ap_token, 'token' => @auth_token})
uri = URI.parse( upload_video_url )
puts "Requesting #{uri.to_s}"
http = Net::HTTP.new(uri.host, uri.port)
xml_data5 = http.start.post2(uri.request_uri,query,{"Content-type" => "multipart/form-data; boundary=" + boundary}).body
doc = REXML::Document.new(xml_data5)
sig_node = doc.get_elements("/kewego_response/message/sig").first
if sig_node
sig = sig_node.text
#url = api_post( get_related_page_url, {'sig' => sig, 'appToken' => @ap_token}, "/kewego_response/message/url")
#url
sig
end
end
def api_post(url, params, result_xpath)
puts "Requesting #{url}"
xml_data = Net::HTTP.post_form( URI.parse( url ), params).body
doc = REXML::Document.new(xml_data)
elements = doc.get_elements(result_xpath).first
error_node = doc.get_elements("/kewego_response/kewego_error").first
if elements
unless error_node.nil?
puts "WARNING: #{error_node.text}"
end
#puts "Returning: #{elements.text}
elements.text
else
if error_node.nil?
raise "Error finding #{result_xpath} in #{xml_data}"
else
raise error_node.text
end
end
end
end
#### FIN KEWEGO