Module: Blitzcrank

Includes:
Configurable
Defined in:
lib/blitzcrank.rb,
lib/blitzcrank/copy.rb,
lib/blitzcrank/movie.rb,
lib/blitzcrank/rsync.rb,
lib/blitzcrank/video.rb,
lib/blitzcrank/tv_show.rb,
lib/blitzcrank/version.rb

Defined Under Namespace

Classes: Copy, Movie, Rsync, TVShow, Video

Constant Summary collapse

VERSION =
"0.1.4"

Class Method Summary collapse

Class Method Details

.configure_with(yaml_path) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/blitzcrank.rb', line 21

def self.configure_with(yaml_path)
  begin
    Blitzcrank.config.update(YAML.load(IO.read(yaml_path)))
  rescue Errno::ENOENT
    log(:warning, "YAML configuration file couldn't be found. Using defaults."); return
  rescue Psych::SyntaxError
    log(:warning, "YAML configuration file contains invalid syntax. Using defaults."); return
  end
end

.file_menu(search_array = nil) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/blitzcrank.rb', line 47

def self.file_menu(search_array = nil)
  #very fancy stuff, get listing of our remote files
  fileArray = Blitzcrank.remote_video_file_list.gsub('./','').split("\n")

  availableFiles = Array.new()
  if search_array.nil?
    fileArray.each do |remoteFile|
      availableFiles.push({:path => remoteFile, :name => remoteFile.split('/').last()})
    end
  else
    fileArray.each do |remoteFile|
      search_array.each do |search_text|
        unless /#{search_text.gsub(' ', '.*')}/i.match(remoteFile).nil?
          # we've found a match, store it as an option
          tempHash = {:path => remoteFile, :name => remoteFile.split('/').last()}
          unless availableFiles.include?(tempHash)
            availableFiles.push(tempHash)
          end
        end
      end
    end
  end
  availableFiles.sort_by! {|h| h[:name].downcase}

  filesToTransfer = Blitzcrank.transfer_menu(availableFiles)

  Blitzcrank.transfer_files(filesToTransfer)
end

.remote_video_file_listObject

get a listing of all remote files that would be considered “videos”



43
44
45
# File 'lib/blitzcrank.rb', line 43

def self.remote_video_file_list
  %x[ssh -q #{Blitzcrank.config.remote_user}@#{Blitzcrank.config.remote_host} "cd #{Blitzcrank.config.remote_base_dir} && find . -type f \\( -iname \'*.avi\' -or -iname \'*.mkv\' -or -iname \'*.mp4\' -or -iname \'*.m4v\' -or -iname \'*.divx\' \\)"]
end

.rsync_all_filesObject

copies all files if they find a matching local directory



77
78
79
80
81
82
83
84
85
# File 'lib/blitzcrank.rb', line 77

def self.rsync_all_files
  puts "Downloading all remote videos\n"
  fileArray = Blitzcrank.remote_video_file_list.gsub('./','').split("\n")
  availableFiles = Array.new()
  fileArray.each do |remoteFile|
    availableFiles.push({:path => remoteFile, :name => remoteFile.split('/').last()})
  end
  Blitzcrank.transfer_files(availableFiles)
end

.transfer_file(video) ⇒ Object



35
36
37
38
39
40
# File 'lib/blitzcrank.rb', line 35

def self.transfer_file(video)
  puts "Copying #{video.remote_path} to #{video.local_path}"
  if !Blitzcrank.config.dry_run
    Rsync.sync(video)
  end
end

.transfer_files(filesToTransfer) ⇒ Object

any files (hashes) passed into here will be checked against our local TV folders and IMDB to see if it’s a movie



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/blitzcrank.rb', line 88

def self.transfer_files(filesToTransfer)
  Dir.chdir(Blitzcrank.config.base_tv_dir)

  filesToTransfer.each do |dh|
    video = Video.with_path dh[:path]
    if video.is_tv_show?
      FileUtils.mkdir_p video.season_path
    end
    Blitzcrank.transfer_file(video)
  end
end

.transfer_menu(files) ⇒ Object

runs the interactive transfer menu, returns an array of files to download



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/blitzcrank.rb', line 101

def self.transfer_menu(files)
  filesToTransfer = Array.new
  begin
    unless filesToTransfer.empty?
      print "Files queued for transfer:\n"
      filesToTransfer.sort_by! {|h| h[:name]}
      filesToTransfer.each do |f|
        print "* #{f[:name]}\n"
      end
      print "------------------------------------------------------------------\n"
    end

    files.each_with_index do |fh, index|
      unless filesToTransfer.include?(fh)
        line = "#{index + 1}: #{fh[:name]}\n"
        print line.light_cyan if index % 2 == 1
        print line.green if index % 2 == 0
      end
    end
    print "Which file would you like to transfer? (enter '0' when finished)\n"
    response = STDIN.gets
    print "\n"
    transfer_index = response.to_i
    if (transfer_index <= files.length && transfer_index > 0)
      filesToTransfer.push(files[transfer_index - 1])
    end
  end while transfer_index > 0 && filesToTransfer.length < files.length
  filesToTransfer
end

.write_sample_config(yaml_path) ⇒ Object



31
32
33
# File 'lib/blitzcrank.rb', line 31

def self.write_sample_config(yaml_path)
  IO.write(yaml_path, Blitzcrank.config.to_yaml)
end