Small is Beautiful: working with short URLs

Today’s world limits our expressions lenght to 140 character. No matter you text SMS or Tweet, you have to fit your sentences based on this restriction. Further to that, nobody has toleration for longer statements. That’s why, text material should be picked and chosen. Herein, some subsdiary materials such as long links would cause to go waste. To sum up, small is beautiful.


Visit Deep Learning Enabled Art Exhibition: Digital Van Gogh




blowfish
Bitly’s blowfish logo stands for url shortening

URL shortening services redirect long urls to shorter one. This kind of urls are friendly for messaging technologies requiring limited number of characters such as sms. Thus, you can append a link into your message and message quota would not be exceeded. Moreover, short urls are memorable. That’s why, referencig short urls are common in hard copied paragraphs such as newspapers or magazines.

Formely, Twitter automatically shortens links with Bitly service, this makes Bitly popular. Today, The Blue Bird consumes their own t.co service.

What’s more, Bitly and Google are the most common url shortening service providers for end users. Although, web inteface of these service providers are easy to consume, urls should be shorten manuelly. In this post, we will focus on how to consume these services in our programs.

Bitly is dedicated firm for url shortening whereas url shortening is one of the services of Google. This means that Google might terminate url shortening service if the service would not be profitable. Similarly, Google terminates Freebase or Google Reader in the past. On the other hand, Bitly makes money only on url shortening. That’s why, termination of Bitly’s service would be surprise. Anyhow, we will mention integrating for both Bitly and Google.

No matter which service provider you consume, you need to register you projet and create a key. You should apply following orders to create application keys.

If you prefer to consume Google as url shortening service, you need to visit Google Developer Console first. Then, you should visit the following menu path to create your custom key: URL Shortener API > Credentials > Create Credentials > API key.

Otherwise, you should visit Bitly Developer Console to consume Bitly’s service. Then you can create your project and key on main page.

The following java code is developed to shorten urls. It consumes both Bitly and Google services. The both services are consumed as REST services. The main difference between accessing Bitly and Google is http methods. Bitly provides to access its service with HTTP GET method whereas Google provides to access the service with HTTP POST method.,





public static void main(String[] args) throws Exception {
 String longUrl = "https://serengil.wordpress.com/2017/01/15/introduction-to-neural-networks-a-mechanism-taking-lessons-from-the-past/";

 System.out.println("Long url: " + longUrl+"\n");

 System.out.println("Bitly short url: "+shorten("Bitly", BITLY_KEY, longUrl)+"\n");
 System.out.println("Google short url: " + shorten("Google", GOOGLE_KEY, longUrl));
}

public static String shorten(String authority, String key, String longUrl) throws Exception {
 if ("Bitly".equals(authority)) {
  String endpoint = "https://api-ssl.bitly.com/"
   + "/v3/shorten?access_token=" + key
   + "&longUrl=" + longUrl;

  longUrl = longUrl.replace(":", "%3A");
  longUrl = longUrl.replace("/", "%2F");
  longUrl = longUrl.replace("&", "%26");
  longUrl = longUrl.replace(" ", "%20");

  java.net.URL url = new java.net.URL(endpoint);
  java.net.HttpURLConnection request = (java.net.HttpURLConnection) url.openConnection();

  request.setRequestMethod("GET");
  request.connect();

  java.io.BufferedReader reader = new java.io.BufferedReader(
   new java.io.InputStreamReader(request.getInputStream()));

  String serviceResponse = "";
  String line;

  while ((line = reader.readLine()) != null) {
   serviceResponse += line;
  }

  System.out.println("Service response: "+serviceResponse);

  return extractUrl(serviceResponse, "url");
 }
 else if ("Google".equals(authority)) {
  String endpoint = "https://www.googleapis.com/"
   + "urlshortener/v1/url?fields=id%2ClongUrl&key=" + key;

  String data = "{\"longUrl\": \"" + longUrl + "\"}";

  java.net.URL url = new java.net.URL(endpoint);
  java.net.HttpURLConnection connection = (java.net.HttpURLConnection) url.openConnection();
  connection.setRequestMethod("POST");
  connection.setDoOutput(true);
  connection.setRequestProperty("Content-Type", "application/json");

  java.io.OutputStreamWriter output = new java.io.OutputStreamWriter(connection.getOutputStream());
  output.write(data);
  output.flush();

  java.io.BufferedReader response = new java.io.BufferedReader(
   new java.io.InputStreamReader(connection.getInputStream()));

  String serviceResponse = "";
  String line;
  while ((line = response.readLine()) != null) {
   serviceResponse += line;
  }

  System.out.println("Service response: "+serviceResponse);

  return extractUrl(serviceResponse, "id");

 }

 return null;
}

public static String extractUrl(String serviceResponse, String keyword){
 serviceResponse = serviceResponse.replace(" ", "");
 keyword = "\""+keyword+"\":\"";
 int keywordLength = keyword.length();

 for (int i = 0; i < serviceResponse.length(); i++) {
  if (i < serviceResponse.length() - keywordLength && keyword.equals(serviceResponse.substring(i, i + keywordLength))) {
   for (int j = i + keywordLength; j < serviceResponse.length(); j++) {
    if ("\"".equals(serviceResponse.substring(j, j + 1))) {
     return serviceResponse.substring(i + keywordLength, j);
    }
   }
  }
 }

return null;
}

So, the following results are produced when the code run. Now, we can involve shortening url service in our business flows. Fortunately, short urls produced by Bitly and Google are non-temporary. Moreover, we would not consider hash collision. In other words, we can produce unique and everlasting urls.

url_shorten_output
URL Shortening via Bitly and Google

Managing

The both Bitly and Google services provide management console to monitor link shortening trafics, quota and click statistics.

bitly-console
Bitly Bitlinks Management Console
google-api-console
Management Console of Google API for URL Shortener

Finally, Bitly limits to shorten 10k links per month for free accounts wheresas providing larger limits for premium accounts. On the other hand, Google limits to shorten 1M link per days. However, Google restricts to shorten 100 links per 100 seconds. That prevents to shorten bulk links. In contrast, Bitly does not restrict traffic in that way.

As mentioned before, Bitly is dedicated firm for url shortening. That’s why, terminating its services would not be expected. However, there are services shut down by Google before. So, I prefer to consume Bitly’s url shortening service for personal use.


Support this blog if you do like!

Buy me a coffee      Buy me a coffee


1 Comment

Comments are closed.