Welcome

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

What next?
1. Bookmark us with del.icio.us or Digg Us!
2. Subscribe to this site's RSS feed
3. Browse the site.
4. Post your own code snippets to the site!

Search open Apache directory listings

Lets face it google is a P2P network!

-inurl:htm -inurl:html intitle:"index of" "Last modified" (pdf|chm) "linux"

Download free ebooks with wget

Mask the user agent as firefox, recursively download 2 levels deep from a span host with a maximum of 1 redirection, use random wait time and dump all pdf files to myBooksFolder without creating any other directories. Host will have no way of knowing that this is a grabber script.

wget -erobots=off --user-agent="Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.3) Gecko/2008092416 Firefox/3.0.3" -H -r -l2 --max-redirect=1 -w 5 --random-wait -PmyBooksFolder -nd --no-parent -A.pdf http://amazoo.com

wget tips

Download entire contents from website
wget -mk -p -N "http://www.yahoo.com"

-m mirror (download all folder on webpage recursively)
-k convert links for local browsing
-w20 wait 20 seconds between retrievals
-np no-parent
-nd no-directory, saves all files under one root directory
-p download all page requisites
-N download newest files only since last update.

Resume download large files
wget -c --output-document=file.zip "http://www.hostname.com/files/testfile1"

-c continue

Download by file type
downloads all avi and ignores everything else.
wget -m -nd -A.avi -erobots=off -i urls.txt

-A acclist comma-separated lists of file names suffixes
-R rejlist
-i input file with URLs
-erobots=off ignores robots.txt from the webserver


Check web site header information ie(type of OS)
wget -S yahoo.com




Find and replace in bash

Find all php files in current directory and replace every instance of SEARCH

find . -iname "*.php" -exec perl -pi -e 's/[SEARCH]>/[REPLACE]/g' {} \;

Convert PAL to NTSC



for F in *.VOB
do
   ffmpeg -i $F -target ntsc-dvd -s 720x480 -r 29.970 new-$F
done

how to implement black hole attack in qualnet

// description of your code here

// insert code here..

Strip html in ruby

def h(string)
  string.gsub(/<\/?[^>]*>/, "")
end

Convert flash video to divx with mencoder

Convert exported flash video (.flv) content into a more standardized codec such as divx or xvid. You can use this to batch convert YouTube videos and watch them on your DVDplayer.

#!/bin/sh

if [ -z "$1" ]; then
  echo "Usage: $0 {-divx|-xvid} list_of_flv_files"
  exit 1
fi

# video encoding bit rate
V_BITRATE=1000

while [ "$1" ]; do
  case "$1" in
    -divx)
      MENC_OPTS="-ovc lavc -lavcopts \
        vcodec=mpeg4:vbitrate=$V_BITRATE:mbd=2:v4mv:autoaspect"
      ;;
    -xvid)
      MENC_OPTS="-ovc xvid -xvidencopts bitrate=$V_BITRATE:autoaspect"
      ;;
    *)
      if file "$1" | grep -q "Macromedia Flash Video"; then
        mencoder "$1" $MENC_OPTS -vf pp=lb -oac mp3lame \
          -lameopts fast:preset=standard -o \
          "`basename $1 .flv`.avi"
      else
        echo "$1 is not Flash Video. Skipping"
      fi
      ;;
  esac
  shift
done

Parses bookmarks and download YouTube video files

Parses your exported bookmarks to generate a clean list of http lines and passes it on to clive to try to download the video file from various sites.

sed 's+href="\([^"]*\)"+\n\1\n+g' bookmarks.html | grep '^http' |clive

Remove all files in a dir

import os

dir_path = "test"

def create_test_files():
    child_dir_path = os.path.join(dir_path, "child_dir")
    os.mkdir(dir_path)
    os.mkdir(child_dir_path)
    open(os.path.join(child_dir_path, "test_file.txt"), 'w').close()
    open(os.path.join(dir_path, "test_file.txt"), 'w').close()
create_test_files()

for root, dirs, files in os.walk(dir_path, topdown=False):
    for name in files:
        os.remove(os.path.join(root, name))
    for name in dirs:
        os.rmdir(os.path.join(root, name))
os.rmdir(dir_path)