Created
April 26, 2011 17:22
-
-
Save catesandrew/942693 to your computer and use it in GitHub Desktop.
add imdb ratings to your itunes movie collection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env /usr/local/bin/ruby | |
require 'rubygems' | |
require 'appscript' | |
require 'imdb_party' | |
include Appscript | |
class LookupAPI | |
def initialize | |
@imdb = ImdbParty::Imdb.new | |
end | |
def getDataFromImdb(movieName, movieYear) | |
puts "!!Looking up data for: #{movieName} - #{movieYear}" | |
movieResults = @imdb.find_by_title(movieName) | |
movies = [] | |
puts "!!Search returned #{movieResults.length} hits" | |
for movieResult in movieResults do | |
# check that the year tag in the file name matches with the release date, | |
# otherwise not the movie we are looking for | |
#puts "!!Potential hit: #{movieResult[:title]}" | |
if movieResult[:year] != nil then | |
if movieResult[:kind] != nil && (movieResult[:kind] == "feature" || movieResult[:kind] =="documentary") then | |
if movieResult[:year].start_with?(movieYear.to_s) || movieResult[:year].start_with?((movieYear.to_i + 1).to_s) || movieResult[:year].start_with?((movieYear.to_i - 1).to_s) then | |
movie = @imdb.find_movie_by_id(movieResult[:imdb_id]) | |
movies.push(movie) | |
end | |
end | |
end | |
end | |
return movies | |
end | |
end # end class | |
itunes = app('iTunes') | |
lookupAPI = LookupAPI.new | |
tracks = itunes.selection.get | |
tracks.each do |track| | |
#track.rating.set(80) | |
movieName = track.name.get | |
movieYear = track.year.get | |
puts " Retrieving data from IMDb" | |
movies = lookupAPI.getDataFromImdb(movieName, movieYear) | |
# for movie in movies do | |
# puts "!!hit: title #{movie.title}, id #{movie.imdb_id}" | |
# end | |
if movies.length == 0 | |
puts " No matches found for \"#{movieName}\" made in #{movieYear}" | |
next | |
end | |
movieChoice = 0 | |
if movies.length > 1 then | |
puts " Potential Title Matches" | |
movieCounter = 0 | |
for movie in movies do | |
puts " #{movieCounter}. #{movie.title} (ID: #{movie.imdb_id}, YR: #{movie.release_date})" | |
movieCounter = movieCounter + 1 | |
end | |
#ask user what movie he wants to use | |
puts " Select correct title: " | |
movieChoice = gets.chomp | |
if movieChoice == nil then | |
movieChoice = 0 | |
end | |
movieChoice = movieChoice.to_i | |
end | |
movie = movies[movieChoice] | |
if movies.length == 1 then | |
puts "Selecting Default. #{movie.title} (ID: #{movie.imdb_id}, YR: #{movie.release_date})" | |
end | |
if movie.rating != nil then | |
rating = movie.rating | |
#track.rating.set((rating * 10).floor) | |
track.rating.set(:to => ((rating * 10).floor) ) | |
else | |
puts " nil rating #{movie.rating}" | |
end | |
#track.comment.set("{imdb_id=\"#{movie.imdb_id}\", genres=#{movie.genres}}") | |
end | |
# selected = itunes.selection.get | |
# | |
# updated = 0 | |
# not_found = 0 | |
# already_there = 0 | |
# | |
# for a in selected | |
# if (a.lyrics.get.eql?("") and !a.artist.get.eql?("") and !a.name.get.eql?("")) | |
# | |
# theartist = a.artist.get | |
# thesong = a.name.get | |
# | |
# url = "http://lyricwiki.org/" | |
# song = theartist.downcase | |
# song += ":" | |
# song += thesong.downcase | |
# #song.gsub!(/^[a-z]|\s+[a-z']/) { |letter| letter.upcase } | |
# song.gsub!(/\'\s/,' ') | |
# song.gsub!(/\s+/,'_') | |
# song.gsub!(/^[a-z]|_+[a-z]|[:\(\)\[\]][a-z]|/) { |letter| letter.upcase } | |
# song.gsub!(/&/, 'And' ) | |
# | |
# puts song | |
# | |
# url += song | |
# | |
# c = Curl::Easy.perform(url) | |
# pagecontent = c.body_str.gsub!(/&/, 'And' ) | |
# doc = Document.new c.body_str | |
# | |
# ln = doc.root_node.find_first_recursive {|node| node.name == "div" and node.attributes["class"] == "lyricbox"} | |
# | |
# if (ln == nil) | |
# puts "CANNOT FIND any lyrics for " + theartist + " - " + thesong | |
# not_found = not_found + 1 | |
# else | |
# lyr = ln.to_s | |
# | |
# lyr.gsub!(/<\/*\s*br\s*\/*>/, "\n") #STRIP brs | |
# lyr.gsub!(/<\/*\s*p\s*\/*>/, "\n") #STRIP p | |
# lyr.gsub!(/\<\s*(.*?)(\s*\>)/m, "") #STRIP any tag | |
# lyr.gsub!(/\n+\Z/, "\n") #STRIP final returns | |
# | |
# a.lyrics.set(lyr) | |
# puts "UPDATED lyrics for " + theartist + " - " + thesong | |
# updated += 1 | |
# end | |
# else | |
# already_there += 1 | |
# end | |
# end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hi there, I've never used scripts like this before, can you please tell me how to run this on a mac?
Thanks in advance.