code, stuff and junk

a simple tinyurl utility in objective-c

I’ve been diving into Cocoa/Objective-C lately and I feel like things are finally starting to click. Like every other language I know, over time I try to build a library of useful utilities, components and subclasses to be reused in future projects – as well as to share with the public. Here is one that talks to the TinyURL API.

//
//  TinyURL.h
//  @author Nick Jensen
//
 
#import 
 
extern NSString * const MAKE_TINY_SERVICE;
 
@interface TinyURL : NSObject 
 
+ (NSString *) makeTiny:(NSString *)aLongURL;
 
@end

…and here is the implementaion.

//
//  TinyURL.m
//  @author Nick Jensen
//
 
#import "TinyURL.h"
#import "NSString+URLEncoding.h"
 
NSString * const MAKE_TINY_SERVICE = @"http://tinyurl.com/api-create.php";
 
@implementation TinyURL
 
+ (NSString *) makeTiny:(NSString *)aLongURL
{
	NSMutableString *urlString = [[NSMutableString alloc] initWithString:MAKE_TINY_SERVICE];
	[urlString appendString:[NSString stringWithFormat:@"?url=%@", [aLongURL URLEncodedString]]];
 
	NSURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]
							cachePolicy:NSURLRequestReloadIgnoringCacheData
						    timeoutInterval:30.0];
	NSURLResponse *response;
	NSData *data = [NSURLConnection sendSynchronousRequest:request 
					     returningResponse:&response 
							 error:nil];
	if (data)
	{
		NSString *shortURL = [[NSString alloc] initWithData:data 
							   encoding:NSASCIIStringEncoding];
		[shortURL autorelease];
		return shortURL;
	}
	else
	{
		return nil;
	}
}
 
@end

Also required are:
NSString+URLEncoding.h
NSString+URLEncoding.m

It turns out that Objective-C is lacking in the ability to URLEncode a string properly. NSString has the method stringByAddingPercentEscapesUsingEncoding, but since the & and / chars are valid URL characters they will not be encoded.

Well this is a problem. What if the data your sending to some URL contains these characters? What if the data you’re sending IS in fact a URL. Whoa. I don’t even want to think about that. That’s like… typing ‘google’ into Google. I think that would break the internet.

find free music with a python script

After reading many different articles about how you can use Google to find free mp3s, I just couldn’t help but think… I should script this. The result actually turned out to be a somewhat sophisticated little program. Check it out.

musicfinder.py (right click and save) or use wget:

me@localhost:~ $ wget http://random.noflashlight.com/scripts/musicfinder.py

Now make the script executable:

me@localhost:~ $ chmod +x musicfinder.py

Now fire up a terminal and run the program like so…
Example 1
The first argument is always the search terms. If you want to search more than one word be sure to wrap them in quotes.
Example 2
If you would like to skip a file that is downloading, hit CTRL+C. By default the program will only search 10 google result pages. If you would like to search more, use the -r flag. Acceptable values are 5, 10, 25, 50, and 100.
Example 3
One last thing to note is that by default the strict flag is turned on. What this means is that the program won’t download a file unless the search term is found somewhere in the URL to the file. I found that this produced MUCH better results. For example, if I searched for Radiohead a page might come back that has Radiohead songs on it but also had hundreds of other songs by random obscure artists that I’m not interested in downloading. However if you would like to turn off the strict flag you can pass the –non-strict argument. I have tried to test this the best I can, but I’m sure there are still some bugs that I’ve missed. I am open to and welcome additions and improvements to this script. Enjoy!

you’ve got 30 minutes… to write a python script

I bet you wished that you had a sweet script that could tell you the status of your dominos pizza order in real time. Well today is your lucky day.

dominos.py (right-click and save)
screenshot

Instructions:

First download dominos.py. You can right-click the link and select save, or use wget:

me@localhost:~ $ wget http://random.noflashlight.com/scripts/dominos.py
me@localhost:~ $ chmod +x dominos.py

That’s it. Now all you have to do is order a pizza, and then execute this script from the command line like this:

me@localhost:~ $ ./dominos.py 'your phone number'

Be sure to keep running the script periodically for the real time status of:

  • When the pizza is being made.
  • When the pizza is in the oven.
  • When the pizza is cooked and waiting.
  • When the pizza is on its way.
  • When the pizza was delivered.