2013-10-18

How to Youtube to mp3 on Ubuntu

sudo apt-get install youtube-dl libavcodec-extra-53
youtube-dl --extract-audio --audio-format mp3 -l http://www.youtube.com/watch?v=qzYAqscEUC8

2012-12-07

Swedish post tracking cron job

/usr/local/bin/post-check
#!/bin/bash

# Script arguments
# argument 1 - package number, i.e. 012345670001234567
# argument 2 - mail address for package status updates
nr=$1
to=$2

# Swedish post perma-link
link="http://server.logistik.posten.se/servlet/PacTrack"
link="$link?lang=GB&xslURL=/xsl/pactrack/standard.xsl&kolliid=$1"

# Script saves status updates into temp dir
cd /tmp/

# Send the mail
# argument 1 - subject
# argument 2 - message
function send_mail() {
 # arg1 subject
 echo "$2" | mail -s "$1" \
    -a "MIME-Version: 1.0" \
    -a "Content-Type: text/html" \
    "$to"
}

# Download package latest status
wget "$link" -q -O post-status.new
if [ $? -ne 0 ]; then
 error="Could not download package status."
 echo "$error Mail sent."
 send_mail "Package $nr status error" "$error"
 exit
fi
# Make sure both current and previous status file exists
touch post-status.new
touch post-status.old

# Check if there is a difference in two last status sizes
new_size=$(stat -c%s post-status.new)
old_size=$(stat -c%s post-status.old)
if [ $new_size -ne $old_size ]; then
 echo "Package status updated. Mail sent."
 send_mail "Package $nr status updated" "`cat post-status.new`"
fi

# Save the status
cp post-status.new post-status.old

crontab -e
*/15 * * * * /usr/local/bin/post-check 012345670001234567 your@mail.com

2012-08-10

Lua output redirection

While ago I was working on Lua based implementation of the scripting library. One of the requirements was a total control of output and error messages, to allow it's automatic handling. One of the issues I've faced is that some output was going to stdout even if there is redefined print Lua function.

I thought about two quick solutions:
- redirect standard streams;
- redefine printf and it's adjacent functions.

Since standard streams, both stdout and stderr, in my case were already redefined (for remote debugging purposes), I didn't had much to choose from.

Looking into Lua 5.1 sources, I've found that stdout was simply hard-coded in some places. During up-streaming this patch to Lua 5.2 surprisingly most of the hard-coded stream names went away, and it looks like it would be possible to use redefined print without losing anything. I haven't tested this though. Following just works for Lua 5.1.x and above.

2012-08-08

Manually adding missing apt public keys

When apt-get update starts complaining about missing public keys, it's always pain in the ass when you're behind proxy and gpg --recv-keys doesn't work. Following is the manual way of adding missing apt public keys:

1. Obtain public key via browser:
http://keyserver.ubuntu.com:11371/pks/lookup?op=get&search=0x[KEY]
2. Save the key.
3. Import the key file in System->Administration->Software Sources

2012-06-12

How-to add a permanent network interface alias on Ubuntu 10.04 and higher

1. Add network interface alias by adding following into /etc/network/interfaces:
# Alias interface
auto eth0:0
iface eth0:0 inet static
  address 192.168.1.1
  netmask 255.255.255.0

2. Create a new file and place it in /etc/network/if-up.d/:
#!/bin/bash

if [ "$IFACE" = "eth0" ]; then
        /sbin/ifup --force eth0:0
fi

3. Restart network via Network Manager or command line:
sudo /etc/init.d/networking restart

2011-12-16

Nine-patch Bitmaps on Android

Excerpt from new Android Training by Google:

Supporting different screen sizes usually means that your image resources must also be capable of adapting to different sizes. For example, a button background must fit whichever button shape it is applied to.

If you use simple images on components that can change size, you will quickly notice that the results are somewhat less than impressive, since the runtime will stretch or shrink your images uniformly. The solution is using nine-patch bitmaps, which are specially formatted PNG files that indicate which areas can and cannot be stretched.

Therefore, when designing bitmaps that will be used on components with variable size, always use nine-patches.

2011-11-08

Multiple gcc versions on Ubuntu machine

# let the beast in
su

# install the older gcc versions
apt-get install gcc-4.4
apt-get install g++-4.4

# configure
update-alternatives --remove-all gcc
update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.4 44
 --slave /usr/bin/g++ g++ /usr/bin/g++-4.4
 --slave /usr/bin/gcov gcov /usr/bin/gcov-4.4
update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.6 46
 --slave /usr/bin/g++ g++ /usr/bin/g++-4.6
 --slave /usr/bin/gcov gcov /usr/bin/gcov-4.6

# switch gcc
update-alternatives --config gcc

2011-09-26

git user configuration override

In case if it's required to have a different user configuration from the one stored in .gitconfig, there is two possible ways to achieve this:

Local (per git)
$ cd git-repo
$ git config user.name "Name Surname"
$ git config user.email "name.surname@domain.com"
Global

This method will override default or git's user configuration. Add following to .bashrc:
function gitswitch () {
    case "$1" in
    "custom")
        export GIT_AUTHOR_EMAIL="name.surname@domain.com"
        export GIT_AUTHOR_NAME="Name Surname"
        export GIT_COMMITTER_EMAIL="name.surname@domain.com"
        export GIT_COMMITTER_NAME="Name Surname"
        echo "Using custom configuration."
        ;;
    "default")
        unset GIT_AUTHOR_EMAIL
        unset GIT_AUTHOR_NAME
        unset GIT_COMMITTER_EMAIL
        unset GIT_COMMITTER_NAME
        echo "Using default configuration from .gitconfig."
        ;;
    *)
        echo "Bad argument - please specify 'custom' or 'default'."
        ;;
    esac
}
And then:
$ gitswitch custom
Using custom configuration.
$ gitswitch default
Using default configuration from .gitconfig.
$ gitswitch ?
Bad argument - please specify 'custom' or 'default'.

2011-08-29

To Go or not to Go?

Apparently Go language makes more and more community attention. Today made a 15 min try and was surprised how simple and fast you can get into it.
Surprisingly, there is bindings for GTK which allows to write GUI apps for both Linux and Windows, and one of the demos looks as simple as:
package main

import (
	"os"
	"github.com/mattn/go-gtk/gtk"
	"fmt"
)

func main() {
	gtk.Init(&os.Args)
	window := gtk.Window(gtk.GTK_WINDOW_TOPLEVEL)
	window.SetTitle("GTK Table")
	window.Connect("destroy", gtk.MainQuit)

	swin := gtk.ScrolledWindow(nil, nil)
	swin.SetPolicy(gtk.GTK_POLICY_AUTOMATIC, gtk.GTK_POLICY_AUTOMATIC)

	table := gtk.Table(5, 5, false)
	for y := uint(0); y < 5; y++ {
		for x := uint(0); x < 5; x++ {
			table.Attach(gtk.ButtonWithLabel(fmt.Sprintf("%02d:%02d", x, y)),
						 x, x+1, y, y+1, gtk.GTK_FILL, gtk.GTK_FILL, 5, 5)
		}
	}
	swin.AddWithViewPort(table)

	window.Add(swin)
	window.SetDefaultSize(200, 200)
	window.ShowAll()

	gtk.Main()
}

2011-07-29

First post, again

Finally decided to move my blog to blogspot. Hello World.