Monday, 22 July 2013

Download photos from a tumblr blog

This is a Ruby script that downloads the specified number of photos from a tumblr blog. It can be used as an automated backup tool if you put it in cron. It detects and skips the photos that have already been downloaded. By default, it's limited to downloading 50 most recent photos at a time, which is a limitation of the tumblr API. I recommend first using the script with the "fullbackup" option, which will download all the photos, and then putting the script in the daily cron with only the destination folder option, which will keep your backup fresh. Mind, if you post more than 50 photos on your tumblr daily it's a good idea to run the script several times a day. You get the idea.

You can get the script on GitHub.

I'm not the original author of the script, though I have contributed a patch. More importantly, it works nicely.

Enjoy!

Tuesday, 26 February 2013

Coffee, and why not

The other day I read about coffee and the effect it has on your brain. By "your brain" I mean my brain. And everyone else's. Coffee doesn't actually introduce any new energy to the system, if we exclude the added sugar. Instead it prevents adenosine from doing its job. Adenosine is a chemical in the brain that makes us feel tired. Caffeine from coffee fits into adenosine receptors in the brain, making no room for adenosine, and thus no room for tiredness.

But the brain is smart. (Well, obviously.) It wants to relax, so it creates new adenosine receptors to get some of that calming effect. So in turn you'll need more caffeine to fill these new receptors, in addition to the old ones. And the brain will still outsmart you by creating some new receptors. This happens quite fast, too. Apparently it only takes a week to become addicted to caffeine, when you get to the point that you drink coffee just to become your normal self. For a few hours. After six hours, when all the caffeine washes out your urinal stream, adenosine gets to occupy all the existing and newly created receptors in your brain. What does that mean? 

You're going down. Fast.

Read more about coffee and the brain on You Are Not So Smart.

Monday, 25 February 2013

Maximising battery lifetime

Every type of battery has its own charging and discharging regime that it prefers. Car batteries are happiest when they're cycling between full charge and full discharge all the time. This is why you can buy a "battery jogger" for your motorcycle battery, to keep it healthy during winter, which does just that: charges the battery to full capacity and then discharges it, and then charges it again.

Li-Ion batteries in our laptops however, are just the opposite. They don't like to be charged to the full or discharged completely flat, and are happiest if kept between approximately 40% to 80% capacity. The most harm you can do to a Li-Ion battery is to keep charging it to full capacity - all the time. And this is exactly what we're doing with our laptop batteries when we have the power plugged in most of the time. 

If you're a road warrior, there's nothing much you can do. You need the full charge. But most of us spend quite some time at the desk not using the battery at all. This is where we can make some optimisations to extend the lifetime of the battery, so it won't have to be replaced before its second birthday. 

This is easy on Windows, because the battery charge manager in Windows is implemented and it works. On Linux, there is no battery charge manager, or I haven't seen one. There are a few implementations of battery managers that reduce screen brightness and change CPU frequency and whatnot to extend battery life on a SINGLE charge. That's not what I'm talking about. I'm talking about extending battery lifetime as a whole. In years.

The concept is simple. If you know you won't need a full charge, don't charge full. Instead, keep the battery in its comfort zone. On most ThinkPads this is easily done by installing tp_smapi and writing the charge thresholds to appropriate locations:

$ echo 40 > /sys/devices/platform/smapi/BAT0/start_charge_thresh
$ echo 80 > /sys/devices/platform/smapi/BAT0/stop_charge_thresh

More info about tp_smapi, how to install it and set it up, and other stuff that it can do, on ArchWiki.

On some ThinkPads, for instance the T420, the lower charge threshold isn't supported by tp_smapi. A workaround is to forget about the lower threshold and only change the upper threshold, depending on the current battery charge. Here is an implementation of this concept along with a few other features, version 0.1:

tp_battery (on GitHub).

This is a bash executable script. To use it, put it into your $PATH and make it executable. It depends on tp_smapi so you'll need to install that, too. Refer to ArchWiki for details. You'll also have to grant the threshold file write access for your user, perhaps best if you claim ownership straight away:

$ chown igor /sys/devices/platform/smapi/BAT0/stop_charge_thresh

Put this line into your /etc/rc.local or similar location, so it will be there at every boot. If you want the script to govern your battery charge automatically (I assume you do), set up a cron job to run $ battery -c every minute. When you need full battery charge, simply say $ battery -f or $ battery --full. When you don't need full charge anymore, put the battery on vacation saying $ battery -u.

There is also a config file batteryrc that contains the lower and upper charge thresholds. I keep my battery script in $HOME/bin, and the config file in $HOME/bin/rc.d. Maybe not the best of practices, but who cares. The important bit is that the path to the config file is hardcoded in line 7 of the script. Check it and change it if necessary.

I found 70% as the lower threshold and 80% as the upper threshold values as most appropriate for my use case. Most of the time I'm on the power cable, and when I need the battery, it's only for an hour or two. I rarely even charge to full. And, I set the full battery threshold to 96% so I don't push my battery too hard when charging full, because those last few percent do a lot of damage to the chemistry and don't really contribute much autonomy time.

Long live the battery!

Saturday, 16 February 2013

Creating notifications in Plasma Desktop

Here's something very useful: if you ever wanted to trigger a Plasma Desktop notification from the shell, here's how you do it:

$ kdialog --title "Some meaningful title" --passivepopup "Whatever you want the notification to display." 10



I used this to notify me of any errors when automatic backup is run, because the backup application (written for Gnome) doesn't have any notifications built in.

The number 10 at the end of the command defines the popup duration in seconds. However, there must be a default maximum duration defined somewhere, because I couldn't get the popup to stay on for more than 30 seconds. Well, this should be enough anyway, because the notification does stay in the Notifications part of the system tray until you remove it by hand.

If you are trying to display a notification from a shell script, don't forget to  put

export DISPLAY=':0'

before the kdialog command, for obvious reasons.

The kdialog command is very flexible, and it supports a number of different modal dialogs. Check out the official tutorial here.

Wednesday, 30 January 2013

See where your disk space is being wasted

One piece of software I'm having tremendous fun with lately is Filelight. It displays a sunburst chart of folder and file sizes. It was originally written for KDE 3, but there is a KDE 4 version as well. Look it up in your distro's repos and reclaim your disk space!