Module: Blitzcrank

Defined in:
lib/blitzcrank.rb,
lib/blitzcrank/version.rb

Constant Summary collapse

VERSION =
"0.0.4"

Class Method Summary collapse

Class Method Details

.configObject



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

def self.config
  @config
end

.configure(opts = {}) ⇒ Object

Configure through hash



21
22
23
# File 'lib/blitzcrank.rb', line 21

def self.configure(opts = {})
  opts.each {|k,v| @config[k.to_sym] = v if @valid_config_keys.include? k.to_sym}
end

.configure_with(yaml_path) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/blitzcrank.rb', line 25

def self.configure_with(yaml_path)
  begin
    config = 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

  configure(config)
end

.file_menu(search_array = nil) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/blitzcrank.rb', line 53

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

.is_movie?(file_name) ⇒ Boolean

Returns:

  • (Boolean)


134
135
136
137
138
139
140
141
142
143
# File 'lib/blitzcrank.rb', line 134

def self.is_movie?(file_name)
  unless /^(.*).(\d{4}|dvdrip)/i.match(file_name).nil?
    movie_name = $1
    nice_movie_name = movie_name.gsub('.', ' ').downcase
    i = Imdb::Search.new(nice_movie_name)
    i.movies.size > 0
  else
    false
  end
end

.nice_tv_name(file_name) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/blitzcrank.rb', line 119

def self.nice_tv_name(file_name)
  unless /(.*).s?(\d{2})e?(\d{2})/i.match(file_name).nil?
    oldShowName = $1
    wordsInShowName = oldShowName.gsub('.', ' ').downcase.split(" ")
    wordsInShowName.each do |word|
      if wordsInShowName.index(word) == 0 || /^(in|a|the|and|on)$/i.match(word).nil?
        word.capitalize!
      end
    end
    wordsInShowName.join(" ")
  else
    file_name
  end
end

.remote_video_file_listObject



49
50
51
# File 'lib/blitzcrank.rb', line 49

def self.remote_video_file_list
    %x[ssh -q #{@config[:remote_user]}@#{@config[:remote_host]} "cd #{@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



82
83
84
85
86
87
88
89
90
# File 'lib/blitzcrank.rb', line 82

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

.season(file_name) ⇒ Object



114
115
116
117
# File 'lib/blitzcrank.rb', line 114

def self.season(file_name)
  /s?(\d{2})e?\d{2}/i.match(file_name)
  $1.gsub(/\A0+/, '')
end

.transfer_file(remote_path, local_dir) ⇒ Object



45
46
47
# File 'lib/blitzcrank.rb', line 45

def self.transfer_file(remote_path, local_dir)
  system("rsync -avz --bwlimit=2000 --progress --rsh='ssh' \"#{@config[:remote_user]}@#{@config[:remote_host]}:#{@config[:remote_base_dir]}#{remote_path.gsub(' ', '\\ ')}\" \"#{local_dir}\"")
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



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/blitzcrank.rb', line 93

def self.transfer_files(filesToTransfer)
  Dir.chdir(@config[:base_tv_dir])
  tv_directories = Dir.glob("*/")

  filesToTransfer.each do |dh|
    Dir.chdir(@config[:base_tv_dir])
    full_path = dh[:path]
    file_name = dh[:name]
    nice_name = Blitzcrank.nice_tv_name(file_name)
    directories = Dir.glob(nice_name, File::FNM_CASEFOLD)
    if directories.count > 0 # see if we already have a directory for this tv show
      nice_name = directories.first
      season_dir = "#{Dir.pwd}/#{nice_name}/#{@config[:season_identifier]}#{Blitzcrank.season(file_name)}"
      Dir.mkdir(season_dir) unless Dir.exists?(season_dir) # make the folder if it doesn't exist
      Blitzcrank.transfer_file(full_path, season_dir)
    elsif Blitzcrank.is_movie?(file_name)
      Blitzcrank.transfer_file(full_path, @config[:base_movie_dir])
    end
  end
end

.transfer_menu(files) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/blitzcrank.rb', line 145

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



41
42
43
# File 'lib/blitzcrank.rb', line 41

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