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!




Friday 11 January 2013

Automating torrent downloads

I'm a big fan of the Dakar Rally. This year's Dakar is particulary interesting to me, because, not only my favourite rider Cyril Despres is looking to an easy win now that his long time rival Marc Coma didn't start the rally due to a shoulder injury, but also because after a looong reign of KTM bikes, YAMAHA IS LEADING! (YAMAHA is my favourite brand of everything.) Since I don't have Eurosport to watch the Dakar on TV, I'm "forced" to download torrents of the coverage. This means checking The Pirate Bay all the time for new torrents and downloading them as soon as they appear. This doesn't sound too difficult. But what if you're not home? Or what if you're doing something else more important? Or what if you just can't be bothered? What then? What do you do?

You write a shell script.

Shouldn't be too difficult. This way, you can put it in CRON and it will do all the checking and downloading for you. Without stalling any further, let's get to the business.

The result page on The Pirate Bay already contains magnet links for every result. That's good. We can just strip these links from curl output and feed them to KTorrent. This is how it happens:

The magnet links are extracted and written to magnets.txt.tmp. They look like this:

magnet:?xt=urn:btih:8854f42ef801cb9c0f69caea1ca51a0a6c77c815

These are then compared to a list of existing magnets, to skip over the ones that we're already downloading. If a new magnet link is found, it is fed to KTorrent and written to the list of existing magnets for future reference.

Put that in CRON to run every 15 minutes and redirect its output to a file for debugging, like so:

This is it, more or less. If all goes well, you should have new torrents downloading and a growing list of magnets. And most important of all, you will have all the latest Dakar coverage at no fuss at all, and you'll help other fans as well by seeding torrents early. Can't stop smiling!

Update

Later I learned you can use KTorrent RSS plugin with filters to the same effect. Well, not exactly same effect. I set up the RSS feed and a filter and got exactly one successful start of a torrent. The very next one failed. "Ambitious, but rubbish." (Clarkson)

But my script still works well!

Saturday 5 January 2013

External display hotplug (on linux, with xrandr)

Every morning when I would come to the office, I would place my laptop on the desk and connect the power, network cable and the external display. And every time I'd have to go through the same external display configuration dialog. And that's just annoying. So I decided to automate. Isn't that a thrilling word? Automate.

A friend helped me write a shell script to enable or disable a twin display configuration using xrandr, and I set it up to be triggered by UDEV when an external display is hotplugged on the VGA port.

This is the script:

The two things you will probably want to change are the constants EXTERNAL_POSITION and BUILTIN_BASE. If you want the external display to show a mirror of your primary display, use setting EXTERNAL_POSITION="same-as".

I placed the script in /usr/local/lib/ and set up an UDEV rule that triggers it when an external display is connected.

This is my UDEV rule, in /etc/udev/rules.d/:

When you add the UDEV rule, you must reload rules for the changes to take effect. You do it by saying

$ sudo udevadm control --reload-rules

And that's it. Enjoy!



Friday 4 January 2013

Disabling the optical drive tray button

I use a ThinkPad T420. A great machine. The shit. I love it. But, there's a tiny annoyance. These laptops' optical drives have a protruding eject button, that's very easy to press accidentally when you grab the laptop to move it, or even just bump against it when moving your mouse. It happens quite often, too often in fact. Imagine your optical tray popping open every now and again at the most unexpected moments. Now this is really annoying. Something has to be done.

The easiest way to disable the eject button is to use the eject utility (on linux, of course). Simply saying

$ eject

will - eject the tray, you guessed it. Using it with the -i option however, will disable or enable the eject button, depending on the option argument.

Usage:

$ eject -i on

disables the device button, and

$ eject -i off

enables it. If on and off aren't recognised as valid arguments, use 0 and 1.

If I recall correctly, this worked flawlessly on openSUSE 11.4. When I switched to Chakra Linux, it didn't anymore. After extensive research I found there was an UDEV rule overriding (or working around, shall I say) the eject utility. What the solution took, was simply commenting out the conflicting UDEV rule. In my case the rule was in /lib/udev/rules.d/60-cdrom_id.rules.

I commented out the line


and reloaded UDEV rules with

$ sudo udevadm control --reload-rules.

It worked. It also works on Kubuntu 12.04 LTS, which I'm currently using, after getting too annoyed with Chakra's updates constantly breaking my system.

To lock the tray automatically (magical word) on system boot, put eject -i on in one of the startup scripts; a good place would be in /etc/rc.local. This way your optical drive will be locked on startup, and if/when you want to use it, you can unlock it saying $ eject -i off.

There you go. Another annoyance eliminated.