Blog Planet Wiki
Feed Break It

TinyURL API in Python and C#

Sunday, May 24, 2009 03:00 PM

Did you know that tinyurl.com has an API? A few months ago while writing lensherr, I was poking around and discovered their API. All you have to do is make a HTTP GET request to this endpoint http://tinyurl.com/api-create.php with a url parameter of the url you want to tinyurl. It's that simple. Below are code snippets of working classes that I have used in past projects.

TinyURL Python class, that is included in my google code lensherr project.

class tinyurl:
        
        API_CREATE_URI = "http://tinyurl.com/api-create.php?url=%s"
        
        def __init__(self, url):
                self.url = url
        
        def create(self):
                encoded_url = urllib.urlencode({'url':self.url})[4:]
                new_url = tinyurl.API_CREATE_URI % (encoded_url)
                tinyurl_request = urllib2.Request(new_url)
                tinyurl_handle = urllib2.urlopen(tinyurl_request)
                return tinyurl_handle.read()

TinyURL C# class, that is included in my github repository here.

public static class TinyUrl
{
    public static string Create(string url)
    {
        string tinyUrlApiFormat = "http://tinyurl.com/api-create.php?url={0}";
        string tinyUrlCall = string.Format(tinyUrlApiFormat, HttpUtility.UrlEncode(url));
        string response = string.Empty;
        HttpWebRequest hr = (HttpWebRequest)WebRequest.Create(tinyUrlCall);
        hr.Method = "GET";
        HttpWebResponse hwr = (HttpWebResponse)hr.GetResponse();
        using(System.IO.Stream receiveStream = hwr.GetResponseStream())
        using (System.IO.StreamReader sr = new System.IO.StreamReader(receiveStream, System.Text.Encoding.UTF8))
        {
            response = sr.ReadToEnd();
        }
        return response;
    }
}

Happy Hacking.

Surpressing the Ugly Javascript Alert

Tuesday, May 12, 2009 12:51 AM

Ever work in an environment where developers use javascript:alert("oh hai!"); to debug their code? The lack of using console.log now results in nasty javascript alerts all over the place in production code. It is a less than plesant user experience. The real way to solve this problem is to scrub the source code of all javascript alerts. Anyhow I wrote some javascript to surpress window.top.alert.

This script block should be thrown somewhere in the head, and slingshot.load() in the onload/onready event. For purposes of my example I just threw the slingshot.load() right inline.

<script type="text/javascript">
	var slingshot = {
		load:function(){
			window.top.oldAlert = window.top.alert;
			window.top.alert = function(){ return; };
		},
		trace:function(message){
			if(slingshot.debug)
			{
				window.top.oldAlert(message);
			}
		},
		debug:false
	};
	slingshot.load();
</script>

In order to actually show the alerts just set slingshot.debug to true.

<script type="text/javascript">
	slingshot.debug = true;
	slingshot.trace("hello world!");
</script>

Enjoy.

Poor man with lots of money

Saturday, May 09, 2009 01:55 PM

I'd like to live as a poor man with lots of money.

Pablo Picasso

Eye Of The Tiger

Saturday, April 25, 2009 09:05 PM

Found this video of a bunch of kids singing "Eye of the Tiger". I really heart this song.

Of course this post wouldn't be right without the original.

Designer Leaves Google For Twitter

Monday, March 23, 2009 06:25 AM

Douglas Bowman has left Google and joined Twitter.

My favorite excerpt from his "goodbye google" post:

Yes, it's true that a team at Google couldn't decide between two blues, so they're testing 41 shades between each blue to see which one performs better. I had a recent debate over whether a border should be 3, 4 or 5 pixels wide, and was asked to prove my case. I can't operate in an environment like that. I've grown tired of debating such minuscule design decisions. There are more exciting design problems in this world to tackle.
Being pragmatic can only take a company so far. From what I have read/seen with Google, being pragmatic is at their ethos. Rightly so, they can use data to make choices in ways that benefit their business. If other companies tried the pragmatic approach they would most likely fail at it. I'm sure what makes Mr. Bowman great cannot be emulated through a-b testing. I cannot wait to see how his influence shapes the twitter interface.