2012-04-03 41 views
1

我将内容上传到托管的CMS。他们提供了一个Ruby Gem,允许我以编程方式上传大量内容。我能够通过编辑其中一个脚本来上传我的内容,但无法通过上传将脚本包含在我的文件中。这是我使用成功的脚本:合并Ruby脚本

#!/usr/bin/env ruby -rubygems 
require File.join(File.dirname(__FILE__), 'authentication') 
require "csv" # faster_csv (ruby 1.9) 

lines = CSV.read(File.join(File.dirname(__FILE__), 'karaoke.csv')) # Exported an Excel file as CSV 
lines.slice!(0) # remove header line 
collection = StorageRoom::Collection.find('my collection ID') 
Song = collection.entry_class 
lines.each do |row| 
karaoke = Song.new(:artist => row[0], :song => row[1], :genre => row[2]) 
    if karaoke.save 
    puts "Misuero Karaoke Latino saved: #{karaoke.artist}, #{karaoke.song}, #{karaoke.genre} " 
    else 
    puts "Misuero Karaoke Latino could not be saved: #{karaoke.errors.join(', ')}" 
    end 
end 

这是脚本,按照他们的榜样,会做文件上传:

#!/usr/bin/env ruby -rubygems 
require File.join(File.dirname(__FILE__), 'authentication') 

path = ::File.expand_path(File.join(File.dirname(__FILE__) + '..', 'spec', 'fixtures', 'image.png')) 
collection = StorageRoom::Collection.find('my collection ID') 

# Upload File 
entry = collection.entry_class.new(:name => "StorageRoom Logo", :file => StorageRoom::File.new_with_filename(path)) 

if entry.save 
    puts "Entry saved (#{entry[:@url]})" 
    puts "URL of the uploaded file is #{entry.image.url}" 
    puts "URL of the automatically generated thumbnail is #{entry.image.url(:thumbnail)}" # Multiple Image Versions can be specified in the interface 
else 
    puts "Entry could not be saved: #{entry.errors.join(', ')}" 
end 

我想组合两个脚本,所以我只跑一个,但我不能让文件上传部分工作。我正尝试上传.mov。文件在哪里与脚本有关?我怎样才能使它们正确命名?我将如何编辑脚本,以便它可以处理多个文件?我该如何合并脚本?

回答

3

这些文件应该在哪里与脚本有关?

File.dirname(__FILE__)返回当前文件的目录和File.join可以让你到其他目录添加到路径(当然'..'意味着父目录)和File.expand_path确保这是一个绝对的,而不是相对路径。

我怎样才能让它们正确命名?

基本上不可能在不知道你的目录结构的情况下回答这个问题。尝试使用ruby控制台并尝试使用the core file methods来感受它。

我该如何编辑脚本,使其能够处理多个文件?

您可能想要在# Upload File之下循环一段文件名。您可能会发现Dir.entries/Dir.glob对于生成此阵列非常有用:Dir api docs

如何合并脚本?

一般而言:连接文件然后删除重复(例如,认证,分配集合var)。你的具体实现将取决于宋类的结构(例如,如果这是你附加文件的对象,你可能只需在你的第一个脚本中添加:file => StorageRoom::File.new_with_filename(path)new语句)和/或csv(例如,如果它具有包含的文件可以通过这个没有摆弄Dir.glob

+0

甜。什么规格灯具部分是什么意思? – hanleyhansen 2012-04-03 19:05:57

+0

我试过这个http://pastebin.com/Rb3Asm2v,但我得到一个错误,说: import_csv.rb:22:语法错误,意外的keyword_end,期待')'不知道这意味着什么。 – hanleyhansen 2012-04-03 23:08:25

+1

'your_rails_root/spec/fixtures/image.png'是一个用于测试的示例图片。 看起来你在第15行有一个错字,这意味着你的文件名字符串没有关闭。尝试'... StorageRoom :: File.new_with_filename(“#{karaoke.artist}#{karaoke.song} .mov”))'。 – RobinGower 2012-04-03 23:48:23