Using Google's URL Shortener (goo.gl) in WordPress

The Google URL Shortener API has been released this week so I came up with a short snippet that generates short goo.gl URLs. The script is quite simple, you can paste it in your theme’s functions.php file or create a plugin out of it, so without further ado:

function googl_shortlink($url, $post_id) {
	global $post;
	if (!$post_id && $post) $post_id = $post->ID;

	if ($post->post_status != 'publish')
		return "";

	$shortlink = get_post_meta($post_id, '_googl_shortlink', true);
	if ($shortlink)
		return $shortlink;

	$permalink = get_permalink($post_id);

	$http = new WP_Http();
	$headers = array('Content-Type' => 'application/json');
	$result = $http->request('https://www.googleapis.com/urlshortener/v1/url', array( 'method' => 'POST', 'body' => '{"longUrl": "' . $permalink . '"}', 'headers' => $headers));
	$result = json_decode($result['body']);
	$shortlink = $result->id;

	if ($shortlink) {
		add_post_meta($post_id, '_googl_shortlink', $shortlink, true);
		return $shortlink;
	}
	else {
		return $url;
	}
}

add_filter('get_shortlink', 'googl_shortlink', 9, 2);

For the tech people I’ll explain what I’m trying to do here. So first of all we don’t want to shorten links on draft posts as that could be a waste of resources, so we return an empty string. The magic with get_post_meta and add_post meta in the middle and at the end of the function are done for caching purposes — we don’t want to send too many requests to the Google API so let’s keep the already shortened links in the post’s custom fields. This may be annoying in the UI so you might want to add an underscore in the meta key (“_googl_shortlink”) which will make it invisible to the UI.

Next up, I retrieve the permalink of the published post, since we don’t want to shorten an already shortened URL which is that ugly one by WordPress. I use that permalink in an HTTP request to Google’s API using WordPress’ WP_Http object. Note that I’ve added the Content-Type header, which is required by Google, otherwise it’ll return an error. I decode the result body using json_decode (you need PHP 5.2 or later to do this) and if there’s a valid id, which is the shortened URL, I save it to the post’s meta and return the shortlink. If something went wrong, I return the original WordPress’ short URL.

So, clicking on the “Get Shortlink” button in your admin UI will now produce a Goo.gl shortened link to your post. Neat eh? You can also use short links in your theme, somewhere in The Loop:

echo "Shortlink: " . wp_get_shortlink();

Goo.gl Analytics

Right, you might want to see your short links’ analytics at some point. Well, Goo.gl shortened links are all public and their analytics are public too, so all you have to do is browse to goo.gl/info/code where code is the 5-6 alphanumeric digits attached to your shortened URL at the end.

For convenience I wrote a little snippet that would output the short URL and a link to it’s analytics page in your posts list screen inside the admin panel. Below is the code that should go to your functions.php file or plugin file:

function googl_post_columns($columns) {
	$columns['shortlink'] = 'Shortlink';
	return $columns;
}

function googl_custom_columns($column) {
	global $post;
	if ('shortlink' == $column)
	{
		$shorturl = wp_get_shortlink();
		$shorturl_caption = str_replace('http://', '', $shorturl);
		$shorturl_info = str_replace('goo.gl/', 'goo.gl/info/', $shorturl);
		echo "<a href='{$shorturl}'>{$shorturl_caption}</a> (<a href='{$shorturl_info}'>info</a>)";
	}
}

add_action('manage_posts_custom_column', 'googl_custom_columns');
add_filter('manage_edit-post_columns', 'googl_post_columns');

I wrote about custom columns in a post called Custom Post Types in WordPress 3.0, which should give you a general idea of what’s going on here. For more details visit the WordPress Codex.

That’s about it! Yeah, I know there’s some stuff that can be improved here, like smooth error handling, but it’s up to you to implement it. For more info check out the Google URL Shortener Getting Started page. A good place for experiments with Google’s APIs is Google APIs Console.

The plugin-version of this code is available at the WordPress plugin directory: Goo.gl for WordPress. Thank you for reading and retweeting ;)

Review: Nexus One, Android & Software Freedom

The first time I mentioned I wanted a Nexus One phone was on January 21st, 2010. That’s almost a year ago and I was ready to trade in my iPhone for a Google phone. This Christmas I made myself a present and got that Nexus One from some “not so legal” Internet store here in Moscow. I guess they’re (Nexus One & Nexus S) not yet or ever will be certified for the Russian market, so I had a hard time finding one, but I did, and I’m glad I did.

I was an iPhone 3G user for over a year and was quite satisfied with everything that it offered. I didn’t jailbreak it, updated the iOS frequently and used the free applications from the App Store. That’s right, I never spent a cent on software for my iPhone, since most applications had an ad-driven free version available, and I’m quite greedy — I can’t remember the last time I payed for software.

I opened up the Nexus One box and figured that Facebook and Twitter applications were pre-installed, gorgeous! So I instantly tweeted, and posted a photo and the fact that it has a flashlight surprised me. I then found out it came with a 4GB SD card, which is awesome. Yeah, my iPhone had 16 gigs but you can get a 16 GB SD card for around $130 when you run out of the 4GB.

Next up was my e-mail and contacts. Honestly I was a little scared here, since as soon as I added my Gmail account, my phone had all my contacts, and not only! It linked my Twitter and Facebook contacts to my Google ones, and then I installed Skype, and there too, everything was linked and merged, so there’s no longer copying contacts from SIM cards, syncing by connecting to your PC and what not. This is definitely more user-friendly than Apple, way to go Google. And there’s more! When I started texting, Google’s dictionary recognized all the names from my contacts, so I never had to spell my or my girlfriend’s last name again — input three letters and voila!

Then I took a few hours to explore the Android Market. I never knew they had such a place, which could have made me miss my iPhone, but they did, and it was quite easy to navigate around, since it looked very much like the App Store on iPhone. I didn’t try the paid apps but I did give a go at a few dozen free and ad-driven ones, including Angry Birds ;) The Market syncs very well, giving instant applications updates when they’re available.

Next thing to surprise me was Google Voice Recognition, they got their TV ads running around here in Russia. It’s not as good as the TV says, but they get most of the stuff right. There are some language configurations which have to be done in order to recognize both English and Russian. I got that working when writing text, but not Google Voice Search, which keeps giving me junk in English when I speak Russian. Anyways, that’ll be fixed I’m sure.

Battery. Definitely better than the iPhone 3G here, and could be boosted even better with some utilities from the Market. Just make sure you don’t have Sync, Wifi and 3G turned on when you don’t really need them. Multimedia? Ah you’ll love Android if you’d like to read books, watch videos or listen to music, iPod? Meh!

Below is a list of applications that I, as a novice Android user recommend. Some might be pre-installed on your phone. Can’t get you direct links to these, but searching by names in the Android Market will get you going:

  • Foursquare — obviously!
  • Google Reader — still waiting for Feedly for Android, but this is okay I guess.
  • Chrome to Phone — I don’t use it much, but certainly a time-saver.
  • Dropbox — sync files and multimedia to the cloud.
  • ConnectBot — an easy to use SSH/telnet client. Good way to reboot your servers when in the metro ;)
  • Adobe Reader — books save you from being bored.
  • MySettings — quickly turn on and off stuff like sync, wifi, rotate and other settings.
  • OI File Manager — surely one of the best file managers for Android.
  • Thinking Space — create neat mindmaps on the fly and export them to various formats to email, Dropbox, etc.
  • WordPress — of course, it’s where I’m writing this post from ;)

There are also a bunch of interesting widgets, one of which is the Google Analytics Widget that can display pageviews or visitors directly on your home screen. Nice thing to check out waking up in the morning haha ;)

So anyways, the bottom line is yeah — I fell in love with Android. I’m not really looking forward to getting the Nexus S model, although I believe it’s better than Nexus One, and it’s been to outer-space too, but Nexus One is really a first, so I’ll sell that for a couple of thousand after a couple of decades. Besides, I think HTC’s better than Samsung anyway ;)

How to Track Your Social Links with Google Analytics

Quick and easy one. See the links to my social profiles on your right? I’ve been displaying those for years, but I never knew if they were ever clicked on, and if they were, which ones are the most popular ones? Perhaps I have to get rid of some of them, and make other brighter to get more attention? Or maybe simply switch places and keep the most relevant ones on the top left?

After thinking for a while, I figured out that Google Analytics can help me solve this in no time. I heard about Event Tracking with Google Analytics quite some time ago, and I was actually using it to find out how many people were clicking the “more widgets” link in my sidebar (not much), but this time it’s about tracking outgoing links to your social profiles and not only — you can use this technique to track any outgoing links, just make sure you keep it simple.

First things first: go through the Event Tracking guide on Google and make sure you understand what’s going on in theory. Then find out a good javascript code or jQuery selector to bring your social links together, mine is:

jQuery(".social a[rel=me]")

Then use that code to assign an onclick event to each of the social links:

jQuery(".social a[rel=me]").click(function() {
    /* some code goes here */
});

And finally add the magic Google code which does two things: A, tells the Google Analytics servers that your links was pressed and B, slows down the browser relocation for a few milliseconds so that A has a little bit more time. I know slowing down your website doesn’t sound right, but I guess that this is the only way to track visitors with low internet speed (do you have any of those?) Below is the full code that I use on my blog, you can use that as is, but keep in mind that you’ll have to modify your jQuery selector in order to match your website structure:

jQuery("div.social a[rel=me]").click(function() {
	_gaq.push(['_trackEvent', 'Social', 'Profile', jQuery(this).attr('href')]);
	setTimeout('document.location = "' + jQuery(this).attr('href') + '"', 100);
	return false;
});

As you can see, I push the event into the Social category with a Profile action, and then also pass the href attribute of the object that was clicked (a social profile in our case.) All this is then visible in your Google Analytics reports under the Content – Event Tracking section. It’s up to you how you name these, but I’m thinking globally, thus having a Social category which can house Profile clicks, and then extend it to track Share events like Facebook Likes, Retweets, etc.

Hope this helps you sort some things out and gives you a better feel of what Google Analytics is capable of. There’s much more to learn, and Google’s Manuals are there to help you. Don’t be afraid to experiment, and keep your eyes on the javascript debugger console (Chrome or Firefox) as that’s usually the first place to look at when things go wrong.

Thanks for reading and sharing. Hope you have a great New Year party, or hope you did if you’re reading this in 2011 already ;) Cheers!

App Engine: JSON Objects in the Google Datastore

Following up on my previous post about Python Objects in the Google Datastore where I’ve shown an easy way of storing any python object inside the Google Datastore by pickling it. Pickling works fine and it can literally store functions, classes and class instances in the datastore which is great, but as I discussed earlier, JSON could work too, so here I’ll introduce a similar approach at storing JSON objects (strings, lists and dictionaries) inside Google.

The problem with pickling objects is that they’re very difficult to maintain. Google provides a front-end for their Datastore where you can run GQL queries, watch statistics and even manage data on the fly. Since pickled objects are stored in bytes as opposed to JSON strings, I figured that when editing an entity using the Google admin, pickled objects tend to break, since the values sent back to the server by your browser are usually strings. I haven’t found a good way around this yet (update: I did find a way), which is why I temporarily switched to JSON, which is easier to edit and maintain.

Here’s the draft code I’m using to store JSON objects in Google App Engine, which I called (surprise) JsonProperty:

from django.utils import simplejson
from google.appengine.ext import db

class JsonProperty(db.TextProperty):
	def validate(self, value):
		return value

	def get_value_for_datastore(self, model_instance):
		result = super(JsonProperty, self).get_value_for_datastore(model_instance)
		result = simplejson.dumps(result)
		return db.Text(result)

	def make_value_from_datastore(self, value):
		try:
			value = simplejson.loads(str(value))
		except:
			pass

		return super(JsonProperty, self).make_value_from_datastore(value)

Note that Google still runs Python 2.5 hence the simplejson library is only available through the django.utils package (it ships with Python starting from version 2.6).

So the logic is quite clear here, we use dumps and loads to dump and load strings from and to the Datatstore. Perhaps it’s lacking some code in the validate method, we do after all need to check if the given value is convertible to JSON and then raise an exception if not. But I’ll leave that to the next version of this snippet, as I’m currently only using it to prototype stuff and maintain small and medium-sized projects — so do use this at your own risk ;)

As a usage sample you can take this simple code which can be run from the App Engine console:

class MyEntity(db.Model)
    name = db.StringProperty()
    obj = JsonProperty()

my_obj = {'key-1': 'value-1', 'key-2': 'value-2'}
entity = MyEntity(name="my-name", obj=my_obj)
entity.put()

entities = MyEntity.all()
entities = entities.fetch(10)
for entity in entities:
    print entity.obj # outputs the dictionary object

And don’t forget that you can manually browse the Datastore using your App Engine dashboard and see how such entities are actually stored.

I wrote about performance in that previous post, but honestly, I didn’t have the time to measure it, so if you guys do feel free to leave your benchmark results in the comments section. Some graphs would be cool too. Oh and thanks for retweeting!

Getting Cached Favicons From Google's User Content

I don’t know how legit this is but I’ve discovered a way of getting all the favicons you want in a sweet PNG format from Google’s server which seem to serve from cache. All you have to do is use the following format in your image src attribute:

http://s2.googleusercontent.com/s2/favicons?domain=DOMAIN_NAME

Where DOMAIN_NAME is the domain you’d like to receive your cached icon for. In case a domain doesn’t have a favicon or is unreachable, Google will return a slick globe icon. It seems that those servers are used for caching favicons for Google’s software such as Google Reader, Chrome and others. Once again, use this at your own risk ;)

I was thinking of a small API that would redirect to Twitter avatars (images) in a similar way by their usernames, so you could use picture sources like example.org/kovshenin.png to get my Twitter avatar. Do you guys think that would be of any use to anybody? Cheers!

Google Unveils Nexus S and Android Gingerbread

I couldn’t go without posting this great video of the new Nexus S phone from Google. Boy I love Google’s video marketing! And Nexus? Ah, I’m still excited about Nexus One and now bam! The new generation Nexus S phone is out, and Gingebread! Holy shmoly, Android’s taking over the world ;)

http://www.youtube.com/watch?v=lxUXulxE5o0

Read more at the official Google Mobile blog: Introducing Nexus S with Gingerbread

App Engine: Python Objects in the Google Datastore

Howdy! Hope you’re all good and getting ready for the upcoming holidays! I’ve been writing Google App Engine code last month more than ever before. I finally got to a file and data structure that I myself can understand. Hopefully we’re all aware that the Google Datastore is not relational and that most of the frequently used data types are supported: StringProperty, IntegerProperty, ListProperty, EmailProperty and many others. But are those enough? Hopefully yes, but this post introduces a new property type I’ve written mainly for myself: the ObjectProperty.

Have you ever thought of storing Python dictionaries or nested lists in the Google Datastore? What about custom objects with their attributes? I’ve gone a little bit further – ObjectProperty lets you store any Python object inside the Datastore. As you might know, everything in Python is an object, lists are objects, dictionaries are objects, functions are objects, classes are objects, objects are objects (duh!).

ObjectProperty uses the Python object serialization module called pickle (and yes, it’s available in Python 2.5). It “pickles” the input values before storing them in the Datastore, then “unpickles” them upon retrieval. This is very similar to JSON, but hey, JSON can’t store functions ;)

The code is fairly simple for now and works out of the box for me. I did make some human error assumptions hence the exception handling, but to be honest, there were no tests. It’s still at the idea stage so test cases will be built later on, which means that for now you should use this carefully, at your own will, provided as is, without any guarantees (obviously). Here’s the ObjectProperty class:

from google.appengine.ext import db
import pickle

# Use this property to store objects.
class ObjectProperty(db.BlobProperty):
	def validate(self, value):
		try:
			result = pickle.dumps(value)
			return value
		except pickle.PicklingError, e:
			return super(ObjectProperty, self).validate(value)

	def get_value_for_datastore(self, model_instance):
		result = super(ObjectProperty, self).get_value_for_datastore(model_instance)
		result = pickle.dumps(result)
		return db.Blob(result)

	def make_value_from_datastore(self, value):
		try:
			value = pickle.loads(str(value))
		except:
			pass
		return super(ObjectProperty, self).make_value_from_datastore(value)

So as you see, I’ve overridden a few methods which the App Engine docs suggests to create new properties. I used the pickle module to dump and load objects to and from the Datastore, as well as gave a try to pickle an object during validation. If that part is time-consuming for you (perhaps you’re pickling large objects) you might want to simply return value.

A quick usage example

Here’s a fairly simple usage example, should be quite easy to follow. You can try it out using App Engine’s interactive console:

class MyEntity(db.Model):
	name = db.StringProperty()
	obj = ObjectProperty() # Kudos

# Let's create an entity
my_object = { 'string': 'Hello World', 'integer': 51, 'list': [1, 2, 3], 'dict': { 'a': 1, 'b': 2, 'c': [4,5,6] } }
entity = MyEntity(name="Konstantin", obj=my_object)
entity.put() # Saves the entity to the datastore

# Then retrieve that value from the datastore
entities = MyEntity.all()
entities = entities.fetch(10)
for entity in entities:
	print "%s: %s" % (entity.name, entity.obj)

# Should output: Konstantin: { 'integer': 51, 'list': [1, 2, 3], 'string': 'Hello World', 'dict': { 'a': 1, 'c': [4, 5, 6], 'b': 2 } }

Voila. And here’s how it looks like in the datastore:

(dp0 S'integer' p1 I51 sS'list' p2 (lp3 I1 aI2 aI3 asS'string' p4 S'Hello ...

Now please note that such fields are not indexed, nor applicable for GQL queries. This means that you cannot lookup an entity whose “list called c inside the obj dictionary contains the number 5”, although that would be quite nice but definitely a bottleneck in performance (we’re using Google App Engine and the Google Datastore for performance after all).

Where can this be used?

It is up to you where to use such a property, but I can think of many. For instance, an entity called Account with some standard set of fields for typical user accounts. Now let’s tie social logins to such accounts – Facebook, Twitter, LinkedIn. Each service uses its own authentication method, for instance Twitter uses OAuth. Now, OAuth works with tokens, request tokens and access tokens, so define a twitter property as an ObjectProperty and store all the tokens in a simple Python dictionary. This works because you’ll never lookup tokens without looking up the Account first, and once you did look up the user account, your tokens are already unpickled and available. Magic, isn’t it?

As I already mentioned, this is still at the idea stage and with some feedback from you guys, I can hopefully decide whether to carry on or leave it as a private snippet for myself. Thanks for reading!

Update: I was concerned about not being able to maintain pickled objects in the Datastore admin, but that db.Blob() function solved the problems, so the values are now not editable through the admin. You might also want to check out JSON Objects for the Google Datastore and a comparison of the two in a post called Pickle vs JSON — Which is Faster?

Installing Python 2.5 on Ubuntu Linux 10.10

If you’ve been working on App Engine and you’ve noticed that some stuff works on your development server but not on your production, it may be related to the different versions of Python. Latest linux builds including Ubuntu 10.04 and 10.10 come with Python 2.6 pre-installed, but Google App Engine still runs Python 2.5 (an issue has been created to add Python 2.6 support, make sure you vote that up).

Their roadmap mentions nothing about upgrading. So in order to make your development server look more like your production servers, you’ll have to get Python 2.5, which is not that trivial at first.

So, Felix Krull has published an archive of new and old Python packages, so let’s use that source to get Python 2.5 running on a new Ubuntu box:

sudo add-apt-repository ppa:fkrull/deadsnakes
sudo apt-get update
sudo apt-get install python2.5

Yup, that was easy! Let’s now see if both Python 2.5 and Python 2.6 are available:

$ python2.5
Python 2.5.5 (r255:77872, Sep 14 2010, 15:51:01)

$ python
Python 2.6.6 (r266:84292, Sep 15 2010, 16:22:56)

All done! Oh and don’t forget to launch your App Engine development server using python2.5 (installing it is not enough):

$ python2.5 dev_appserver.py .

As a bonus to this post, I’d like to share with you my way of working with App Engine, not in terms of code, but in terms of libraries organization. If you’re writing code for App Engine you’re probably working on more than one project at a time, hence you’ll need to use the SDK more than once.

So instead of copying it, replacing Python packages, etc, simply move the google_appengine folder to /usr/share and in every App Engine project create a symbolic link called .gae that points to that location. The SDK will automatically locate all the Google libraries and the development server is easy to launch:

$ ln -s /usr/share/google_appengine/ .gae
$ python2.5 .gae/dev_appserver.py .

Don’t forget the dot at the end, since it tells the SDK which project to launch. And make sure you don’t push the .gae directory to your source control ;) Happy coding!

Foller.me Has Got a New Home: Google App Engine

That’s right, Google App Engine! For those of you who don’t remember, Foller.me is a Twitter app I wrote back in 2009. Honestly, I never realized back then that performance is an issue until the day I got featured on Mashable. More about it in a previous post.

If you’re not familiar with Foller.me, I’ll explain it here in a couple of words:

Foller.me is a web application based on the Twitter API, used to gather Twitter analytics from any profile – in seconds! Foller.me scans your Twitter profile, parses your latest tweets and followers. Tweets are separated into topics, mentions and hashtags. Followers are mapped out on a Google Map.

This weekend I made a choice I thought I never will – I’m moving Foller.me over to Google App Engine. It was all after running a couple of experiments in my Juice Toolkit project (note that there’s a GAE branch).

I realized that it wouldn’t take me too much time to rewrite Foller.me using the new language I’m learning – Python. And I was right, the first few tests were up and running on Saturday, and there’s now a live preview including more fixes and updates on App Spot. The clouds seem to work fine, geography is even better than I expected (with a few more updates) and the interface got a few extra buttons!

More over, the new version of Foller.me has been moved over to GitHub which means that it is now an open source project! Anybody can dive into the code, suggest a few patches or updates. Contributors are always welcome ;) Also, if there’s anybody interested in the PHP code that runs on the old version, let me know, I’ll be more than glad to share it.

Now, for the bad news – I’m closing down the Foller.me blog, since there’s not time to post, and not that much I could write about. I hope that the announcements, commits and wikis on GitHub and Twitter would be enough. The API will be temporarily shut down during the move and a few weeks after, but I’ll bring that to life, perhaps with a few more fixes. The domain will probably move to a www mirror, since App Engine doesn’t allow naked domains at this stage (see this issue).

So yeah, Foller.me will live, donations are always welcome. The app will remain free of charge and so will the API. But there’s a plan to launch a premium service on Foller.me – detailed profile analytics. I’m able to give you even more statistics and analytics on your Twitter profile, but of course not instantly – my rough calculations are 1-2 days. This will generate a very sophisticated PDF report about your profile, including much more information about your followers, about people you’re following, their relations, lists, graphs and charts, and even more geographical data.

All this is still in the stage of an idea or a concept, lacking a business plan. So my question to you right now is – would you be interested in such a service? And if yes, how much money are you willing to pay for one report? Please answer in the comments section. Everybody who answered will get their first reports free of charge (if this is ever implemented).

Video: Google Developer Day Moscow 2010

Hurray, the video’s out! Damn I love when Google posts videos about their events, they’re always so exciting! Didn’t find myself anywhere in the shots, but I did find some of my friends. Not to keep you waiting any longer:

Never knew barcode readers are able to parse data from an iPhone screen, that was amazing. For those of you who missed out my short review of the event: Google Developer Day Moscow 2010