HypeMachine Cocoa Woes

Published 2011-03-23 on Farid Zakaria's Blog

Cocoa

So I've been working at trying to refit the Python batch downloader as a GUI application; specifically a mac application written in Cocoa. However, porting the existing code has been much more troublesome/difficult than I expected.

Background

Each song link in HypeMachine is built using 2 tags: the song's id and a key. With these two tags, the final url to stream the song is:

'http://hypem.com/serve/play/' + id + '/' + key + '.mp3'

As far as I can tell from what I've determined (my javascript is pretty limited), is that the key's are generated on the server side based on a timestamp. So if you look at my python code, I perform the following steps:


url='http://hypem.com/popular/1?ax=1&ts='+str(time.time())
request = urllib2.Request(url)
response = urllib2.urlopen(request)
html = response.read()

Running the above script multiple times appropriately generates different keys every time.

The key and ID are parsed from the HTML, where they occur in the following format:


trackList[document.location.href].push({	
	type:'normal',
	id:'1a08r',
	postid:'1433616',
	posturl:'http://jackinforlinks.com/wiz-khalifa-rooftops-ft-curreny/',
	time:'261',
	ts: '1300754012',
	fav:'0',
	key: '30b2310a6f3fb28564d443ffb3804ffc',
	imeem_id:'',
	artist:'Wiz Khalifa',
	song:'Rooftops Ft. Currensy (Produced By E. ...',
	amazon:'',
	itunes:'',
	emusic:'',
	exact_track_avail:'0'
  });

The problem

I've been running into multiple problems porting this over to Objective-C cocoa. However the most damaging is the fact that I cannot seem to generate the appropriate HTML file. The HTML file I keep generating has invalid keys.

I am retrieving the HTML and parsing it similarly as I did in Python however the server is returning me invalid keys.

Not only are the keys invalid, however re-running my application, retrieves the same keys over and over. This helps confirm that they are feeding me garbage keys as they are not being properly dynamically generated. I've attempted to retrieve the keys in the following manner (both of which do not work):


NSTimeInterval time = [[NSDate date] timeIntervalSince1970];
NSString * completeUrl = [url stringByAppendingFormat:@"/%d?ax=1&ts=%1.2f", pageNumber, time];
NSURL * hypeURL = [NSURL URLWithString:completeUrl];

//ATTEMPT 1
NSString * hypeHTML = [NSString stringWithContentsOfURL:hypeURL encoding:NSASCIIStringEncoding error:nil];

//ATEMPT 2
NSString* userAgent = @"Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8) Gecko/20051111 Firefox/1.5 BAVM/1.0.0";
NSMutableURLRequest* request = [[[NSMutableURLRequest alloc] initWithURL:hypeURL] autorelease];
[request setValue:userAgent forHTTPHeaderField:@"User-Agent"];
NSURLResponse* response = nil;
NSError* error = nil;
NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *hypeHTML = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];

Request

If anyone has some ideas on how they can help me fix my problem please let me know via a comment, e-mail or my question at Stackoverflow. I'm also looking for help on setting up the GUI / Core data model for the application once I get this issue sorted out if you'd like to help in that respect as well.