2017-03-06 10 views
1

ウェブサイトから複数の動的ファイルをrubyでダウンロードしたい。 は、5つの異なるファイルがあり、それらはすべての基本的な構造を持っている:ruby​​を使用して複数の動的ファイルをダウンロードするにはどうすればよいですか?

BACKUP_ YYYY - MM - DD - HHMM _JRAddOns_Die_Pflegeserie_fr_ ID - CONTENTを。 ENDING

大胆に書かれた部分はすべて動的であり、置き換えなければなりません。

ファイルは次のようになります。

backup_2017-03-06-1020_JRAddOns_Die_Pflegeserie_fr_2867ed5aba3d-db.gz 
backup_2017-03-06-1020_JRAddOns_Die_Pflegeserie_fr_2867ed5aba3d-others.zip 
backup_2017-03-06-1020_JRAddOns_Die_Pflegeserie_fr_2867ed5aba3d-plugins.zip 
backup_2017-03-06-1020_JRAddOns_Die_Pflegeserie_fr_2867ed5aba3d-themes.zip 
backup_2017-03-06-1020_JRAddOns_Die_Pflegeserie_fr_2867ed5aba3d-uploads.zip 

誰かが助けてもらえますか?

編集: 私も、文句を言わない長い働くことの非動的なバージョンを試してみました:

puts 'Starting Download...' 
require 'net/http' 
Net::HTTP.start("jr-addons.de") do |http| 
    resp = http.get("/btemp/backup_2016-10-23-1520_JRAddOns_Die_Pflegeserie_fr_2867ed5aba3d-db.gz") 
    open("backup_2016-10-23-1520_JRAddOns_Die_Pflegeserie_fr_2867ed5aba3d-db.gz", "wb") do |file| 
     file.write(resp.body) 
    end 
    resp = http.get("/btemp/backup_2016-10-23-1520_JRAddOns_Die_Pflegeserie_fr_2867ed5aba3d-others.zip") 
    open("backup_2016-10-23-1520_JRAddOns_Die_Pflegeserie_fr_2867ed5aba3d-others.zip", "wb") do |file| 
     file.write(resp.body) 
    end 
    resp = http.get("/btemp/backup_2016-10-23-1520_JRAddOns_Die_Pflegeserie_fr_2867ed5aba3d-plugins.zip") 
    open("backup_2016-10-23-1520_JRAddOns_Die_Pflegeserie_fr_2867ed5aba3d-plugins.zip", "wb") do |file| 
     file.write(resp.body) 
    end 
    resp = http.get("/btemp/backup_2016-10-23-1520_JRAddOns_Die_Pflegeserie_fr_2867ed5aba3d-themes.zip") 
    open("backup_2016-10-23-1520_JRAddOns_Die_Pflegeserie_fr_2867ed5aba3d-themes.zip", "wb") do |file| 
     file.write(resp.body) 
    end 
    resp = http.get("/btemp/backup_2016-10-23-1520_JRAddOns_Die_Pflegeserie_fr_2867ed5aba3d-uploads.zip") 
    open("backup_2016-10-23-1520_JRAddOns_Die_Pflegeserie_fr_2867ed5aba3d-uploads.zip", "wb") do |file| 
     file.write(resp.body) 
    end 
end 
puts 'Done.' 
+1

あなたには、いくつかのコードを追加してくださいすることができます。あなたはまだ何を試しましたか? –

+0

Google "ruby string interpolation" –

+0

@DeepakMahakale編集しました –

答えて

3
DATES = %w|03-06-1020| 
SUFFIXES = %w|db others plugins themes uploads| 

puts 'Starting Download...' 
require 'net/http' 
Net::HTTP.start("jr-addons.de") do |http| 
    DATES.product(SUFFIXES).each do |(date, suffix)| 
    file = "/foo_#{date}_bar-#{suffix}.baz" 
    puts file 
    # do other stuff with this file 
    end 
end 
+0

なぜ%w()の代わりに '%w ||'が好奇妙なのですか? –

+0

具体的な理由はありますか? –

+0

@DeepakMahakaleいいえ、具体的な理由はありません。ちょうど癖の問題です。括弧内の単語の配列は、縦線のある単語よりも頻繁に発生するためです。 – mudasobwa

0

はここで、時間と文字列と文字列の補間のための基本的な例です。

あなただけに必要な日数、IDと拡張子を持つ配列を定義し、反復処理する必要があるでしょう:

time = Time.new(2017, 3, 6, 10, 20) 
id = '2867ed5aba3d' 
content = 'db' 
ext = 'gz' 

puts time.strftime("backup_%Y-%m-%d-%H%M_JRAddOns_Die_Pflegeserie_fr_#{id}-#{content}.#{ext}") 
#=> backup_2017-03-06-1020_JRAddOns_Die_Pflegeserie_fr_2867ed5aba3d-db.gz 
関連する問題