Never been to DZone Snippets before?

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

About this user

Darren Paul Griffith http://madphilosopher.ca/

« Newer Snippets
Older Snippets »
Showing 1-6 of 6 total  RSS 

Python CGI script to display client's IP address

When run as a cgi script, this will print the client's IP address.

import cgi
import os

print "Content-type: text/html"
print ""

print cgi.escape(os.environ["REMOTE_ADDR"])

sox audio editor settings for companding voice

I used the following sox audio editor settings to do dynamic compression on voice.

sox in.wav out.wav lowpass 4000 compand 0.1,0.3 -60,-60,-30,-15,-20,-12,-4,-8,-2,-7 -2

Create a single JPG gallery from an image archive

The following comes from Jason Scott and is posted here with permission under the Creative Commons Attribution-ShareAlike License.

The script expands a zip or rar archive of images, makes thumbnails, and compiles the thumbnails into a single JPG to represent what's in the archive. Requires ImageMagick.

Code:
#!/bin/sh
# GALLERATE: Turn a zip of images into a gallery.
# From Jason Scott. http://ascii.textfiles.com/archives/000137.html
if [ -f "$1" ]
   then
   rm -rf .galleryworld
   echo "[%] Preparting to squat out $1...."
   mkdir .galleryworld
   cd .galleryworld
   unzip -j "../$1"
   unrar e -ep "../$1"
   echo "[%] WHY DOES IT HURT!!!!"
   montage +frame +shadow +label -tile 7 *.JPG *.GIF *.gif *.jpg *.bmp *.png *.PNG *.BMP "../$1.jpg"
   cd ..
   rm -rf .galleryworld
   ls -l "$1.jpg"
  else
  echo "No such file, assmaster."
fi


Usage:
GALLERATE [file]

where [file] is the zip or rar archive of images to be processed.

Filter all URLs from standard input

Code:

Put the following in a file named url_scrape.py.
#!/usr/bin/env python
'''Prints a list of URLs that are found in standard input.

It will only find URLs between quotes ("" or '') and starting with http://
'''

import re
import sys

# Pattern for fully-qualified URLs:
url_pattern = re.compile('''["']http://[^+]*?['"]''')

# build list of all URLs found in standard input
s = sys.stdin.read()
all = url_pattern.findall(s)

# output all the URLs
for i in all:
    print i.strip('"').strip("'")


Example Usage:
wget -O - http://madphilosopher.ca/ | ./url_scrape.py | sort | uniq

Scan the FreeBSD ports collection and print one-line summary for each port

The FreeBSD ports collection is a skelton of installable software on a local FreeBSD machine. This small utility lets you scan the collection by category, to find out what software is available in that category. Call the script portinfo.py.

A deeper explanation of the script can be found here.

"""Scans a directory under /usr/ports/ and gives the portname and comment for each port found."""

import os
import os.path
import sys

def findmakefiles(curdir):
    '''goes one level deep under curdir to find and act on the Makefiles.'''

    for name in os.listdir(curdir):
        pathname =  os.path.join(curdir,  name, 'Makefile')  # COMMENTs are found in the Makefiles
        if os.path.isfile(pathname):
            processmakefile(name, pathname)

def processmakefile(name, pathname):
    '''grabs the port's COMMENT from the Makefile.'''
    f = open(pathname)
    data = f.readlines()
    for line in data:
        if line[0:7] == 'COMMENT':
            try:
                comment = line.strip().split('\t')[1]   # most COMMENTs are tab delimited
            except:
                comment = line.strip()                  # some are not, so just print the whole line
            print "%-15s  %s" % (name, comment)

    f.close()

def usage():
    print 'Usage: portinfo.py /usr/ports/[category]'
    sys.exit(1)


if __name__ == '__main__':

    # get directory from command line
    args = sys.argv[1:]
    if len(args) == 1:
        curdir = args[0]
    else:
        usage()
    
##    curdir = '/usr/ports/security/'
 
    # scan this directory   
    if os.path.isdir(curdir):
        findmakefiles(curdir)
    else:
        sys.stdout = sys.stderr
        print "'%s' is not a valid directory." % curdir
        usage()

Shell script to look up words at dictionary.com from the command line

The following will look up a word on dictionary.com from the command line, where $1 is the word you want to look up, entered in as a parameter on the command line. (Lynx is a text-based browser that handles the html-to-text conversion for us.)

#!/bin/sh 
lynx -dump -nolist -pseudo_inlines                \
  'http://dictionary.reference.com/search?q='$1'&r=67'  \
  | tail -n +13 | less -r
« Newer Snippets
Older Snippets »
Showing 1-6 of 6 total  RSS