Help with iTunes related automation

Joined
Aug 13, 2011
Messages
19
Reaction score
0
Ok, I will try to be specific and explain as best I can what I am trying to do, but the bottom line is I am looking for help in doing it.

Basically, I want to be able to organize my movies in iTunes in a specific way that uses IMDb data.

Every movie in my iTunes has the last part of the corresponding IMDb URL in the comments section (ie. tt0154506). I need automator to find that information for each specific title and retrieve data from the /ratings page of each URL to be used in the following equation.
The data required is the Total Votes, the Overall Rating and My Rating.

(Total Votes/(Total Votes+3000)*(((Overall Rating*2.46)+My Rating)/3.46)+(3000/(Total Votes+3000))*6.9)*1000000 = X

I then want the the 'Sort Name' field of each title in iTunes to be replaced with 'X'

If this is even possible, and I have a feeling it probably isn't, I would love for someone to help in making a workflow.

Thanks
 
Joined
Nov 26, 2010
Messages
3,558
Reaction score
52
This could be done with AppleScript, but its not trivial.

If you want mentoring you could PM me for more information.
 
Joined
Nov 26, 2010
Messages
3,558
Reaction score
52
Ok, a news update.
Endlin3 and I have talked about this in PM and we are going to do this in steps here for everyone to follow along and ask questions. As a professional programmer I do this for money:) so if you get something worthwhile from this thread I encourage you to donate both to this forum and myself.

When this thread error 500s we'll move to the AppleScript section.

Note: The scripts I post here will be focused on core functionality, if I were producing commercial code there would be all manner of error checking and handling. Not here, as that code would swamp the lessons. So all these scripts are supplied AS IS, with no warranty or guaranty, you use these AT YOUR OWN RISK!
 
Joined
Nov 26, 2010
Messages
3,558
Reaction score
52
Lets start by checking those IMDb tags we've been putting in our comments.

Code:
tell application "iTunes"
	-- set holdURL to make new document with properties {URL:"http://www.imdb.com/find?s=all&q=sucker+punch"}
	set holdSelectedMovies to selection
	if holdSelectedMovies is not {} then
		set holdFrontOfSearch to "http://www.imdb.com/find?s=all&q="
		repeat with holdTrack in holdSelectedMovies
			set holdEndSearch to comment of holdTrack
			if holdEndSearch is not "" then
				set holdAllSearch to holdFrontOfSearch & holdEndSearch
				tell application "Safari"
					set holdURL to make new document with properties {URL:holdAllSearch}
				end tell
			end if
		end repeat
	end if
end tell

This gets the tag from the selected iTunes make a URL and open it in Safari.

Do we need instructions on how to make a Scripts folder in ~/Library/iTunes. Copy this into AppleScript Editor and save it into the Scripts folder?
 
Joined
Aug 13, 2011
Messages
19
Reaction score
0
Ok, so I made a folder in the /Library/iTunes folder and saved the script above to there, is it supposed to do anything at the moment? or is this just the beginning?
 
Joined
Nov 26, 2010
Messages
3,558
Reaction score
52
If you have entered the IMDb number in the comments field, select the movie in iTunes, running this script will open the page in Safari.

Lets have a closer look:
1: Tell iTunes we want it to do things
2: put the current selection into the List holdSelectedMovies
3: if there are some selections proceed, check its not an empty list
4: Load the Front part the of the IMDb search URL
5: Repeat for each movie in the selection
6: Set the EndSearch to the comment field from the Movie in holdTrack
7: Check that there is a comment to proceed with
8: Join the Front part of the URL to the End bit from the comment field
9: Talk to Safari
10: Make a new Safari document with the URL, this will open a window and display it.
11 - End: End all the Ifs, Repeats, Tells
 
Joined
Nov 26, 2010
Messages
3,558
Reaction score
52
This is just a first step. Its a test script. It's always good to know the data we're working with in the comments field is the correct IMDb search key. If this doesn't open the IMDb web page we have a problem.
 
Joined
Aug 13, 2011
Messages
19
Reaction score
0
I see, yeah, when I run it it opens the url of the IMDb page in safari, but it opens each one in a different window so if u select 19 like i did, you get 19 windows pop up. But, will that matter if this is just a test, as I take it when it's done you won't actually see the window it's taking data from, it'll just do it in the background right? Sort of like a 'click-a-button and it magically updates the info for all your movies'?
 
Joined
Nov 26, 2010
Messages
3,558
Reaction score
52
Yes, this will be of more use 2 years from now, when you want to revisit the web page to read more about a movie. But a good starting place.

I'm not sure about scraping the actual web pages for 2 reasons, It is that it is violation of Amazons terms of use, and parsing Web pages is very fragile in AppleScript. So we'll use http://www.imdbapi.com/ to get the information you need.
 
Joined
Aug 13, 2011
Messages
19
Reaction score
0
Ok, but what do Amazon have to do with it? and that imdbapi looks ok, you seem to know what you're doing anyway
 
Joined
Nov 26, 2010
Messages
3,558
Reaction score
52
The value coming from iTunes for rating for the stars is:

-- rating (integer) : the rating of this track (0 to 100)
-- star values 0, 20, 40, 60, 80, 100

does this effect your sorting calculation?
 
Joined
Aug 13, 2011
Messages
19
Reaction score
0
Is this for the personal vote? I am a bit lost as to what you mean, but using those figures will definitely affect the sorting calculation. Can you not just use the actual title's rating without the star system? like 7.4 or 8.3, n just forget the stars?
Sorry if that's not what you meant.
 
Joined
Aug 13, 2011
Messages
19
Reaction score
0
Also, when you come to adding the equation for getting the weighted rating, it turns out iTunes doesn't like to sort numbers ascending for some reason so instead of those closest to 10 being at the top of the list, the result of the equation needs to be subtracted from 10 making those that are closest to zero be at the top of the list.
Don't know if you've got that far yet, but just thought I'd mention it.
 
Joined
Nov 26, 2010
Messages
3,558
Reaction score
52
(Total Votes/(Total Votes+3000)*(((Overall Rating*2.46)+My Rating)/3.46)+(3000/(Total Votes+3000))*6.9)*1000000 = X

Is the My Rating from iTunes Star rating? Maybe I'll post what I have and then you can alter with the calculation to suit;-)
 
Joined
Aug 13, 2011
Messages
19
Reaction score
0
It is from the star rating given by each individual person yeah, but on the /ratings page of each title there is actually a line that reads: 'Your Rating: 10' or whatever, so you don't have to deal with the star system
 
Joined
Nov 26, 2010
Messages
3,558
Reaction score
52
Code:
tell application "iTunes"
	set holdSelectedMovies to selection
	if holdSelectedMovies is not {} then
		set holdFrontOfSearch to "http://www.imdbapi.com/?i="
		repeat with holdTrack in holdSelectedMovies
			set holdEndSearch to comment of holdTrack
			-- rating (integer) : the rating of this track (0 to 100)
			-- 0, 20, 40, 60, 80, 100
			set holdMyRating to rating of holdTrack
			if holdEndSearch is not "" then
				set holdAllSearch to holdFrontOfSearch & holdEndSearch
				set holdResults to (do shell script "curl " & holdAllSearch) as text
				if holdResults is not equal to "" then
					set holdResponse to findKey("Response", holdResults) of me
					if holdResponse is equal to "True" then
						set holdRating to findKey("Rating", holdResults) of me
						set holdVotes to findKey("Votes", holdResults) of me
						set holdX to (holdVotes / (holdVotes + 3000) * (((holdRating * 2.46) + (holdMyRating)) / 3.46) + (3000 / (holdVotes + 3000)) * 6.9) * 1000000
						set sort name of holdTrack to holdX as integer as string
					end if
				end if
			end if
		end repeat
	end if
end tell

to findKey(theKey, curlResults)
	set curlResults to trimBraces(curlResults) of me
	
	set holdNormalDelimiter to AppleScript's text item delimiters
	set AppleScript's text item delimiters to "\",\""
	set holdKeyValuePairs to curlResults's text items
	
	set AppleScript's text item delimiters to "\":\""
	repeat with holdKeyValueText in holdKeyValuePairs
		set holdKeyValuePairList to holdKeyValueText's text items
		if 1st item of holdKeyValuePairList is equal to theKey then
			set AppleScript's text item delimiters to holdNormalDelimiter
			return last item of holdKeyValuePairList as text
		end if
	end repeat
	
	set AppleScript's text item delimiters to holdNormalDelimiter
	return ""
end findKey

to trimBraces(someText)
	set someText to text 3 thru ((length of someText) - 2) of someText
	return someText
end trimBraces

This still only changes the selected Movies, as I don't want it to run amuck on the whole Library. The one thing that I'm not happy about is that it Parses the JSON each time I can findKey, but even with a large Library thats not much of a hit, compared to getting the data from the web.

set holdX to (holdVotes / (holdVotes + 3000) * (((holdRating * 2.46) + (holdMyRating)) / 3.46) + (3000 / (holdVotes + 3000)) * 6.9) * 1000000

is the Line to change to get the rating system you want.

-- 0, 20, 40, 60, 80, 100
set holdMyRating to rating of holdTrack
Is the rating from the iTunes Stars, 1 Star = 20 points
 
Joined
Aug 13, 2011
Messages
19
Reaction score
0
Oh right, I see, you're using the iTunes rating system. That doesn't really come into it, the 'myRating' value is my rating on imdb not my rating on iTunes, sorry if that complicates everything
 
Joined
Nov 26, 2010
Messages
3,558
Reaction score
52
I don't know how IMDb calculates their star system. I'd guess, like you, they have some complex weighted calculation. The problem they would face with Averages on a very large data set is everything is sucked to half way.

Here is a list of other Keys available from imdbapi.com

Title, Director, Writer, Actors, Plot, Poster, Runtime, Rating, Votes, Genre, Released, Year, Rated, ID (IMDb ID)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top