Skip to content
Browse files

first commit

  • Loading branch information...
0 parents commit fdb6fd80e98dde77b4bc1656352de8d34d2bf207 Christopher Stott committed
BIN ._createFakeFiles.rb
Binary file not shown.
BIN ._renameAllFiles.rb
Binary file not shown.
BIN ._renameAllFilesTest.rb
Binary file not shown.
BIN ._renameEpisodes.rb
Binary file not shown.
BIN ._renameFile.rb
Binary file not shown.
BIN ._renamer.rb
Binary file not shown.
0 README
No changes.
71 createFakeFiles.rb
@@ -0,0 +1,71 @@
+#!/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'
+
+def CreateFile(filename)
+ File.open(filename, 'w') {|f| f.write('h') }
+end
+
+def CreateDummyFiles
+ dir =`pwd`
+ if dir.index("FakeDownloaded") == 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_s03'`
+ `mkdir 'Blackadder/Series 4'`
+ `mkdir 'Blackadder/Series4'`
+ `mkdir 'Blackadder Season 4'`
+ Dir::mkdir('Ocean\'s.11[2001]DvDrip[Eng]-aXXo')
+ `mkdir 'Point Break.1991-DvDRiP[x]'`
+ `mkdir 'Mr And Mrs Smith 2005 BRRip [A Release Lounge H264]'`
+ `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_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 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'`
+ CreateFile('Ocean\'s.11[2001]DvDrip[Eng]-aXXo/Ocean\'s.11[2001]DvDrip[Eng]-aXXo.avi')
+ CreateFile('Ocean\'s.11[2001]DvDrip[Eng]-aXXo/ocean\'s.11-aXXo.nfo')
+ CreateFile('Ocean\'s.11[2001]DvDrip[Eng]-aXXo/IMPORTANT.Read carefully before enjoy this movie.txt')
+ `echo h > 'Point Break.1991-DvDRiP[x]/Point Break.1991-DvDRiP[x].avi'`
+ `echo h > 'Point Break.1991-DvDRiP[x]/xRiPp3d.txt'`
+ `echo h > 'Mr And Mrs Smith 2005 BRRip [A Release Lounge H264]/Mr And Mrs Smith 2005 BRRip [A Release Lounge H264].mp4'`
+end
+
+CreateDummyFiles()
17 renameAllFiles.rb
@@ -0,0 +1,17 @@
+#!/usr/bin/env ruby
+$: << File.dirname( __FILE__)
+require 'renamer'
+
+####################################################################
+#
+
+if ARGV.count != 1
+ puts "Must enter a directory to process"
+ exit
+end
+
+directory = ARGV[0]
+
+Dir[directory+'/**/*.*'].each do |f|
+ RenameAndMove(f)
+end
17 renameAllFilesTest.rb
@@ -0,0 +1,17 @@
+#!/usr/bin/env ruby
+$: << File.dirname( __FILE__)
+require 'renamer'
+
+####################################################################
+#
+
+if ARGV.count != 1
+ puts "Must enter a directory to process"
+ exit
+end
+
+directory = ARGV[0]
+
+Dir[directory+'/**/*.*'].each do |f|
+ Rename(f)
+end
345 renameEpisodes.rb
@@ -0,0 +1,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
14 renameFile.rb
@@ -0,0 +1,14 @@
+#!/usr/bin/env ruby
+$: << File.dirname( __FILE__)
+require 'renamer'
+
+####################################################################
+#
+
+if ARGV.count != 1
+ puts "Must enter a file to process"
+ exit
+end
+
+filename = ARGV[0]
+RenameAndMove(filename)
677 renamer.rb
@@ -0,0 +1,677 @@
+# Need to give mention to thetvdb.com
+
+# Takes 1 input file & renames it & moves it into an organised directory tree
+
+# Hierarchy looks like
+# - Films
+# - Film Name (Year).extension
+# - TV Episodes
+# - Series Name (Year)
+# - Season 0x
+# - Series Name (Year) - S0xE0y - Episode Name.extension
+# - Uncertain TV
+# - Uncertain Film
+# - Quarantine
+
+
+require 'net/http'
+require 'rubygems'
+require 'xmlsimple'
+require 'cgi'
+
+$thetvdb_api_key = "89909344A1B56E06"
+$themoviedb_api_key = "e89374c1f4a9921af76dd3fc28f1a247"
+$destination_root = '/SortedFiles'
+
+$movies_root = "/Volumes/MEDIA2/Movies/"
+$tv_root = "/Volumes/MEDIA2/TV Episodes/"
+
+####################################################################
+#
+def ValidateFile(filename)
+ if not File.exists? filename
+ return false
+ end
+
+ if File.directory? filename
+ return false
+ end
+
+ return true
+end
+
+####################################################################
+#
+def ShouldDelete?(filename)
+ extension = filename[-4..-1]
+ if [".txt",".nfo",".srt",".jpg",".png"].include?(extension)
+ return true
+ else
+ if filename.downcase.include?('sample')
+ return true
+ else
+ return false
+ end
+ end
+end
+
+####################################################################
+#
+def IsVideoFile?(filename)
+ extension = filename[-4..-1]
+ return [".avi",".mp4",".mkv",".mov"].include?(extension)
+end
+
+####################################################################
+#
+def StripConfusingThings(filename)
+ filename = filename.gsub(/\[.*?\]/,'')
+ filename = filename.gsub(/\{.*?\}/,'')
+ filename = filename.gsub(/AC3/i,'')
+ filename = filename.gsub(/H264/i,'')
+ filename = filename.gsub(/20\d\d\D/,'')
+ filename = filename.gsub(/19\d\d\D/,'')
+ filename = filename.gsub(/dvdrip/i,'')
+ filename = filename.gsub(/XviD/i,'')
+ filename = filename.gsub('-aXXo','')
+ filename = filename.gsub('1337x','')
+ filename = filename.gsub(/720\S*/,'')
+ filename = filename.gsub('KLAXXON','')
+ filename = filename.gsub("Collector's Edition",'')
+end
+
+####################################################################
+#
+def IsMovie?(filename,base_directory)
+
+
+ #$message = isMovieSized ? "Yes" : "No"
+
+
+ short_filename = filename.sub(base_directory+'/','')
+ modded_filename = StripConfusingThings(short_filename[0..-5])
+ #filename_pieces = modded_filename.split('/')
+
+ hasSeasonNumber = FindCertainSeasonNumber(modded_filename)!=0
+
+ if (hasSeasonNumber)
+ return false
+ else
+ isMovieSized = File.size(filename) > 600*1024*1024
+
+ if (isMovieSized)
+ return true
+ else
+ return false
+ end
+ end
+
+
+
+
+ #if (filename_pieces.length>1)
+ # #
+
+ #$message = filename_pieces.to_s
+ #p "#{filename_pieces} : #{GuessSeasonNumber(modded_filename)}"
+ return
+end
+
+def TVDBQuery(url)
+ if $cached_queries == nil
+ $cached_queries = {}
+ end
+
+ if !$cached_queries.has_key?(url)
+ $cached_queries[url] = Net::HTTP.get URI.parse(url)
+ end
+
+ return $cached_queries[url]
+end
+
+def HasResults(movie)
+ r = !(movie["totalResults"][0] == "0" || movie[0]=="Nothing found.")
+ #puts "#{movie["totalResults"][0] == "0"} || #{movie[0]=="Nothing found."}"
+ return r
+end
+
+def LookupMovie(name,alternative,year)
+ #puts "LookupMovie #{name},#{alternative},#{year}"
+ movie = {}
+ if name != ''
+ encname = CGI::escape(name)
+ #puts "http://api.themoviedb.org/2.1/Movie.search/en/xml/#{$themoviedb_api_key}/#{encname}"
+ if (encname != '')
+ movie_xml = Net::HTTP.get URI.parse("http://api.themoviedb.org/2.1/Movie.search/en/xml/#{$themoviedb_api_key}/#{encname}")
+ movie = XmlSimple.xml_in(movie_xml)
+ end
+ end
+ if (!HasResults(movie))
+ encalternative = CGI::escape(alternative)
+ #puts "http://api.themoviedb.org/2.1/Movie.search/en/xml/#{$themoviedb_api_key}/#{encalternative}"
+ if (encalternative != '')
+ movie_xml = Net::HTTP.get URI.parse("http://api.themoviedb.org/2.1/Movie.search/en/xml/#{$themoviedb_api_key}/#{encalternative}")
+ movie = XmlSimple.xml_in(movie_xml)
+ end
+ end
+ if (!HasResults(movie))
+ encalternative = CGI::escape(name.downcase.gsub('and','&'))
+ #puts "http://api.themoviedb.org/2.1/Movie.search/en/xml/#{$themoviedb_api_key}/#{encalternative}"
+ if (encalternative != '')
+ movie_xml = Net::HTTP.get URI.parse("http://api.themoviedb.org/2.1/Movie.search/en/xml/#{$themoviedb_api_key}/#{encalternative}")
+ movie = XmlSimple.xml_in(movie_xml)
+ end
+ end
+ if (!HasResults(movie))
+ encalternative = CGI::escape(alternative.downcase.gsub('and','&'))
+ #puts "http://api.themoviedb.org/2.1/Movie.search/en/xml/#{$themoviedb_api_key}/#{encalternative}"
+ if (encalternative != '')
+ movie_xml = Net::HTTP.get URI.parse("http://api.themoviedb.org/2.1/Movie.search/en/xml/#{$themoviedb_api_key}/#{encalternative}")
+ movie = XmlSimple.xml_in(movie_xml)
+ end
+ end
+
+ if (!HasResults(movie))
+ return nil
+ end
+
+ movies = movie["movies"]
+
+ #p movies[0]["movie"]
+ #p movies[0]["movie"].length
+
+ found_name = name
+ found_released = year
+
+ if (movies[0]["movie"].length==1)
+ #puts "Only one movie"
+ found_name = movies[0]["movie"][0]["name"]
+ found_released = movies[0]["movie"][0]["released"][0]
+ else
+ if (year != '')
+ movies[0]["movie"].each do |m|
+ if (m["released"][0].include?(year))
+ #puts "Grabbed by year #{year}"
+ found_name = m["name"][0]
+ found_released = m["released"][0]
+ break
+ end
+ end
+ else
+ #puts "Grabbed the first"
+ found_name = movies[0]["movie"][0]["name"][0]
+ found_released = movies[0]["movie"][0]["released"][0]
+ end
+ end
+
+ info = {}
+ info[:name] = found_name
+ info[:year] = found_released.sub(/-.*/,'')
+ return info
+end
+
+def ApplyCommonTVSeriesSubstitutions(name)
+ name = name.gsub('HIGNFY','Have I Got News For You')
+ return name
+end
+
+def LookupTVSeries(foldername,season,episode,filename)
+ foldername.strip!
+ foldername = foldername.sub('-',' ')
+ foldername = ApplyCommonTVSeriesSubstitutions(foldername)
+ puts "foldername = #{foldername}"
+ # Get Series ID, Name, Year
+ esc_seriesname = CGI::escape(foldername)
+ series_xml = TVDBQuery("http://www.thetvdb.com/api/GetSeries.php?seriesname=#{esc_seriesname}")
+ series = XmlSimple.xml_in(series_xml)
+
+ if series == {} || series == 'seriesname is required'
+
+ foldername = foldername.sub(/\S+?$/,'')
+ if (foldername=='')
+ return nil
+ end
+ esc_seriesname = CGI::escape(foldername)
+ series_xml = TVDBQuery("http://www.thetvdb.com/api/GetSeries.php?seriesname=#{esc_seriesname}")
+ series = XmlSimple.xml_in(series_xml)
+
+ if series == {} || series == 'seriesname is required'
+
+
+ foldername = filename.split('/')[-2]
+ puts "trying yet again #{foldername}"
+ if (foldername=='')
+ return nil
+ end
+ esc_seriesname = CGI::escape(foldername)
+ series_xml = TVDBQuery("http://www.thetvdb.com/api/GetSeries.php?seriesname=#{esc_seriesname}")
+ series = XmlSimple.xml_in(series_xml)
+ end
+ end
+
+ #p series["Series"][0]
+ if series != {} and series != "seriesname is required"
+
+ if series["Series"][0].has_key?("FirstAired")
+ year = series["Series"][0]["FirstAired"][0]
+ year = year.split('-')[0]
+ else
+ if filename =~ /((19|20)\d\d)/
+ year = $1
+ else
+ year = ''
+ end
+ end
+ name = series["Series"][0]["SeriesName"][0]
+ seriesid = series["Series"][0]["seriesid"][0]
+ info = {}
+ info[:year] = year
+ info[:name] = name
+ info[:episodeName] = ""
+
+ episodes_xml = TVDBQuery("http://www.thetvdb.com/api/#{$thetvdb_api_key}/series/#{seriesid}/all/en.xml")
+ episodes = XmlSimple.xml_in(episodes_xml)
+
+ for e in episodes["Episode"] do
+ if (e["SeasonNumber"][0]==season.to_s)
+ if (e["EpisodeNumber"][0]==episode.to_s)
+ info[:episodeName] = e["EpisodeName"][0]
+ end
+ end
+ end
+ end
+ return info
+end
+
+####################################################################
+#
+def FindCertainSeasonNumber(filename)
+ # 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 =~ /(\s\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{2}\D/
+ # return $1.to_i
+ #end
+
+ # like 1204
+ #if filename =~ /(\d\d)\d\d/
+ # return $1.to_i
+ #end
+
+ return 0
+end
+
+####################################################################
+#
+def StripTVInfo(filename)
+ filename = filename.sub(/S\d.*?$/,'')
+ filename = filename.sub(/\s\d.*?$/,'')
+ filename = filename.gsub('.',' ')
+
+ parts = filename.split('/')
+ if (parts.length > 1)
+ return parts[-2]
+ else
+ return filename
+ end
+end
+
+####################################################################
+#
+def GuessSeasonNumber(filename)
+ # 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{2}\D/
+ 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
+
+ if filename =~ /Ep(\d{1,2})/i
+ return $1.to_i
+ end
+
+ if filename =~ /(\d{1,2})of/i
+ return $1.to_i
+ end
+
+
+ # like 1x1 12x03 03x04
+ if filename =~ /x(\d{1,2})/i
+ return $1.to_i
+ end
+
+ # like Pt8
+ if filename =~ /Pt(\d{1,2})/i
+ return $1.to_i
+ end
+
+ # like 102
+ if filename =~ /\d(\d\d)/
+ 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\d)$/
+ return $1.to_i
+ end
+
+ if pos=filename =~ /(\d)$/
+ return $1.to_i
+ end
+ return 0
+end
+
+####################################################################
+#
+def GetMirror
+ 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]
+end
+
+
+####################################################################
+#
+def FormTVFilename(filename,base_directory)
+ short_filename = filename.sub(base_directory+'/','')
+ extension = filename[-4..-1]
+ season = GuessSeasonNumber(filename)
+ episode = GuessEpisodeNumber(filename)
+ puts "#{filename} #{season} #{episode}"
+ info = LookupTVSeries(StripTVInfo(short_filename),season.to_i,episode.to_i,filename)
+ if (info == nil)
+ return
+ end
+ programfolder = "#{info[:name]} (#{info[:year]})"
+ seasonfolder = "Season %02d" % season.to_i
+ newFilename = "#{info[:name]} (#{info[:year]}) - #{ConstructSxxExxDescriptor(season,episode)}"
+
+ if info[:episodeName].to_s != ''
+ newFilename = newFilename + " - " + info[:episodeName].to_s
+ end
+
+
+ return "#{programfolder}/#{seasonfolder}/#{newFilename}#{extension}"
+end
+
+####################################################################
+#
+def GutMovieName(file)
+ file = file.sub(/\[.*?$/,'')
+ file = file.sub(/\(.*?$/,'')
+ file = file.sub(/\{.*?$/,'')
+ file = file.sub(/19\d\d.*?$/,'')
+ file = file.sub(/20\d\d.*?$/,'')
+ file = file.gsub('.',' ')
+ file = file.sub(/WS.*?$/,'')
+ file = StripConfusingThings(file)
+ return file
+end
+
+####################################################################
+#
+def FormMovieFilename(filename,base_directory)
+ extension = filename[-4..-1]
+ file = filename.split('/')[-1][0..-5]
+ file = GutMovieName(file)
+
+ alt = filename.split('/')[-2][0..-5]
+ alt = GutMovieName(alt)
+
+ if filename =~ /((19|20)\d\d)/
+ year = $1
+ else
+ year = ''
+ end
+
+ info = LookupMovie(file,alt,year)
+ if info != nil
+ if info[:year]!=''
+ filename = "#{info[:name]} (#{info[:year]})#{extension}"
+ else
+ filename = "#{info[:name]}#{extension}"
+ end
+ else
+ # Unable to name file
+ filename = ''
+ end
+ return filename
+end
+
+####################################################################
+#
+def ConstructSxxExxDescriptor(season,episode)
+ season = season.to_i
+ episode = episode.to_i
+ return "S"+"%02d"%season+"E"+"%02d"%episode
+end
+
+####################################################################
+#
+def PrintOutcome(outcome,oldfilename,newfilename)
+ padding = " "*(16-outcome.length)
+ puts "[#{outcome}]#{padding}#{oldfilename} -> #{newfilename}"
+end
+
+####################################################################
+#
+def ConfusedBy?(filename)
+ if filename.downcase.include? 'cd1'
+ return true
+ elsif filename.downcase.include? 'cd2'
+ return true
+ elsif filename.downcase.include? 'daily.show'
+ return true
+ end
+ return false
+end
+
+####################################################################
+#
+def Relocate(old_filename, new_filename)
+ if not File.exists? new_filename
+ directory = new_filename.split('/').slice(0..-2).join('/')
+ `mkdir -p "#{directory}"`
+ `mv -n "#{old_filename}" "#{new_filename}"`
+ end
+end
+
+####################################################################
+#
+def Rename(filename)
+ new_filename = ''
+
+ base_directory = filename.split('/').slice(0..-2).join('/')
+
+ short_filename = filename.sub(base_directory+'/','')
+
+ ######################################
+ if (!ValidateFile(filename))
+ PrintOutcome("leave",short_filename,'')
+ return false
+ end
+
+ ######################################
+ if ShouldDelete?(filename)
+ PrintOutcome("deleting",short_filename,'')
+ `rm "#{filename}"`
+ return false
+ end
+
+ ######################################
+ if (!IsVideoFile?(filename))
+ PrintOutcome("leave",short_filename,'')
+ return false
+ end
+
+ ######################################
+ if ConfusedBy?(filename)
+ PrintOutcome("confused",short_filename,'')
+ return false
+ end
+
+ ######################################
+ if IsMovie?(filename,base_directory)
+ new_filename = FormMovieFilename(filename,base_directory)
+ if (new_filename != '')
+ new_filename = $movies_root + new_filename
+ end
+ PrintOutcome("Movie",short_filename,new_filename)
+ else
+ new_filename = FormTVFilename(filename,base_directory)
+ if (new_filename != nil && new_filename != '')
+ new_filename = $tv_root + new_filename
+ end
+ PrintOutcome("TV Episode",short_filename,new_filename)
+ end
+end
+
+####################################################################
+#
+def RenameAndMove(filename)
+ new_filename = ''
+
+ base_directory = filename.split('/').slice(0..-2).join('/')
+
+ short_filename = filename.sub(base_directory+'/','')
+
+ ######################################
+ if (!ValidateFile(filename))
+ PrintOutcome("leave",short_filename,'')
+ return false
+ end
+
+ ######################################
+ if ShouldDelete?(filename)
+ PrintOutcome("deleting",short_filename,'')
+ `rm "#{filename}"`
+ return false
+ end
+
+ ######################################
+ if (!IsVideoFile?(filename))
+ PrintOutcome("leave",short_filename,'')
+ return false
+ end
+
+ ######################################
+ if ConfusedBy?(filename)
+ PrintOutcome("confused",short_filename,'')
+ return false
+ end
+
+ ######################################
+ if IsMovie?(filename,base_directory)
+ new_filename = FormMovieFilename(filename,base_directory)
+ if (new_filename != '')
+ new_filename = $movies_root + new_filename
+ end
+ PrintOutcome("Movie",short_filename,new_filename)
+ else
+ new_filename = FormTVFilename(filename,base_directory)
+ if (new_filename != nil && new_filename != '')
+ new_filename = $tv_root + new_filename
+ end
+ PrintOutcome("TV Episode",short_filename,new_filename)
+ end
+
+ ######################################
+ if (new_filename != nil && new_filename != '')
+ Relocate(filename,new_filename)
+ end
+
+end
+

0 comments on commit fdb6fd8

Please sign in to comment.
Something went wrong with that request. Please try again.