Like many institutions, we put numbers for our various online presences in our annual report and other presentations: YouTube views, Twitter followers, Facebook fans, etc. For most services, this is very easy to do: log in, go to the stats page, write the big number down. We also want to include the iTunes U numbers, but for iTunes U, there is no centralized stats reporting outside of the Microsoft Excel file Apple sends iTunes U administrators every week. Tabulating the stats by hand would be time consuming and error-prone, so I wrote a short python script to automate it. Here is is:
[python]
#!/usr/bin/env python
# encoding: utf-8
"""
itunesUstats.py
Created by Justin Heideman on 2010-05-19.
"""
import sys, os, glob, xlrd
def main():
#change this to the path where your stats are
path = ‘itunes_U_stats/all/’
totalDownloads = 0
for infile in glob.glob( os.path.join(path, ‘*.xls’) ):
# open the file
wb = xlrd.open_workbook(infile)
# get the most recent days’s tracks
sh = wb.sheet_by_index(1)
# get the downloads from that day
downloads = sh.col_values(1)
#first entry is u’Count’, whcih we don’t want
downloads.pop(0)
#sum it up
totalDownloads += sum(downloads)
# show a little progress
print sum(downloads)
#done, output results
print "—————————————————-"
print "Total downloads: %d" % totalDownloads
if __name__ == ‘__main__’:
main()
[/python]
This script uses the excellent xlrd python module to read the Excel files (simple xlrd tutorial here), which is roughly 27.314 times easier than trying to use an Excel macro to do the same thing. To use this, simply change the path on line 13 to a directory containing all your iTunes stats files, and run the script from the command line. You’ll get output like this:
929.0 732.0 779.0 854.0 1000.0 987.0 765.0 812.0 1275.0 1333.0 1114.0 1581.0 1278.0 1568.0 1854.0 2102.0 1108.0 1078.0 ---------------------------------------------------- Total downloads: 21149
Do note that the script is tied to the excel format Apple has been using, so if they change it, this will break. Apple explains the iTunes report fields here. This script tells you ” all the iTunes U tracks users downloaded that week through the DownloadTrack, DownloadTracks, and SubscriptionEnclosure actions”.


