-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenameEpisodes.rb
executable file
·345 lines (293 loc) · 9.18 KB
/
renameEpisodes.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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#!/usr/bin/ruby
# Need to give mention to thetvdb.com
# Required gems :
# gem install xml-simple
require 'net/http'
require 'rubygems'
require 'xmlsimple'
require 'cgi'
$thetvdb_api_key = "89909344A1B56E06"
def ValidateInput
puts "Validating Input"
$directory = ARGV[0]
if not File.exists? $directory
puts "Directory does not exist"
exit
end
if not File.directory? $directory
puts "Input is not a directory"
exit
end
end
def CreateDummyEpisodes
dir =`pwd`
if dir.index "Testing" == nil
puts "CREATING DUMMY EPISODES CAN ONLY BE RUN INSIDE A dir called Testing"
exit
end
puts "Creating Dummy Episodes"
`rm -r -f -d *`
`mkdir 'Blackadder'`
`mkdir 'Blackadder/1'`
`mkdir 'Blackadder/Season 1'`
`mkdir 'Blackadder/Season 2'`
`mkdir 'Blackadder/s3'`
`mkdir 'Blackadder/Season3'`
`mkdir 'Blackadder/blackadder_s03'`
`mkdir 'Blackadder/Series 4'`
`mkdir 'Blackadder/Series4'`
`mkdir 'Blackadder/Blackadder Season 4'`
`echo h > 'Blackadder/Blackadder 1x1.avi'`
`echo h > 'Blackadder/1/Blackadder S01E02.avi'`
`echo h > 'Blackadder/Season 1/Blackadder S01E03.avi'`
`echo h > 'Blackadder/Blackadder S01-E04.avi'`
`echo h > 'Blackadder/Blackadder 02x01.avi'`
`echo h > 'Blackadder/Blackadder S02-E02.avi'`
`echo h > 'Blackadder/Season 2/Blackadder S02E03.avi'`
`echo h > 'Blackadder/Season 2/Blackadder S02E04.avi'`
`echo h > 'Blackadder/Blackadder 205.avi'`
`echo h > 'Blackadder/Blackadder 3x01.avi'`
`echo h > 'Blackadder/s3/Blackadder S03E02.avi'`
`echo h > 'Blackadder/blackadder_s03/Blackadder S03E03.avi'`
`echo h > 'Blackadder/Blackadder S03E04.avi'`
`echo h > 'Blackadder/Season3/Blackadder S03E05.avi'`
`echo h > 'Blackadder/Series 4/Blackadder S04E01.avi'`
`echo h > 'Blackadder/Series 4/Blackadder 02.avi'`
`echo h > 'Blackadder/Series4/Blackadder 3.avi'`
`echo h > 'Blackadder/Blackadder Season 4 Episode 4.avi'`
`echo h > 'Blackadder/Blackadder Season 4/Blackadder Series 4 Ep 5.avi'`
`echo h > 'Blackadder/Blackadder S05E01.avi'`
`echo h > 'Blackadder/Blackadder S05E02.avi'`
`echo h > 'Blackadder/Blackadder S05E03.avi'`
`echo h > 'Blackadder/Blackadder S05E04.avi'`
`echo h > 'Blackadder/Blackadder S05E05.avi'`
`echo h > 'Blackadder/Blackadder S07E05.avi'`
end
def FindCurrentSeriesName
$array = ARGV[0].split('/')
return $array[$array.length-1]
end
# Startup
#CreateDummyEpisodes()
ValidateInput()
# Get Mirror
$mirrors_xml = Net::HTTP.get URI.parse("http://thetvdb.com/api/#{$thetvdb_api_key}/mirrors.xml")
$mirrors = XmlSimple.xml_in($mirrors_xml)
$mirror = $mirrors["Mirror"][0]["mirrorpath"][0]
# Find the series name
$foldername = FindCurrentSeriesName()
$oldseriesname= $foldername.split("(")[0]
# Get Series ID, Name, Year
$esc_seriesname = CGI::escape($foldername)
$series_xml = Net::HTTP.get URI.parse("http://www.thetvdb.com/api/GetSeries.php?seriesname=#{$esc_seriesname}")
$series = XmlSimple.xml_in($series_xml)
if $series=={}
$esc_seriesname = CGI::escape($oldseriesname)
$series_xml = Net::HTTP.get URI.parse("http://www.thetvdb.com/api/GetSeries.php?seriesname=#{$esc_seriesname}")
$series = XmlSimple.xml_in($series_xml)
end
p $series
$year = $series["Series"][0]["FirstAired"][0]
$year = $year.split('-')[0]
$name = $series["Series"][0]["SeriesName"][0]
$seriesid = $series["Series"][0]["seriesid"][0]
if $name.index('(') == nil
$seriesname = $name + " (" + $year + ")"
else
$seriesname = $name
end
puts "Series Name : " + $seriesname
puts "Folder Name : " + $foldername
if $seriesname != $foldername
`mv "#{$foldername}" "#{$seriesname}"`
else
puts "Folder already correctly named"
end
$seasons = []
$episodes_xml = Net::HTTP.get URI.parse("http://www.thetvdb.com/api/#{$thetvdb_api_key}/series/#{$seriesid}/all/en.xml")
$episodes = XmlSimple.xml_in($episodes_xml)
p $episodes
def ConstructSxxExxDescriptor(season,episode)
season = season.to_i
episode = episode.to_i
return "S"+"%02d"%season+"E"+"%02d"%episode
end
$episode_table = {}
for $episode in $episodes["Episode"] do
puts $episode["SeasonNumber"][0]+":"+$episode["EpisodeNumber"][0]+":"+$episode["EpisodeName"][0]
$episode_table[ConstructSxxExxDescriptor($episode["SeasonNumber"][0],$episode["EpisodeNumber"][0])] = $episode["EpisodeName"][0]
end
p $episode_table
# Load the files (take all files from the root and first child directories)
$files = []
rootfiles = `ls -p "#{$seriesname}"`
for file in rootfiles
# Directory?
file.strip!
if file[-1,1]=="/"
childfiles = `ls -p "#{$seriesname}/#{file}"`
for childfile in childfiles
# Regular File
childfile.strip!
if childfile[-1,1]!="/"
$files<<"#{file}"+childfile.strip
end
end
else
$files << file.strip
end
end
def GuessSeasonNumber(filename)
puts "Guessing Season number"
# does it contain something like S01,S1,s01,s1?
if filename =~ /S(\d{1,2})/i
return $1.to_i
end
if filename =~ /\sS\s(\d{1,2})/i
return $1.to_i
end
# does it contain something like Season 01/season 1/etc
pos = filename =~ /Season\s*(\d{1,2})/i
if pos != nil
return $1.to_i
end
if filename =~ /Series\s*(\d{1,2})/i
return $1.to_i
end
# like 1x1 12x03 03x04
if filename =~ /(\d{1,2})x/i
return $1.to_i
end
# like folder 1/
if filename =~ /(\d{1,2})\//i
return $1.to_i
end
# like 102
if filename =~ /(\d)\d{1,2}/
return $1.to_i
end
# like 1204
if filename =~ /(\d\d)\d\d/
return $1.to_i
end
return 0
end
def GuessEpisodeNumber(filename)
filename = filename[0,filename.rindex('.')]
filename = filename[filename.index('/'),filename.length]
# does it contain something like E01,E1,e01,e1?
if filename =~ /E(\d{1,2})/i
return $1.to_i
end
if filename =~ /\sE\s(\d{1,2})/i
return $1.to_i
end
# does it contain something like Episode 01/episode 1/etc
if filename =~ /Episode\s*(\d{1,2})/i
return $1.to_i
end
# does it contain something like Episode 01/episode 1/etc
if filename =~ /Ep\s*(\d{1,2})/i
return $1.to_i
end
# like 1x1 12x03 03x04
if filename =~ /x(\d{1,2})/i
return $1.to_i
end
# like 102
if filename =~ /\d(\d{1,2})/
return $1.to_i
end
# like 1204
if filename =~ /\d\d(\d\d)/
return $1.to_i
end
# like 2 or 06 (at the end of the line)
if pos=filename =~ /(\d{1,2})$/
return $1.to_i
end
return 0
end
$episodes = []
$unsorted = []
$seasons = []
for file in $files
season = GuessSeasonNumber(file)
episode = GuessEpisodeNumber(file)
puts file+":"+episode.to_s
sxxexx = ConstructSxxExxDescriptor(season,episode)
puts sxxexx
title = $episode_table[sxxexx]
seasonfolder = "Season %02d" % season
extension = file.split(".")[-1]
puts title
if title
correctfilename = $seriesname + " - " + sxxexx + " - " + title + "." + extension
#correctfilename = correctfilename.delete ":"
$episodes << {"file"=>file,"SxxExx"=>sxxexx,"title"=>title,"correctfilename"=>correctfilename,"seasonfolder"=>seasonfolder}
$seasons << seasonfolder
else
$unsorted << file
end
end
$seasons.uniq!
if not File.exists? "Unknown/#{$seriesname}"
`mkdir -p "Unknown/#{$seriesname}"`
end
$seasons.each { |name| `mkdir -p '#{$seriesname}/#{name}'` }
$episodes.each { |hash| `mv "#{$seriesname}"/"#{hash['file']}" "#{$seriesname}"/"#{hash['seasonfolder']}/#{hash['correctfilename']}"` }
# move random left over ones
puts "Unsorted"
for unsorted in $unsorted
if unsorted.split('/').length==2
if unsorted.split('/')[0] =~ /^Season \d\d/
$regex = "^"+$seriesname.gsub("(","\\(").gsub(")","\\)")+" -"
if unsorted.split('/')[1] =~ /#{$regex}/
$seasons << unsorted.split('/')[0]
next
end
end
end
filename = "Unknown/#{$seriesname}/#{unsorted}"
folder = filename[0,filename.rindex("/")]
`mkdir -p '#{folder}'`
`mv "#{$seriesname}/#{unsorted}" "Unknown/#{$seriesname}/#{unsorted}"`
end
$seasons.uniq!
#delete the old ones
dirs = `ls '#{$seriesname}'`
dirs = dirs.split("\n")
$seasons.each { |name| dirs.delete name }
dirs.each { |name| `rmdir '#{$seriesname}/#{name}'` }
# Places the files in those directories
#for $file in $files do
# $file = $file.strip
# $pos = $file =~ /S(\d?\d?)-?E(\d?\d?)/i
#
# $series = $1
# $episode = $2
#
# $descriptor = ConstructSxxExxDescriptor($series,$episode)
# $series = "%02d" % $series.to_i
#
# if $episode_table.has_key? $descriptor
# $extension = $file.split('.')[-1]
# $newfilename = $seriesname + " - " + $descriptor + " - " + $episode_table[$descriptor] + "." + $extension
#
#
# if not File.exists? "#{$seriesname}/Season #{$series}"
# puts "Creating Directory : #{$seriesname}/Season #{$series}"
# `mkdir '#{$seriesname}/Season #{$series}'`
# end
#
# puts "Creating : " + "#{$seriesname}/Season #{$series}/#{$newfilename}"
#
# `mv "#{$seriesname}/#{$file}" "#{$seriesname}/Season #{$series}/#{$newfilename}"`
# else
# puts "Can't find description for : #{$file} (moving to unknown)"
# if not File.exists? "Unknown/#{$seriesname}"
# `mkdir -p "Unsorted/#{$seriesname}"`
# end
# `mv "#{$seriesname}/#{$file}" "Unknown/#{$seriesname}/#{$file}"`
# end
#end