-
Notifications
You must be signed in to change notification settings - Fork 0
/
tvsortr.rb
executable file
·104 lines (84 loc) · 2.75 KB
/
tvsortr.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env ruby
# Requires #
require 'fileutils'
require 'rubygems'
require './lib/tvshow'
require './lib/simplelog'
# Constants #
TVSORTR_VERSION = "0.3"
# Globals #
log_file = File.open(File.expand_path('~/Library/Logs/tvsortr.log'), File::CREAT | File::WRONLY | File::APPEND)
$logger = SimpleLog.new(log_file,true)
# Functions #
def print_usage
puts "#{__FILE__} --downloads=<path> --destination=<path>"
exit(-1)
end
def move_show(tvshow,destination)
Dir.mkdir(destination) unless File.directory?(destination) unless $DRYRUN
$targetPath = "#{destination}/#{tvshow.fileName}".gsub("//","/")
$logger.info "\tMoving #{tvshow.fileName} -> #{$targetPath}"
FileUtils.mv "#{TV_DOWNLOADS_DIR}/#{tvshow.fileName}",$targetPath unless $DRYRUN
end
# Main #
# Manage Options
# Required:
# --downloads
# --destination
# Optional:
# --dryrun
# --move
print_usage unless ARGV.select {|a| a =~ /--downloads/ || a =~ /--destination/}.length == 2
ARGV.each do |a|
if a =~ /--downloads=(.+)/
TV_DOWNLOADS_DIR="#$1"
unless File.exists? TV_DOWNLOADS_DIR
$logger.warning "Download Directory \'#{TV_DOWNLOADS_DIR}\' not found"
exit -1
end
elsif a =~ /--destination=(.+)/
TV_ROOT_DIR="#$1"
unless File.exists? TV_ROOT_DIR
$logger.error "TV Directory \'#{TV_ROOT_DIR}\' not found"
exit -1
end
elsif a =~ /--rename/
$RENAME = true
elsif a =~ /--dryrun/
$DRYRUN = true
elsif print_usage
end
end
$logger.info "-----------------------------------------------------------------------"
$logger.info "Starting #{__FILE__}"
$logger.info "#{__FILE__} version #{TVSORTR_VERSION}"
$logger.info "The script running is: #{__FILE__} #{ARGV.join(" ")}"
$logger.info "logger.info File is located: #{log_file.to_s}"
$logger.info "-----------------------------------------------------------------------"
$logger.info "Scanning TV Downloads..."
$movecount = 0
Dir.open(TV_DOWNLOADS_DIR).each { |tvshow|
$logger.info "Checking '#{tvshow}'"
if File.directory?(tvshow)
$logger.info "Skipping directory '#{tvshow}'"
next
elsif tvshow == "."
$logger.info "Skipping '#{tvshow}'"
next
elsif tvshow == ".."
$logger.info "Skipping '#{tvshow}'"
next
end
new_show = TVShow.new(tvshow)
$logger.info "Detected: '#{new_show.to_s}'"
if new_show.type != "Unknown"
$movecount++
move_show(new_show,"#{TV_ROOT_DIR}/#{new_show.name}")
else
$logger.info "Not moving Unknown show: #{new_show.to_s}"
end
}
$logger.info "Summary: Moved #{$movecount} shows"
$logger.info "Exiting..."
$logger.info "-----------------------------------------------------------------------"
exit(0)