Farid Zakaria

Archive 3 min read

HypeMachine Cocoa Woes


This post comes from my original WordPress blog, written between 2009 and 2018. It is kept here verbatim as an archive — the formatting did not always survive the move, and I no longer agree with all of it.

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.

6 archived comments

Thanks and Up Next – Small Thoughts 2011-03-23

[…] Small Thoughts A blog by Farid Zakaria; somewhat software oriented Skip to content AboutProjectsCompendiumCalendarSitemap “Successful interactions with objects that you use should get simpler, not more complex–you don’t need to be watching the car’s engine running in order to drive it, the experience should be intuitive”by Steve Jobs « HypeMachine Cocoa Woes […]

Anthony Volodkin 2011-03-29

Oh, I know. :)

Farid Zakaria 2011-03-29

@Anthony Volodkin:

At least I’m not going crazy and you guys are doing something lol.

What’s odd is that the Python script still works… so worst case scenario is I can run the script in the application but I’m confused why urllib2 works but not NSURLRequest….

Any tips ? ;)

Michael Soares 2011-03-29

Anthony be trollin’.

Farid Zakaria 2011-03-30

@Anthony Volodkin: Solved ;)

HypeMachine Cocoa Win – Small Thoughts 2011-03-30

[…] had made an earlier post regarding how specifically I have been having trouble porting over my python hypemachine script […]