-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseries-rake.rb
61 lines (53 loc) · 1.59 KB
/
series-rake.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
# using:
# verifying modifications: rake g:series:nice-names:see[PREFIX]
# applying modifications : rake g:series:nice-names:apply[PREFIX]
#
# by [email protected]
ONDE_BUSCAR = File.join(Rake.original_dir,"*")
REGEXPS = [
/(\d+)(\d{2})/, # 515
/s(\d+)e(\d+)/, # s05e15
/(\d+)x(\d+)/ # 05x15
]
def red(text)
"\033[31m\033[2m#{text}\033[0m"
end
def green(text)
"\033[32m\033[4m#{text}\033[0m"
end
def nice_paths(backname)
series = Dir[ONDE_BUSCAR].collect do |path|
name = File.basename(path).downcase
matched = REGEXPS.find{|rgx| name.match(rgx)}
matched ? [path,$1.to_i,$2.to_i] : nil
end.compact
series.collect do |(path,season,episode)|
name = File.basename(path)
season_s = sprintf("%.2d",season)
episode_s = sprintf("%.2d",episode)
new_name = "#{backname}.#{season_s}x#{episode_s}#{File.extname(name)}"
new_path = File.join(File.dirname(path),new_name)
new_path == path ? nil : [path,new_path]
end.compact
end
namespace :series do
namespace :"nice-names" do
task :see , :backname do |t,args|
nice_paths(args[:backname]).each do |(path,new_path)|
name = File.basename(path)
new_name = File.basename(new_path)
puts "#{green(new_name.inspect)} #{red(name.inspect)}"
end
puts 'terminou'
end
task :apply, :backname do |t,args|
nice_paths(args[:backname]).each do |(path,new_path)|
name = File.basename(path)
new_name = File.basename(new_path)
puts "#{green(new_name.inspect)} #{red(name.inspect)}"
File.rename path, new_path
end
puts 'terminou'
end
end
end