2014-05-29 17:27:02 +00:00
|
|
|
require 'httparty'
|
|
|
|
require 'tempfile'
|
|
|
|
|
|
|
|
class AmazonCopy
|
|
|
|
def download_video videolisting
|
2014-05-29 18:53:10 +00:00
|
|
|
if not videolisting.on_s3
|
|
|
|
download_file_and_upload_to_s3 videolisting.get_video_url,
|
|
|
|
videolisting.videocode,
|
|
|
|
"mp4",
|
|
|
|
"bvideos"
|
2014-05-29 17:56:37 +00:00
|
|
|
|
2014-05-29 18:53:10 +00:00
|
|
|
download_file_and_upload_to_s3 videolisting.get_preview_url,
|
|
|
|
videolisting.videocode,
|
|
|
|
"mp4",
|
|
|
|
"bvideos_preview"
|
2014-05-29 18:34:52 +00:00
|
|
|
|
2014-05-29 18:53:10 +00:00
|
|
|
videolisting.on_s3 = true
|
|
|
|
videolisting.save
|
2014-05-29 17:27:02 +00:00
|
|
|
end
|
2014-05-29 18:53:10 +00:00
|
|
|
end
|
2014-05-29 20:44:23 +00:00
|
|
|
handle_asynchronously :download_video
|
|
|
|
|
2014-05-29 18:53:10 +00:00
|
|
|
|
|
|
|
def download_file_and_upload_to_s3 url, videocode, extension, prefix
|
|
|
|
file = Tempfile.new ["video_", ".#{extension}"], "#{Rails.root}/tmp"
|
2014-05-29 18:34:52 +00:00
|
|
|
begin
|
2014-05-29 18:53:10 +00:00
|
|
|
file.write HTTParty.get(url).parsed_response.force_encoding("utf-8")
|
2014-05-29 18:34:52 +00:00
|
|
|
file.flush
|
|
|
|
|
|
|
|
s3 = AWS::S3.new
|
2014-05-29 18:53:10 +00:00
|
|
|
obj = s3.buckets[ENV['VIDEO_BUCKET']].objects["#{prefix}/#{videocode}.mp4"]
|
2014-05-29 18:34:52 +00:00
|
|
|
obj.write(file)
|
|
|
|
|
|
|
|
ensure
|
|
|
|
file.close
|
|
|
|
file.unlink
|
|
|
|
end
|
2014-05-29 17:27:02 +00:00
|
|
|
end
|
2014-05-29 18:53:10 +00:00
|
|
|
|
2014-05-29 17:27:02 +00:00
|
|
|
end
|