Created
September 9, 2011 20:51
-
-
Save hexatridecimal/1207295 to your computer and use it in GitHub Desktop.
Sample creation code.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'open-uri' | |
def get_file_path() | |
name = ActiveSupport::SecureRandom.hex(16) | |
Rails.root.join('processing', | |
name[0], | |
name[1], | |
name[2 .. name.length]).to_s + '.pdf' | |
end | |
def call_create_sample(file_path) | |
script_name = Rails.root.join('lib', 'create_sample.pl') | |
logo = Rails.root.join('lib', 'logo.png') | |
samples_dir = Rails.root.join('samples') | |
STDERR.puts "#{script_name} #{file_path} #{logo} #{samples_dir}" | |
output_pdf = `#{script_name} #{file_path} #{logo} #{samples_dir}` | |
if $?.success? == true | |
if output_pdf != nil and output_pdf =~ /\.pdf$/ | |
return output_pdf | |
else | |
return nil | |
end | |
else | |
return nil | |
end | |
end | |
def process_file(doc, d) | |
url = doc.download_url('pdf') | |
file_path = get_file_path() | |
open(url, 'rb') do |remote| | |
File.open(file_path, 'wb') do |local| | |
local.write(remote.read) | |
end | |
end | |
output_pdf = call_create_sample(file_path) | |
if output_pdf == nil | |
doc = Scribd::Document.upload(:file => output_pdf) | |
d.scribd_sample_id = doc.doc_id | |
d.scribd_sample_key = doc.access_key | |
d.status = 'DONE' | |
d.save | |
else | |
d.status = 'ERROR' | |
d.save | |
end | |
end | |
namespace :ns do | |
task :create_samples => :environment do | |
Scribd::User.login APP.scribd.login | |
APP.scribd.password | |
Document.find(:all, :conditions => "status='PROCESSING'").each do |d| | |
doc = Scribd::Document.find(d.scribd_document_id.to_i) | |
status = doc.conversion_status() | |
if status == 'DONE' | |
process_file(doc, d) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment