ruby kewego upload video


SUBMITTED BY: Guest

DATE: Sept. 29, 2014, 5:53 p.m.

FORMAT: Text only

SIZE: 5.7 kB

HITS: 1201

  1. #### KEWEGO
  2. module Kewego
  3. def prepare_access_token(oauth_token, oauth_token_secret)
  4. consumer = OAuth::Consumer.new("APIKey", "APISecret",
  5. { :site => 'http://login.kewego.com/api/getAuthToken',
  6. })
  7. token_hash = { :oauth_token => oauth_token,
  8. }
  9. access_token = OAuth::AccessToken.from_hash(consumer, token_hash )
  10. return access_token
  11. end
  12. def format_flux_kewe(title_var,descpt_var,meta_var,categ_var,file_2_upload_var)
  13. #file_xml_out =File.open("data/poubelle/datas.xml",'w')
  14. meta = meta_var.split(/,/)
  15. doc = REXML::Document.new
  16. #xm = doc.add_element("?xml")
  17. #xm.add_attribute("version", "1.0")
  18. #xm.add_attribute("encoding", "UTF-8")
  19. doc.add_element("kewego_call")
  20. doc.root.add_element("params")
  21. vidz = doc.root.elements[1]
  22. title = REXML::Element.new("title")
  23. title.text = title_var
  24. vidz.elements << title
  25. desc = REXML::Element.new("description")
  26. desc.text = descpt_var
  27. vidz.elements << desc
  28. cat = REXML::Element.new("categories")
  29. cat.add_element("category")
  30. cat.elements["category"].text = categ_var
  31. vidz.elements << cat
  32. key = REXML::Element.new("keywords")
  33. meta.each{ |keywrd|
  34. sous_key = key.add_element("keyword")
  35. sous_key.text = keywrd
  36. }
  37. vidz.elements << key
  38. lang = REXML::Element.new("language")
  39. lang.text = "fr"
  40. vidz.elements << lang
  41. #file_xml_out.write doc
  42. #file_xml_out.close
  43. return doc
  44. end
  45. def query_to_multipart(boundary, attributes={}) #:nodoc:
  46. file = attributes.delete(:file)
  47. params = [file_to_multipart("file", File.basename(file),
  48. "video/mp4", File.open(file))]
  49. attributes.each_pair{|k,v| params << text_to_multipart(k.to_s, v.to_s)}
  50. params.map do |p|
  51. '--' + boundary + "\r\n" + p
  52. end.join('') + "--" + boundary + "--\r\n"
  53. end
  54. def text_to_multipart(key,value)
  55. text = ""
  56. value = [value] if !value.instance_of? Array
  57. value.map{|v| text += "Content-Disposition: form-data; name=\"#{key}\"\r\n\r\n#{v}\r\n"}
  58. text
  59. end
  60. def file_to_multipart(key,filename,mime_type,content)
  61. mem_buf = content.read
  62. return "Content-Disposition: form-data; name=\"#{key}\"; filename=\"#{filename}\"\r\n" +
  63. "Content-Type: #{mime_type}\r\n" +
  64. "\r\n" +
  65. "#{mem_buf}\r\n"
  66. #"FILE_DATA_GOES_HERE\r\n"
  67. end
  68. def kewego_load(title_var,descpt_var,meta_var,file_vid, categ_var, subcat, username, password, api_key)
  69. #puts "enter the KEWEGO loader \r\n"
  70. #puts file_2_upload_var
  71. ##############################################
  72. get_token_url = 'http://api.kewego.com/app/getToken/' # app_token
  73. get_auth_token_url = 'http://login.kewego.com/api/getAuthToken/' #auth_token
  74. upload_file_url = 'http://api.kewego.com/upload/uploadFile/index.php'
  75. upload_video_url = "http://upload.kewego.com/api/uploadVideo/index.php"
  76. get_related_page_url = "http://api.kewego.com/video/getRelatedPageUrl/"
  77. ## APP TOKEN
  78. @ap_token = api_post(get_token_url, {'appKey' => api_key}, "kewego_response/message/appToken")
  79. ## AUTH TOKEN
  80. @auth_token = api_post(get_auth_token_url, {'username' => username, 'password'=> password, 'appToken' => @ap_token}, "kewego_response/message/token")
  81. ## FORMAT XML
  82. xml_data3 = ''
  83. file_xml_out = format_flux_kewe(title_var,descpt_var,meta_var,categ_var,file_vid)
  84. ## PREPARE UPLOAD
  85. s_md5 = Digest::MD5.hexdigest(file_vid)
  86. file_vid_z = File.open(file_vid,'r')
  87. # uploadFile
  88. boundary = Digest::MD5.hexdigest(DateTime.now.to_s)
  89. query = query_to_multipart(boundary, {:file => file_vid, 'xml_data' => file_xml_out, 'appToken' => @ap_token, 'token' => @auth_token})
  90. uri = URI.parse( upload_video_url )
  91. puts "Requesting #{uri.to_s}"
  92. http = Net::HTTP.new(uri.host, uri.port)
  93. xml_data5 = http.start.post2(uri.request_uri,query,{"Content-type" => "multipart/form-data; boundary=" + boundary}).body
  94. doc = REXML::Document.new(xml_data5)
  95. sig_node = doc.get_elements("/kewego_response/message/sig").first
  96. if sig_node
  97. sig = sig_node.text
  98. #url = api_post( get_related_page_url, {'sig' => sig, 'appToken' => @ap_token}, "/kewego_response/message/url")
  99. #url
  100. sig
  101. end
  102. end
  103. def api_post(url, params, result_xpath)
  104. puts "Requesting #{url}"
  105. xml_data = Net::HTTP.post_form( URI.parse( url ), params).body
  106. doc = REXML::Document.new(xml_data)
  107. elements = doc.get_elements(result_xpath).first
  108. error_node = doc.get_elements("/kewego_response/kewego_error").first
  109. if elements
  110. unless error_node.nil?
  111. puts "WARNING: #{error_node.text}"
  112. end
  113. #puts "Returning: #{elements.text}
  114. elements.text
  115. else
  116. if error_node.nil?
  117. raise "Error finding #{result_xpath} in #{xml_data}"
  118. else
  119. raise error_node.text
  120. end
  121. end
  122. end
  123. end
  124. #### FIN KEWEGO

comments powered by Disqus