Skip to content

Instantly share code, notes, and snippets.

@spangenberg
Created March 14, 2012 12:53
Show Gist options
  • Save spangenberg/2036261 to your computer and use it in GitHub Desktop.
Save spangenberg/2036261 to your computer and use it in GitHub Desktop.
Convert file names of movies to the right form for Boxee with informations from IMDb
# encoding: UTF-8
require 'net/http'
require 'json'
module Boxee
class Rename
class File
attr_reader :path, :name, :extension, :part, :canonical
def initialize(path)
@path = path
@name = @path.split('/').last
name_parts = @name.split('.')
@extension = name_parts.slice!(-1)
if name_parts.last.match(/[cd|part](\d+)/)
name_parts.slice!(-1)
@part = $1
end
if name_parts.last.match(/\((\d{4})\)/)
name_parts.slice!(-1)
@year = $1
end
@canonical = name_parts.join(' ')
imdb
end
def imdb
return @imdb if @imdb
params = {:t => @canonical}
params.merge({:y => @year}) if @year
uri = URI('http://www.imdbapi.com')
uri.query = URI.encode_www_form(params)
res = Net::HTTP.get_response(uri)
if res.is_a?(Net::HTTPSuccess)
begin
@imdb = JSON.parse(res.body)
rescue
end
end
end
def boxee_name
return false unless imdb['Title']
name = [imdb['Title'].gsub(/:/, '').split.join('.')]
name << "(#{imdb['Year']})" if imdb['Year']
name << "part#{@part}" if @part
name << @extension
name.join('.')
end
end
def initialize(path)
@files = {}
files(path).each do |file_name|
file = File.new("#{path}/#{file_name}")
begin
if file.boxee_name
print "\t\t" if file.name == file.boxee_name
puts "Convert #{file.name} to #{file.boxee_name} with IMDb ID: #{file.imdb['ID']}"
else
puts "Nothing found for #{file.name}"
end
rescue Encoding::CompatibilityError
puts "Encoding Compatibility Error with #{file.name}"
end
end
end
def files(path)
file_entries = Dir.entries(path)
file_entries.slice!(0, 2)
file_entries.sort
end
end
end
Boxee::Rename.new ARGV[0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment