iTunes Music Store 500 Million Song Countdown

Yes, I know it’s useless, but I wanted to at least try. If you haven’t seen it, Apple now has a countdown on their page counting down to the 500 millionth song sold at the iTunes Music Store. The commented code towards the end is the same as some of the javascript at http://wdirect.apple.com/home/counter500million.js.

property xmlPath : "http://www.apple.com/itunes/external_counter.xml"
set xmlData to do shell script "curl " & quoted form of xmlPath

set old_delimiters to text item delimiters
set text item delimiters to "timestamp=\""
set xmlData to text items of xmlData
set text item delimiters to "\">"
set xmlData to text items of (xmlData as string)
set text item delimiters to "</count>"
set xmlData to text items of (xmlData as string)
set text item delimiters to old_delimiters

set timeToGMT to time to GMT
set initCount to item 3 of xmlData
set initDate to (date (item 2 of xmlData)) + timeToGMT
set preCount to item 6 of xmlData
set preDate to (date (item 5 of xmlData)) + timeToGMT

--calculate difference in values
set countDiff to initCount - preCount
--calculate difference in time of values
set dateDiff to initDate - preDate
--calculate rate of increase ((songs downloaded in previous time)/time)*incr
set rate to countDiff / dateDiff
set rate to rate * 0.8 --dunstan adjusting the rate down to 80% as per dan's instructions
--calculate number of songs to add each loop
--calculate current time
set curDate to (current date)
--calculate time since last update
set curDateElapsed to curDate - initDate
--calculate projected number of songs since last update
set curCountElapsed to curDateElapsed * rate
--add projected number of songs to most recent value
set curCount to initCount + curCountElapsed

set curCount to round curCount
-- Edit: Added this:
if curCount is greater than or equal to 500000000 then set curCount to "500000000+"

get curCount

Darn, I just saw an applescript app at MacUpdate that does the same thing (http://www.macupdate.com/info.php/id/18718). Oh well, doesn’t matter.

:rolleyes: - - - - - Wonders why so many people are trying to do this - - - - - :rolleyes:
I mean, surely they have better things to do with their time. :wink:

cool stuff!

Changed to work with the new 1 billion song counter.
Edit: added parentheses around (xmlData as string).

property xmlPath : "http://www.apple.com/itms_counter.xml"

set xmlData to do shell script "curl " & xmlPath

set text item delimiters to "timestamp=\""
set xmlData to text items 2 thru 3 of xmlData
set text item delimiters to "</count>"
tell xmlData as string to set xmlData to {text item 1, text item 3}
set text item delimiters to "\">"
set {initDate, initCount, preDate, preCount} to text items of (xmlData as string)
set text item delimiters to ""

set timeToGMT to time to GMT
set initDate to (date initDate) + timeToGMT
set preDate to (date preDate) + timeToGMT

(* Following used from http://images.apple.com/home/counter1billion.js *)
--calculate difference in values
set countDiff to initCount - preCount
--calculate difference in time of values
set dateDiff to initDate - preDate
--calculate rate of increase ((songs downloaded in previous time)/time)*incr
set rate to countDiff / dateDiff
set rate to rate * 0.8 --pledit
--calculate number of songs to add each loop
--calculate current time
set curDate to (current date)
--calculate time since last update
set curDateElapsed to curDate - initDate
--calculate projected number of songs since last update
set curCountElapsed to curDateElapsed * rate
--add projected number of songs to most recent value
set curCount to initCount + curCountElapsed

tell curCount as string to set curCount to character 1 & text 3 thru (2 + (last word) as number) of first word

curCount

I tried to make that an applescript studio app simply by adding:

	set contents of text field "count" of window "Window" to (curCount)

But it didn’t work. Nor did display dialog (curCount)

It worked for me in Script editor, why not applescript studio?

I added a bit to it, and found that they got around 45 downloads a second :stuck_out_tongue:

When I run it, it errors at the first instance of “rate” with “Can’t make rate into type reference.”

Generally, it’s not a good idea to use regular words as variable labels - since they might also be used as terms by an application or scripting addition. In this case, it seems likely that the word rate is being claimed as some property or other.

Try changing all occurrences of rate to, say, curRate - or something equally unlikely to cause a namespace conflict within your current setup.

The commented block of code is copied straight from http://images.apple.com/home/counter1billion.js, with the original variable names untouched.

Understood, Qwerty. All I’m saying is that I suspect Adam may have some third-party scripting addition on board that’s claiming the word rate as a property - or some other type of keyword.

(He can determine whether or not this is the case by simply checking the formatting of the word, once the script is compiled in Script Editor. If it’s not the same font/colour as a regular user variable, then that will be the cause of the script failing on his machine.)

Of course, one way to fix the problem might be to locate and disable the ‘culprit’ software. But that would remove its functionality - and it would surely be preferable to simply change the variable name to something less likely to cause a namespace conflict (especially since the same issue could arise on other machines). Just something else to consider when porting code from one environment to another, I’m afraid.

While I may be wrong about the possible source of the problem, this still seems the most likely explanation to me. Hopefully, if Adam happens to read this, he’ll be able to shed some more light on the matter… :slight_smile: