[RUBY] sab2ftp: Automate FTP Uploading

Come up with a useful post-processing script? Share it here!
Post Reply
Multiplier
Newbie
Newbie
Posts: 1
Joined: August 30th, 2011, 2:28 pm

[RUBY] sab2ftp: Automate FTP Uploading

Post by Multiplier »

Introducing sab2ftp
I don't know if a script like this i made before, but I thought I might share mine. Put this script in your script directory and hook it up to a category. Configure the script by choosing a FTP server address, username, password, a upload directory (Note: You need write access) and specify the filetypes you want it to transfer in the array. If you want the script to delete the local folder after upload, set $DELETE_AFTER_TRANSFER to true. Should something fail, adjust the $DEBUG setting.

Note that this is only tested on Linux/UNIX, and requires Ruby installed (should be in any package manager), but it is also possible to use it on Windows. Just install Ruby, create a batch file with something like:

Code: Select all

@echo off
ruby <path_to_script>.rb %1 
... and use that in the script directory and it should all work fine.

And here is the script:

Code: Select all

#!/usr/bin/ruby
require 'net/ftp'
require 'fileutils'

##############################
#
# sab2ftp by Multiplier
#
# Config
#
##############################
$DEBUG = 1 # Debug 0 = Nothing, 1 = Only important, 2 = Everything

$FTPSERVER = '127.0.0.1'
$FTPUSER   = 'username'
$FTPPASS   = 'password' # Leave blank if none
$FTPDIR    = '/'
$FILETYPES = [".avi", ".mkv"]
$DELETE_AFTER_TRANSFER = true

#############################
#
# Logic
#
#############################

$DOWNLOADED_DIR = ARGV[0]
$hasBeenTransfered = false

# Prepare
Dir.chdir($DOWNLOADED_DIR)
$FOLDERNAME = File.basename(Dir.getwd)

class Logging
  def log(msg, important = false)
    if $DEBUG == 1 && important 
      puts msg
    elsif $DEBUG == 2 
      puts msg
    end
  end
end

class Transfer < Logging
  def initialize(server, login, password)
    begin
      @ftp = Net::FTP.new(server)
      log("Connected to #{server}", true)
      @ftp.login(login, password)
      log("Logged in as #{login}", true)
    rescue
      log("Connection failed, check config", true)
    end
  end
  
  def transfer_to(ftpdir)
    begin
      @ftp.chdir(ftpdir)
      log("Changed to transfer directory #{ftpdir}")
    rescue
      log("Could not change to transfer directory #{ftpdir}", true)
    end
  end
  
  def create_directory(rlzdir)
    begin
      @ftp.mkdir(rlzdir)
      log("Created directory #{rlzdir}")
      @ftp.chdir(rlzdir)
      log("Changed directory to #{rlzdir}")
    rescue
      log("Could not change directory to #{rlzdir}", true)
    end
  end
  
  def transfer(file)
    begin
      @ftp.putbinaryfile(file)
      log("Successfully uploaded #{file}", true)
    rescue
      log("Failed uploading #{file}", true)
    end
  end
end

def get_files(directory)
  getfiles = Dir.entries(directory)
  regex =  /(#{Regexp.union($FILETYPES)})$/
  return getfiles.select { |s| s =~ regex}
end

# Setup
ftp = Transfer.new($FTPSERVER, $FTPUSER, $FTPPASS)
ftp.transfer_to($FTPDIR)
ftp.create_directory($FOLDERNAME)

# Transfer
filesToTransfer = get_files($DOWNLOADED_DIR)
filesToTransfer.each {|file| ftp.transfer(file)}

if $DELETE_AFTER_TRANSFER
  logging = Logging.new
  FileUtils.rm_rf($DOWNLOADED_DIR)
  logging.log("Deleted folder #{$DOWNLOADED_DIR}")
end
Nick123
Newbie
Newbie
Posts: 20
Joined: April 4th, 2010, 8:14 am

Re: [RUBY] sab2ftp: Automate FTP Uploading

Post by Nick123 »

Great script! Was wondering if it is possible to add support so it can upload to two ftp servers.

Thanks
Post Reply