Module: Blitzcrank

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

Constant Summary collapse

VERSION =
"0.1.0"

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



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
81
# File 'lib/blitzcrank.rb', line 54

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)


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

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



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

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(" ").gsub(/\b(us|uk|\d{4})$/i, '(\1)') # adding parens around US/UK marked shows, or shows with years
  else
    file_name
  end
end

.remote_video_file_listObject

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



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

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

copies all files if they find a matching local directory



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

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

pulls the season number froma file



116
117
118
119
# File 'lib/blitzcrank.rb', line 116

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=' + @config[:bwlimit] unless @config[:bwlimit].nil? } --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



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

def self.transfer_files(filesToTransfer)
  Dir.chdir(@config[:base_tv_dir])

  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

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



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
174
175
176
# File 'lib/blitzcrank.rb', line 148

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