Global Hot Keys for Songbird in Linux

First, let me preface this by saying I used to love Amarok, but I feel Amarok 2 is just a bit too far off par for me to continue using.  It all really came to a head when I was listening to a 4-disc set, which all had the same Album name, and I couldn’t sort them by disc, or even by track number.  In the end, I broke down and installed Songbird on my Ubuntu desktop.  I should also mention I am a Gnome user, so these instructions may not work for you KDE users.

Now Songbird has come a long way since I tried it a while back, but it still has a lack of global shortcut keys on Linux.  A very helpful user of the Songbird community released an add-on which enables Command Line Support.

Now that I’ve set up my machine with Global Hot Keys, I’ll show you how to do so as well.  Here’s a step-by-step:

Note: If you are using the .deb installed from gotdeb, there is a wrapper script located at /usr/bin/songbird
Open this file as root in your favorite text editor. At the bottom of the short file, you’ll see:

./songbird

You need to add “$@” to the end of this line, so it should look like:

./songbird "$@"

This will allow the command line arguments to be passed to the actual binary.

· Run “gconf-editor” either from a terminal or the run dialog.  This will bring up an window to edit the gnome configuration options.

· In the menu list to the left, go to “apps” -> “metacity” -> “keybinding_commands”

· I used the first 3 commands as follows (double click on the line to edit):
- command_1: /usr/bin/songbird -pause
- command_2: /usr/bin/songbird -next
- command_3: /usr/bin/songbird -previous

· Once that is done, you will want to go back to your menu list on the left, and find the “global_keybindings” folder (“apps” -> “metacity” -> “global_keybindings”)

· Inside this folder, You should see run_command_1 through run_command_12.  You can now set you desired key bindings.  Personally, I set them similar to Amarok (since I’m used to them.)
- run_command_1: <Super>c
- run_command_2: <Super>b
- run_command_3: <Super>z

If you run into any problems during this process, or just want to keep in touch, you can contact me on twitter or leave a comment below.

Posted in linux, songbird, ubuntu at April 29th, 2009. No Comments.

Use wget to download large directories recursively

Have you ever wanted to recursively get a web directory? Or how about a web site?

If your a linux user, you can use wget (man wget for more info). Wget is a non-interactive network downloader. Mac users can try to get a binary <a href=”http://www.statusq.org/archives/2008/07/30/1954/”>here</a>.  Wget is a very powerful tool for many reasons, but it can be quite useful if you want to download multiple files from a site.

A few days ago, I complained that I didn’t have many fonts for use in designing, so a friend linked me to <a href=”http://www.fonts500.com”>fonts500.com</a>.  My first thought was “Wow, I want all of these fonts, but I don’t want to click all of those links, spread over 5 pages.” Enter wget!

wget -r --level=2 -A zip --quiet http://www.fonts500.com/

A little explanation of the command:

The -r is to be recursive.

The –level=2  sets the maximum depth (we don’t want to go too far off the page)

The “-A zip” sets the file inclusion parameter, which means we only want the zip files.

–quiet is just to squelch output, just a personal preference.

And lastly, the URL you want to use.

Hope it helps!

Posted in Bash, wget at April 15th, 2009. No Comments.

Clean URLs with mod_rewrite and .htaccess

Do you stop and think that the .php  file extension on your files looks ugly and unnecessary?  I do. When I’m creating a project, one of the first things I do is get rid of those file extensions.

Here’s a quick and easy way to do that using <a href=”http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html”>mod_rewrite</a> in your .htaccess file. (<i>Note: You will need to have the mod_rewrite module enabled, and the directory will need <a href=”http://httpd.apache.org/docs/2.0/mod/core.html#allowoverride”>AuthOverride</a> All on, in the apache configuration</i>)

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php

This code basically tells mod_rewrite that if the requested filename is not a file, and not a directory (the first 2 lines), then rewirte the URL as %{REQUEST_FILENAME}.php.  Take this code, create a new file in your web root, named .htaccess, paste the code in and save it. That’s all there is to it.

So if you have a file called “file.php” on your domain (http://example.com/), you should now be able to access it by going to http://example.com/file rather than http://example.com/file.php.

Posted in .htaccess, Apache, URLs, mod_rewrite at March 10th, 2009. No Comments.

MySQL Tip: Use unsigned int for larger values

Have you ever created an <b>int</b> field for a primary key? Or perhaps to store a numeric value? Chances are, you probably have, and if you don’t know what <b>unsigned</b> means, you are limiting the number of possible values that can be used in that field.

In MySQL, a field with the data type of int, without unsigned specified is capable of storing negative values. Since auto incremented primary keys will not use negative numbers for identifiers, you are limiting yourself to effectively half of the possible values of the int data type (between 0 and 2147483647).

That may seem like a fairly large number to most of you, but why take the risk?  If you specify your integer field as <b>unsigned</b>, you will double the number of values accepted by the field.

<b>Why?</b>  A <b>signed</b> integer, which in MySQL are all integers that are not specified as unsigned, will use the first of 32 bits as a marker for negative or positive.  This means that the possible values for are 2^31.  With an unsigned integer, the values are 2^32, or 0 to 4294967295.

The same exact rules apply for BIGINT (which is signed 2^63 and unsigned 2^64).

Posted in Datatypes, MySQL at March 7th, 2009. No Comments.

Easy Benchmarking class

When working to optimize either your own or someone else’s PHP code, it can be very useful to find out where the most time is being spent. This is especially true with long running cron scripts.

It’s pretty easy to do on a case-by-case basis, but after years of programming and writing the code to accomplish this, I decided it was easier to just create a Bench class.

The point of the Bench class is to allow me to add multiple data points throughout the code, and to show me the results upon completion of the script. I tried to make it as least obstructive as possible.

Please feel free to use and share this code as desired. If you run into any problems, or have any issues, let me know.

Bench.php Source Code:

<?php
/*
* author sgricci
* access public
* license gpl
* website http://deepcode.net
*/
class Bench {
	private $points = array();
	private $start;
 
	public function __construct() {
		$this->start = $this->utime();
	}
 
	private function utime()
	{
		$time = explode(" ", microtime());
		$usec = (double) $time[0];
		$sec = (double) $time[1];
		return ($sec + $usec);
	}
 
	public function pt($name = "")
	{
		if (!$name)
			$this->points[] = $this->utime();
		else
			$this->points[$name] = $this->utime();
	}
 
	public function __destruct()
	{
		$this->pt("total");
		foreach ($this->points as $name => $pt)
		{
			echo $name.": ".round(abs($this->start - $pt),4)."\n";
		}
	}
}
?>

Usage:

<?php
require_once "Bench.php";
$B = new Bench(); # Place at the beginning of the script.
// process some queries and then call
$B->pt("post-query process");
// process the data from the queries and then call
$B->pt("post-data process");
// go on with the rest of your script
?>

Posted in PHP, benchmarking, code, optimizing at February 27th, 2009. No Comments.