Skip to content

Instantly share code, notes, and snippets.

@andy318
Created October 17, 2013 21:28
Show Gist options
  • Save andy318/7032511 to your computer and use it in GitHub Desktop.
Save andy318/7032511 to your computer and use it in GitHub Desktop.
Rename TV Episode video files using data from the TVDB api and add the season and episode number to the file name. For this to work the name of the file should contain the episode name in it.
require 'xmlsimple'
# XML file was retrieved using TVDB api
doc = XmlSimple.xml_in('/Users/xyz/smurfs.xml')
doc['Episode'].each do |episode_hash|
# Find the next valid episode (with a season and episode number)
if episode_hash['SeasonNumber'][0].class == String && episode_hash['SeasonNumber'][0].to_i == 5
episode_season = episode_hash['SeasonNumber'][0].rjust(2, "0")
episode_num = episode_hash['EpisodeNumber'][0].rjust(2, "0")
episode_name = episode_hash['EpisodeName'][0]
episode_fullname = "The Smurfs S#{episode_season}E#{episode_num} - #{episode_name}"
# Find the file name which contains that episode name
f = Dir.glob("./*#{episode_name}*", File::FNM_CASEFOLD)[0]
# Rename the file to have the right season and episode number
if f
puts "Renaming #{f} to #{episode_fullname + File.extname(f)}"
File.rename(f, episode_fullname + File.extname(f))
else
puts "Could not find file corresponding to #{episode_fullname}"
end
else
# puts "Skipping #{episode_hash['EpisodeName'][0]}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment