MW

Code-Snippets - page 2

Recent Posts

Recursive Wordcount (March 25, 2009)

Yes, I am a Linux user and I really appreciate the freedom I get by using either an awesome desktop environment or the command line - or both!

The function I’m going to present you gives you a good overview of how many words, lines and bytes files in a given folder have. I’m speaking about wc.

The bash script I wrote applies wc to every web file in a folder and every sub folder. Web files are:

  • *.php
  • *.html / *.htm
  • *.tpl
  • *.sql
  • *.js
  • *.css
  • .htaccess
  • files without an extension (e.g. README)

Furthermore you can exclude folders by using the -e parameter. I needed that feature to exclude scripts which are not written by me (see below). But because of that I have had to commit a sin: using eval

Recursive wc

    #!/bin/bash
     
    # usage
    # -s = search path
    # -e = excluded paths
     
    # examples:
    # current folder:    ./wc.sh
    # other folder:      ./wc.sh -s ../foobar/
    # exclude folder:    ./wc.sh -e "*/foobar/*"
    # exclude folder:    ./wc.sh -e "*/folder1/*" -e "*/folder2/*"
     
    # default params
    SEARCH_PATH="./"
    EXCLUDE=""
     
    # read command line params
    while getopts  "s:e:" PARAM
    do
        case "${PARAM}" in
                s)      SEARCH_PATH="$OPTARG";;
                e)      EXCLUDE=$EXCLUDE" -not -path \"$OPTARG\"";;
                esac
    done
     
    if [ "$EXCLUDE" = "" ]
    then
        find $SEARCH_PATH \
            -regextype posix-egrep \
            -type f \
            -regex ".*(\.(php|html?|tpl|css|sql|js))$" \
            -or -name ".htaccess" \
            | xargs wc
    else
        # evil eval
        eval 'find $SEARCH_PATH \
                -regextype posix-egrep \
                -type f \
                \( -regex ".*(\.(php|html|tpl|css|sql|js))$" \
                -or -name ".htaccess" \)\
                '$EXCLUDE' \
                | xargs wc'
    fi

continue reading...

Attack of the shell helpers (March 03, 2009)

Everyone who uses the command line regularly has a bunch of (at least for him) useful helper scripts. I now took the liberty to put mine on github for general consumption.

You can find them at: http://github.com/milianw/shell-helpers/tree/master

Some of these might be more useful than others, you decide :) Personally, I can’t live without the following:

apupgrade

a shortcut to update your Debian system with one command - no questions asked `openurl`

opens a given URL in an already opened browser instance or starts a new browser session. Not only one browser is checked. I use it because firefox is slow to start and konqueror is blazingly fast to start. But when firefox is already open I want to use that. `xerr`

shortcut for fast error checking in your Xorg log `clipboard`

makes KDE4 Klipper contents available on the CLI (read _and_ write access!) `debug`

shortcut to start a GDB session: debug APP APP_ARGS is all you have to do. Its basically the same as doing:

continue reading...

Download script for springerlink.com Ebooks (December 20, 2008)

After a long period of silence I present you the following bash script for downloading books from http://springerlink.com. This is not a way to circumvent their login mechanisms, you will need proper rights to download books. But many students in Germany get free access to those ebooks via their universities. I for example study at the FU Berlin and put the script in my Zedat home folder and start the download process via SSH from home. Afterwards I download the tarball to my home system.

Read on for the script.

Download the script (attached below), push it to your Zedat account, make it executable and run it. You’ll have to give it a link to a book-detail page like this one for example. Also take a look at the example call at the top of the script.

Requires bash, wget, iconv, egrep.

Note: Take a look at the comments, Faro has come up with an updated Bash script which properly handles ebooks which span multiple pages on SpringerLink and merges the pdf-files with pdftk. Thanks Faro!

continue reading...

mp3dump: Rip the audio stream of a Flash video to MP3 (November 26, 2008)

UPDATE : Also check out mp3dump part 2

On some undefined occasion you might want to dump the audio stream of a flash file. This is how you do it:

  1. Get the *.flv, e.g. visit youtube with Konqueror and check /tmp for a file like FlashV5SQLt - this is your FLV.
  2. Install some dependencies: ffmpeg is required, mp3info and ecasound are optional but strongly recommended.
  3. Download the script below (it’s attached!) and save it inside your $PATH, remember how you’ve called it!
  4. I’ve saved the script inside ~/.bin which is inside my $PATH so all I have to do now is running it:

mp3dump FlashV5SQLt "Foo Bar" "All Your Base Are Belong To Us"

This would rip the audio stream of FlashV5SQLt to a file named Foo Bar - All Your Base Are Belong To Us.mp3 - with emulated stereo sound and basic MP3 tags (artist and title). Coolio!

So what does this neat-o-bash-magic look like?

Note: The script is attached below, so no copy’n’paste is required (but optionally supported :-) )

    #!/bin/bash
    #
    # Simple script to rip the audio stream of a FLV to MP3
    #
    # usage:      mp3dump INPUT.flv ARTIST TITLE
    # example:       mp3dump FlashhxSjv3 "Foo Bar" "All your Base are Belong to Us"
    # Author:        Milian Wolff
    # depends on:  ffmpeg
    #   optional:    ecasound for stereo sound emulation
    #   optional:   mp3info to write title and artist mp3 tags
     
    if [[ "$(which ffmpeg)" == "" ]]; then
     echo -e "\033[41mInstall ffmpeg or this script wont work!\033[0m"
        exit
    fi
     
    if [[ "$1" == "" || "$2" == "" || "$3" == "" || "$4" != "" ]]; then
      echo "Usage: $(basename $0) INPUT.flv ARTIST TITLE"
      exit
    fi
     
    dest="$2 - $3.mp3"
    tmpfile="/tmp/$$-$dest"
     
    echo "Destination file is: $dest"
    echo
    echo
     
    echo "generating mp3 file..."
    echo
    ffmpeg -i "$1" -f mp3 -vn -acodec copy "$dest" 1>/dev/null
    echo
    echo -e " \033[32mdone\033[0m"
    echo
     
    if [[ "$(which ecasound)" == "" ]]; then
     echo "You can optionally install ecasound to simulate stereo sound"
      echo
    else
      echo -n "simulating stereo sound..."
     
        ecasound -d:1 -X -i "$dest" -etf:8 -o "$tmpfile" 1>/dev/null
        mv "$tmpfile" "$dest"
     
     echo -e " \033[32mdone\033[0m"
       echo
    fi
     
     
    if [[ "$(which mp3info)" == "" ]]; then
      echo "You can optionally install mp3info to write basic mp3 tags automatically"
      echo
    else
      echo -n "writing basic mp3 tags..."
     
     mp3info -a "$2" -t "$3" "$dest"
     
     echo -e " \033[32mdone\033[0m"
       echo
    fi
     
    echo "Have fun with »$dest«"

continue reading...

profile.class.php (June 24, 2008)

Every now and then I want to profile a given part of PHP code. For example I want to quickly check wether my current changeset to GeSHi works faster or is horribly slower. For a big change I’ll stick to Xdebug and KCachegrind. But for a quick overview? Overkill in my eyes.

Say hello to profile.class.php, a simple timer class for PHP5 which you can use to get an overview about where how much time is spent. This is in no way a scientific method nor should you take the results of a single run as a basis for you decisions.

I’ve set an emphasize on an easy API so you don’t have to pollute your code with arbitrary hoops and whistles.

UPDATE: You can find the current updated source in the SVN repo of GeSHi.

Simple example

This is a quick example on how you could use the class:

    <?php
    require 'profile.class.php'; // should be obvious ;-)
     
    // here might be uninteresting code
     
    profile::start('overall'); // start a timer and give it a name
                                       // we add this one to get a time
                                       // for the overall runtime
     
    // this is the code you want to profile
    profile::start('$_SERVER');
    $foo = count($_SERVER);
    profile::stop(); // stop the last active counter
     
    profile::start('$GLOBALS');
    $bar = count($GLOBALS);
    profile::stop();
     
     
    profile::stop(); // stop overall timer
    profile::print_results(profile::flush()); // print the results

continue reading...

mp3dump take two - better audio quality (April 08, 2008)

So just yesterday I’ve published a bash script which rips the audio stream of Flash Videos (*.flv) to mp3 format. It’s nice, fast and imo all-purpose. But I didn’t like the audio quality. Thus below you can find a second version of the script which is using mplayer and lame instead of ffmpeg. Usage and behaviour should be pretty much the same. Only the audio quality should be better on cost of a bit more computing.

Since it relies on the fact that the input *.flv video already uses MP3 encoding for its audio stream this might not work for every flash file! Youtube works just fine though. You can find the script after the break, it’s also attached below. For usage / installation / more information read the old article.

If you wonder why I reencode the audiodump with lame: The dumped mp3 file is considered by most (all?) audio players to be ~10x the length. A five minute video gets a 45 minute audio dumpfile. It plays fine but seeking is a mess. Reencoding fixes this. If you know an alternative which does not require reencoding or is generally faster - please drop a comment!

continue reading...

Patching Kirocker Music Display for volume control via mouse scrolling (March 28, 2008)

Kirocker is such a great application, it’s a pity the developer Sébastien Laoût has given up on it and is using Windows now!

Usually I only use the kicker applet, the full screen window is nice for parties though. But I didn’t like the way the applet handles scroll events: It seeks (if possible). Since I rarely seek when listening to music I’d rather have the behaviour of the Amarok tray icon: volume control!

I had a look inside the sources and found this part in src/coverdisplay.cpp:

    void CoverDisplay::wheelEvent(QWheelEvent *event)
    {
        if (areControlsShown()) {
          if (event->orientation() == Qt::Vertical) {
             PlayerInformation *infos = PlayerInformation::instance();
              if (infos->canSeek()) {
                 int deltaSeconds = 10 * (event->delta() > 0 ? 1 : -1);
                   m_infos->seekRelative(deltaSeconds);
                }
          } else {
               if (event->delta() > 0)
                  AmarokApi::volumeUp();
             else
                   AmarokApi::volumeDown();
           }
      }
    }

continue reading...

Reinstall Nvidia Driver (February 09, 2008)

For those of you, who use the original NVIDIA display driver and have to reinstall it with every kernel update: Here is a little bashscript for your convenience.

    #!/bin/bash
    sudo /etc/init.d/kdm stop
    sudo chvt 1
    sudo sh ~/Downloads/NVIDIA-Linux-x86-*.run --ui='none' -aNq
    sudo /etc/init.d/kdm start
    sudo chvt 7

Save it e.g. as ~/.bin/nv_reinst.sh, make it executable (chmod +x ~/.bin/nv_reinst.sh). Then put your NVIDIA driver (only the newest version please) into ~/Downloads/ (or change the path above). Now run your script.

Attention: Save all your work, since kdm will be stopped! You’ll lose all unsaved work!

continue reading...

open URL a in variable browser (July 22, 2007)

I was recently annoyed by how URLs were started in KDE: I used an antiquated firefox command to run an URL in a new tab (firefox-remote "OpenURL(%u, new-window)") which does the job quite well. But: If you have no running firefox instance this command will simply do nothing. Right - nothing! You’ll have to start firefox manually and click once again on the link (in konversation, kopete or wherever).

But the newer firefox versions (I don’t know when this feature was added) support the firefox -newtab %u command. This is all you need if you only use firefox. Configure KDE via kcontrol to use this command as a standard webbrowser.

This was still not enough for me though, as I regularly switch to konqueror or opera, because firefox is sometimes to slow for a quick browse. This is what I came up with:

open_url script

what it does

Via open_url it is possible to use one of the currently open browsers. If you for example have a running konqueror instance but no firefox it is more likely (in my point of view) to open a new URL in konqueror instead of launching a new firefox instance (which would take a long time).

continue reading...

open URL a in variable browser (July 22, 2007)

I was recently annoyed by how URLs were started in KDE: I used an antiquated firefox command to run an URL in a new tab (firefox-remote "OpenURL(%u, new-window)") which does the job quite well. But: If you have no running firefox instance this command will simply do nothing. Right - nothing! You’ll have to start firefox manually and click once again on the link (in konversation, kopete or wherever).

But the newer firefox versions (I don’t know when this feature was added) support the firefox -newtab %u command. This is all you need if you only use firefox. Configure KDE via kcontrol to use this command as a standard webbrowser.

This was still not enough for me though, as I regularly switch to konqueror or opera, because firefox is sometimes to slow for a quick browse. This is what I came up with:

open_url script

what it does

Via open_url it is possible to use one of the currently open browsers. If you for example have a running konqueror instance but no firefox it is more likely (in my point of view) to open a new URL in konqueror instead of launching a new firefox instance (which would take a long time).

continue reading...