2007-03-31

Shell exercices

Wanna convert a pdf to images and create a gallery. Well, I had some actions with that and I did my way through the GNU, and other, utilities.

1. First
You need to convert the PDF document to images. Not so easy, there is pdftohtml but it doesn't generate images, instead it extracts the text and images from the document. However there is ImageMagick, a great project, which provides convert. So I ran a convert file.pdf file.png but I couldn't figure out how to preselect the size to get bigger resolutions. After that I have opened file.pdf with xpdf and I took screenshots with scrot from the xpdf window.

2. Crop images
I cropped the images to remove the borders and toolbars with for image in *.png ; do convert $image -crop 150x150+30+20 ; done where I selected the geometry of the crop size in GIMP within the dialog of the crop tool.

3. Rename files
Next I figured out how to rename files, and how to bulk rename (Thunar -B) them better.

4. Finally
I have found photon --thumbsize=50x50 --sizelist=0 -o photon ../images which creates nice web galleries. With javascript, you can view the presentation as in a pdf reader, by pressing space to switch pages.

However there is a bug, fortunately this blog entry will remember me to file it upstream. My images are png files, and it creates jpeg files. The original files are copied verbatim without modifications, but the html files contains source images only with .jpg extensions. Bad, because I have png files...

5. A last one before to go
for html in *.html ; do cat $html | sed -r 's,(original)/([0-9]+)-(projet-tuteure)\.jpg,\1/\2-\3.png,' > $html ; done

;-)

You can see the result of photon if you are interested.

Edit: Just in case I forget about it and would need it again... photon --thumbsize=105x105 --display-lines=4 --sizelist=0 --resize-quality-low=95 --img-bgcolor=#ffffff --body-bgcolor=#2e3436 -k steel -o photon ../images && cd photon && for html in *.html ; do cat $html | sed -r 's,(original)/([0-9]+)-(projet-tuteure)\.jpg,\1/\2-\3.png,' > $html ; done && cd ..

2007-03-14

Wanna download a YouTube video?

If you want to download a YouTube video it is really easy with the shell. All you have to do is to pick up the video_id in the URL and feed it to the script. It will then fetch the valid URL to download the flv video.

But that's not why I want to blog about it because there are already scripts to download YouTube videos. The reason I'm blogging is because it is a one-liner.

wget "http://www.youtube.com/get_video?`wget -q -O - "http://youtube.com/watch?v=$1" | egrep -m1 -o 'video_id=[^&]+&l=[0-9]+&t=[^"&]+'`" -O $1.flv

Note: $1 is the video_id parameter sent to the script.

Download the script.

2007-03-13

Random MAC address

I just thought I had need to blog about that, a script to generate a random MAC address. It is based on $RANDOM from any shell and works reliably.

#! /bin/sh
# Generates a random mac address
echo $RANDOM | openssl md5 | sed 's/\(..\)/\1:/g' | cut -b0-17

Cheers!

2007-02-26

Set the proxy for GNOME

I like surfing with Epiphany over the bloated Firefox but it doesn't provide a setting dialog for the proxy. It is configured system wide with gconf. A nice editor to have a quick look at the settings is gconf-editor and once you know what to set you can use the command line tool gconftool-2.

Here is a script to switch the proxy on/off with an optional host and port.

#! /bin/sh
# Switches the GNOME proxy on/off and
# can take an optional proxy host+port

usage ()
{
cat << CAT
Usage: `basename $0` on|off [host [port]]
CAT
exit 1
}

[ $# -lt 1 ] && usage

case "$1" in
"on")
mode="manual"
;;
"off")
mode="none"
;;
*)
usage
;;
esac

gconftool-2 -t string -s /system/proxy/mode "$mode"
[ ! -z "$2" ] && \
gconftool-2 -t string -s /system/http_proxy/host "$2"
[ ! -z "$3" ] && \
gconftool-2 -t int -s /system/http_proxy/port "$3"

As usual you can follow the changes at mykey57.free.fr/pub/misc/bin/.
Enjoy!

2007-02-24

MPD repeat track

I have written a Shell script to repeat the current track of MPD. The daemon doesn't provide this feature because it isn't seen as necessary, however I love (sometimes) to stick with the same song. Most people will suggest you to run mpc crop && mpc repeat on && mpc play ^_^, but that kills you playlist and I really want to avoid it.

There are two scripts, one to repeat the track when it has to be done, and a daemon which restarts the repeat process if the user changes the current track. Note: the daemon doesn't take care if you seek in the track, so... if you had like to repeat the same track and seek into it just hack the script and why not leave a comment with your modifications.

The repeat script

#!/bin/sh
# Repeat the same track

function repeat ()
{
min=`mpc|head -2|tail -1|awk '{print $3}'|cut -d: -f1`
sec=`mpc|head -2|tail -1|awk '{print $3}'|cut -d: -f2`
tmin=`mpc --format %time%|head -1|cut -d: -f1`
tsec=`mpc --format %time%|head -1|cut -d: -f2`
sleep $(( ($tmin*60+$tsec) - ($min*60+$sec) )) && mpc prev && repeat
}

repeat


The daemon

#!/bin/sh
# Daemon which takes care of restarting the repeat process
# if the user changes the current track

function restart ()
{
killall mpd-repeat-track.sh
mpd-repeat-track.sh&
}

restart

while sleep 2
do
file=/tmp/mpd-repeat-track.sh
current=`mpc|head -1`
last=`cat $file`
echo $current > $file
[ x"$current" != x"$last" ] && restart
done


Now run mpd-repeat-trackd.sh and it will just work<tm>.

You can follow the changes here:

Old links:

2007-02-07

Deactivate PHP for a container

Since I have set up a File-center on my web server, I never thought about deactivating PHP for the public folder. Now it is done and it is a one line directive. Run "echo SetHandler send-as-is > /to/your/root/public/folder/.htaccess".