// // LyricHistogram.m // LyricsHistogram // // Created by Varun Mehta on 5/3/10. // #import #import "LyrDbInterface.h" #import "LyricsHistogramUtils.h" #import "iTunes.h" #import void printHelp(); int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; //Options configuration BOOL weightedOperation = NO; NSString *outputPath = @"~/lyricsHisto.csv"; if (argc > 4) { printHelp(); return -1; } for (int i = 1; i < argc; i++) //skip program name - we know who we are { NSString *optionString = [NSString stringWithCString:argv[i] encoding:[NSString defaultCStringEncoding]]; if ([optionString caseInsensitiveCompare:@"-w"] == NSOrderedSame) weightedOperation = YES; else if ([optionString caseInsensitiveCompare:@"-o"] == NSOrderedSame) //output file specified { outputPath = [NSString stringWithCString:argv[i+1] encoding:[NSString defaultCStringEncoding]]; //get the output file path i++; //move the index ahead } else { NSLog(@"Unrecognized option: %@", optionString); printHelp(); return -1; } } //Report on options NSLog(@"iTunes play count-based weighting is %@.", weightedOperation?@"ENABLED":@"DISABLED"); NSLog(@"Output file is %@", outputPath); //Begin doing actual work NSMutableDictionary *allWords = [[NSMutableDictionary alloc] init]; iTunesApplication *iTunes = [SBApplication applicationWithBundleIdentifier:@"com.apple.iTunes"]; SBElementArray *libraryPlaylists = [[[iTunes sources] objectAtIndex:0] libraryPlaylists]; for (iTunesLibraryPlaylist *playlist in libraryPlaylists) { SBElementArray *trackList = [playlist tracks]; NSLog(@"Number of tracks: %d", [trackList count]); int i = 0; for (iTunesTrack *aTrack in trackList) { i++; //if (i >= 100) break; //We will weight each word in the song based on how many times the song has been played. //This way songs that are more frequently listened-to (or never listened to) are given appropriate importance int trackPlayCount = [aTrack playedCount]; if (trackPlayCount == 0) continue; //shortcut to skip songs that have never been played //NSLog(@"%@ by %@", aTrack.name, aTrack.artist); if ([aTrack.name isEqualToString:@""] || [aTrack.name isEqualToString:@""]) continue; NSString *returnString = [LyrDbInterface lyricsForSong:aTrack.name byArtist:aTrack.artist]; //[returnString autorelease]; NSArray *strippedStrings = [LyricsHistogramUtils stripString:returnString]; for (NSString *word in strippedStrings) { word = [word lowercaseString]; if ([allWords objectForKey:word] == nil) [allWords setObject:[NSNumber numberWithInt:0] forKey:word]; int value; if (weightedOperation) value = [[allWords objectForKey:word] intValue]+trackPlayCount; //This is how weighting is applied else value = [[allWords objectForKey:word] intValue]+1; //Unweighted, so just add 1 [allWords setObject:[NSNumber numberWithInt:value] forKey:word]; } NSLog(@"Processed %d of %d tracks.", i, [trackList count]); } } //Write out the word/frequency pairs NSMutableString *outputString = [[NSMutableString alloc] init]; for (NSString *key in [allWords allKeys]) { int wordCount = [[allWords objectForKey:key] intValue]; [outputString appendFormat:@"%@,%d\n",key, wordCount]; } //Dump the output to file [outputString writeToFile:[outputPath stringByExpandingTildeInPath] atomically:true]; NSLog(@"Data written to %@, go analyze it!", outputPath); [pool drain]; return 0; } void printHelp() { NSLog(@"Help for LyricsHistogram:"); NSLog(@"\t-w : Use weighted operation - adds frequency for each word based on played count in iTunes (default: off)"); NSLog(@"\t-o [outputFile] : Send csv output to the specified path. Defaults to ~/lyricsHisto.csv"); }