Posts Tagged ‘HOW-TO’

Building a custom case for a handmade TV-B-Gone

Saturday, April 27th, 2013

I had a chance to visit MakerBar the other night, and tried building my own TV-B-Gone, using MakerBar’s own upgraded design.

It was the first time I’d soldered anything for a real project, and until I’d completed and tested it, I wasn’t sure if it would actually work.

But it did, and a big part of the reason why is that Zack Freedman, the designer of MakerBar’s upgraded circuit, not only took the time to write a detailed set of instructions but also waited patiently with us as we went through the process, and provided help as we needed.

My own functional TV-B-Gone, which I built myself

My own functional TV-B-Gone, which I built myself

Since the double-sided tape which was meant to mount the board on top of the battery case didn’t quite work, and I also didn’t like how the board was exposed, I went looking for a case I could use.

Hello, green tea mints

Hello, green tea mints

A empty box of mints was the perfect size, and it was easy to take apart. I needed some filler material, so I used the styrofoam container from one of my computers.

Almost a perfect fit

Almost a perfect fit

I wanted to be able to continue to use the styrofoam for my computer, though, so to preserve the structural integrity of the container, I planned two cuts, roughly a third of the width of the middle element.

Planning to cut out the bottom right and upper left rectangles

Planning to cut out the bottom right and upper left rectangles

One cut-out done

One cut-out done

Just as I was about to cut out the second piece, I remembered the concept of "single-piece flow" from The Lean Startup, and I stopped to check my first cut-out in the mint box.

One is enough!

One is enough!

It turned out that was all I needed, so not only did I not waste time cutting the second piece, but also I was able to keep the styrofoam more intact than if I’d cut the second piece.

The circuit board on top, battery case below

The circuit board on top, battery case below

A sectional view

A sectional view

I still needed something to stick the board to the styrofoam, and so I tried some mounting squares I had left over from another project.

Next, came the hardest part: cutting holes in the lid to accomodate the LEDs and circuit components.

I planned on cutting off this rectangle entirely, but making the two slits was hard enough, so I stopped there

I planned on cutting off this rectangle entirely, but making the two slits was hard enough, so I stopped there

Cut-outs for the top of the lid

Cut-outs for the top of the lid

Close enough

Close enough

Cutting the top of the lid, which was mostly clear plastic instead of metal, was much easier, and eventually I was able to fit the whole thing together.

Closing the lid, with a little gentle persuasion from a pair of pliers

Closing the lid, with a little gentle persuasion from a pair of pliers

I finished it by applying electrical tape around the edge, to make sure the lid would stay closed.

The completed case mod, nice and snug

The completed case mod, nice and snug

It came out quite nicely, and having the battery case underneath, gives the whole thing a nice sense of heft and control.

A live action shot

A live action shot

Ubuntu Server + Fluxbox on Mac OSX using VirtualBox

Sunday, March 31st, 2013

I like Mac OSX as a general computing environment, but I prefer Debian (and more recently Ubuntu) for development.

Instead of running my Mac in dual-boot mode, I tried using VirtualBox to run Ubuntu Desktop.

Unfortunately, the virtual image was unusable: even at two gigabytes of memory (the most allowed by the vm) it was painfully slow, and using the dash was basically impossible.

But since my development environment consists mostly of ROXTerm and emacs, I wanted to see how Ubuntu Server plus a lightweight window manager such as Fluxbox would work instead.

Fluxbox running like a champ on Ubuntu Server in VirtualBox

Fluxbox running like a champ on Ubuntu Server in VirtualBox

It’s been terrific so far, and the setup was simple:

  1. After downloading the Ubuntu Server iso file, I created a new virtual image and used the iso as the base media
  2. Once installed, I followed the instructions for setting up Fluxbox without a login manager:
    sudo apt-get install xorg fluxbox fluxconf
  3. Then, I started Fluxbox for the first time:
    startx
  4. For the Guest Additions installation, I installed these packages first:
    apt-get install dkms build-essential linux-headers-generic

    And then requested the guest additions from the VirtualBox menu: Devices -> Install Guest Additions which “loads” the guest additions virtual disk. Under gnome or kde the disk gets mounted automatically, but in Fluxbox I had to mount it myself before running the install script:

    sudo mount /dev/dvd /media/cdrom
    cd /media/cdrom
    sudo sh ./VBoxLinuxAdditions.run

Using webcams in ubuntu: fixing the audio

Monday, March 25th, 2013

I recently tried using my laptop’s webcam to record video for the first time, but ran into a problem with the audio.

The ubuntu community help page says:

With recent versions of Ubuntu (>= 12.10) you should use avconv instead of ffmpeg command:

avconv -f oss -i /dev/dsp -f video4linux2 -s 320x240 -i /dev/video0 out.mpg

Unfortunately, that gave me this error:

[oss @ 0xd5dbc0] /dev/dsp: No such file or directory
/dev/dsp: Input/output error

It turns out that the /dev/dsp device is part of the obsolete OSS sound API, which has since been replaced by the ALSA API.

In fact, the OSS API was removed from the ubuntu kernel in 2010, so it’s surprising to see such outdated advice on the ubuntu help page.

Buried deep in the avconv manpage are notes about how to use alsa.

First, I had to find the recognized cards and devices, which I got by using these two commands respectively:

$ cat /proc/asound/cards
$ cat /proc/asound/devices

In my case the “CARD,DEV,SUBDEV” values which follow the “hw:” prefix were “0,0,0″ and so the avconv command which did work was:

avconv -f alsa -i hw:0,0,0 -f video4linux2 -s 320x240 -i /dev/video0 out.mpg

Rediscovering LaTeX

Thursday, January 5th, 2012

I first used LaTeX while an intern at a very old-school software company that ran only unix workstations.

When I needed to write a letter (that had to be printed on paper and signed, for some bureaucratic task), I was told "try this".

At first, the idea of writing in markup, then compiling it to get final document seemed strange, but I quickly came to love using it. Pretty soon, anything that I used to do in Word I would do in LaTeX instead.

I got away from it entirely these last few years, as most things that used to require a printed letter or memo have succumbed to email, web forms, and the like.

But recently I had the need again, for a new project, and thought: why not?

The only difference now is that instead of printing to paper, I would be sending pdf files by email.

Fortunately, the Ghostscript ps2pdf utility makes that simple, and it was already installed on my computer.

Likewise, LaTeX itself was already installed and available, thanks to the TeX Live package.

The only remaining annoyance was all the commands I needed to run for each document:

$ latex test.tex
$ dvips test.dvi
$ ps2pdf test.ps

and, to clean-up all the intermediate files those commands generated:

$ rm test.aux test.dvi test.log test.ps

So I wrote this latex2pdf shell script:

#!/bin/sh

if [ $# -ne 1 ]
then
    echo "usage: latex2pdf.sh [file(.tex)]"
else
    # split $1 on / to get the path and filename
    path=`echo ${1%/*}`
    file=`echo ${1##*/}`
    if [ $path = $file ]
    then
        path=`pwd`
    fi

    # check if the file already has the .tex ext
    suffix=`echo $file | grep ".tex$" | wc -l`
    if [ $suffix -eq 0 ]
    then
        f=`echo "$file.tex"`
    else
        f=`echo "$file"`
    fi

    # define the filename base string w/o the .tex ext
    # (what the .aux, .dvi., .ps, .log files will be named)
    s=`echo "$f" | sed -e 's/\.tex$//'`

    # compile the .tex file and convert to pdf
    latex "$path/$f"
    dvips "$s.dvi"
    ps2pdf "$s.ps"
    rm -f "$s.aux"
    rm -f "$s.dvi"
    rm -f "$s.log"
    rm -f "$s.ps"
fi

Now, with a single command, I can build and view the result immediately:

$ ./latex2pdf.sh test.tex; xpdf test.pdf &

Who needs WYSIWYG?

Splitting and Extracting MPEG video files with MEncoder

Monday, January 2nd, 2012

One of the nice things about MythTV is that it lets me save any broadcast as an unencrypted, DRM-free mpeg file.

I recently found out how to use MEncoder to split and trim those mpeg files into single or multiple clips.

MEncoder is a good tool to use because it’s free (as in both freedom and beer), and runs on all major platforms (there are even pre-built binaries for Mac OSX).

MEncoder has two command line options, -ss and -endpos, which let you define the start or end position of the clip you want to extract.

Unfortunately, the default command doesn’t work with mpeg files.

The work-around is to convert the mpeg file to avi format first:

$ mencoder original.mpeg -ovc lavc -oac lavc -o original.avi

Then, create a copy starting or ending at a given point in time, defined as hour:minute:second using either the -ss or -endpos options.

For example, to extract a clip from the 17 minute 50 second mark to the 57 minute 47 second mark from a one-hour file, these two commands will do the trick:

$ mencoder -ss 00:17:50 -oac copy -ovc copy original.avi -o clip_start.avi
$ mencoder -endpos 00:39:57 -oac copy -ovc copy clip_start.avi -o clip.avi

Note that the -endpos was recalculated for the second command as 39:57, not 57:47.

That’s because the clip_start.avi file is 17 minutes and 50 seconds shorter than the original, and so we need to recalculate the clip end position in terms of the new length.

The file clip.avi contains the clip from 17:50 to 57:47 extracted from the original file, and we can discard the intermediate clip_start.avi file.

It takes two commands because MEncoder seems to ignore the second -ss or -endpos option it finds, and uses just the first one.

It would be nice if it would just let us do this instead:

$ mencoder -ss 00:17:50 -endpos 00:57:47 -oac copy -ovc copy original.avi -o clip.avi

 

A quick guide to DIY animated videos

Friday, December 9th, 2011

Now that TeamWork.io is out in public beta, I wanted to see how difficult it would be to make an intro video, similar to what Google did for its Voice service.

The idea is based on the observation that most people don’t read web pages and would rather watch a video than skim even a brief description.

So with no background in video animation (and no cash to pay anyone to do it for me), I set out to see how far I could get on my own, using free software tools.

I wrote a script consisting of a few frames of stop-motion animation, which I thought would be the simplest to do.

The script starts with someone planning a project, surrounded by a few gantt charts and similar project management paraphernalia. Soon, though, the various charts and forms he needs to process multiply until he’s overwhelmed, and the screen fades to black.

From out of the darkness, a bright light emerges, and the TeamWork.io logo emerges.

That’s just part one. The next step would be to explain how it works, but part one was enough on its own to keep me busy for a while.

Fortunately, there are several free tools available for this kind of production.

Free as in Beer

I started with GIMP, the GNU community’s answer to PhotoShop.

GIMP let me create all the images I needed for part one: the charts and forms smothering our hero are easily done incrementally, by just adding more junk on top and saving each edit as a separate file.

Going from dark to light was also fairly simple, since GIMP has a nice selection of effects filters, one of which, Supernova, let me create a small sunburst in the middle of the black field, then expand it slowly, until the field was white.

Looking back at the first draft, I see that I rushed it a bit too much, but that is a problem with stop-motion: updating changes frame-by-frame is tedious, and there’s always the risk of jumping ahead too much in any given snapshot.

Next, I used Pencil to put the individual frames together with sound and create a single movie file.

Pencil is capable of exporting to QuickTime’s .mov format at a default 851×715 screen resolution, so to keep things simple, I made all my GIMP images 851 pixels wide by 715 pixels tall.

It’s not an ideal aspect ratio for YouTube, though, and I noticed black filler bands on both sides after uploading, but it doesn’t get in the way of comprehending the video.

Pencil also let me add a soundtrack and preview the entire composition of moving frames and sound, but somewhat annoyingly, it didn’t export with sound.

This is a long-time bug, apparently, but I was able to get around it using MEncoder (more on that later).

Finally, I used the speech synthesizer built in to Mac OSX to produce the voice-over.

Say, a free tool for capturing the Mac’s Text-to-Speech output as a file, was invaluable for this task.

Say produces .aiff format sound files, which can be imported as-is into Pencil.

As nice as it is to write a script and have it turned into speech immediately, the sound of a computer-generated voice-over is less than ideal.

“Alex”, by far the best-sounding of the synthesized voices, still came out clunky and awkward.

It seems for the final video I need to bite the bullet and use a real human voice.

The Final Draft Cut

Once I was happy with the sequence of still frames in Pencil, and I made sure the sound synched (more or less) with the video, I created a .mov file of the project.

I could play the .mov file in QuickTime, but, as noted earlier, there was no sound.

That’s where MEncoder comes in, since it’s able to add a sound layer to any video file, using a single command line instruction:

$ mencoder pencil-output.mov -o final.avi \
  -ovc copy -oac copy -audiofile sound.mp3

The only hitch is that it didn’t work with my .aiff file, so I had to convert it to .mp3 format first.

Fortunately, ffmpeg makes this easy:

$ ffmpeg -i sound.aiff -f mp3 -ab 192 \
  -ar 44100 sound.mp3

Here’s the first draft in all its (10 seconds of) glory:

Re-creating Mailinator in Python

Friday, November 11th, 2011

Update: February 21, 2012

I’ve extended this concept into a framework for creating an intelligent email-based agent server, whereby email sent to designated inboxes get dynamic, or custom replies.

It’s the same logic used by the TeamWork.io web service and I’ve decided to open source it on github: https://github.com/dpapathanasiou/intelligent-smtp-responder


Paul Tyma, the creator of Mailinator, once wrote about its architecture. He said that after starting with sendmail, he found it necessary to write his own SMTP server from scratch.

While he never released the Java source code of his server, I wanted to see if I could re-create it using Python, since I also wanted to understand how state machines work in that language.

The Basic Server

To start, I needed some code that would listen on a specific port, and read and respond to clients.

Python’s SocketServer module makes this simple.

Here, in a few lines, is a multi-threaded TCP server that listens on port 8888 of the local machine and echoes back what a connected client sends to it:

#!/usr/bin/python
import SocketServer
cr_lf = "\r\n"
class SMTPRequestHandler (SocketServer.StreamRequestHandler):
    def handle (self):
        try:
            while 1:
                client_msg = self.rfile.readline()
                self.wfile.write(client_msg.rstrip()+cr_lf) # a simple echo
        except Exception, e:
            print e
# server hostname and port to listen on
server_config = ('localhost', 8888) 
if __name__ == '__main__':
    tcpserver = SocketServer.ThreadingTCPServer(server_config, SMTPRequestHandler) 
    tcpserver.serve_forever() 

Start it from a command line prompt (if the port number you choose is less than 1025, then you need to do this as root):

$ python server.py

And test it using telnet:

$ telnet localhost 8888
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
This is an echo
This is an echo
Ok, I get it
Ok, I get it
What next?
What next?

Handling SMTP

Now I needed to be able to understand and reply to SMTP requests. The protocol is fairly simple, with only a handful of commands.

Each command consists of four letters, which appear at the start of the stream sent by the client, and terminated with “\r\n”.

SMTP commands

Tyma did not, however, implement the full list of SMTP commands, since RSET (Reset), VRFY (Verify), NOOP (No operation), and others are used by spammers to abuse or even take over a server, and are rarely required by legitimate email clients.

The server needs to be able to handle the basic interaction, so HELO (Hello) / EHLO (Extended Hello), MAIL (Mail from), RCPT TO (Recipient To), and DATA all need to be supported.

At first glance, it’s tempting to try to implement it like this:

class SMTPRequestHandler (SocketServer.StreamRequestHandler):
    def handle (self):
        try:
            data = {}
            while 1:
                client_msg = self.rfile.readline()
                if client_msg.startswith('MAIL FROM:'):
                    data['sender'] = get_email_address(client_msg)
                elif client_msg.startswith('RCPT TO:'):
                    data['recipient'] = = get_email_address(client_msg)
                ...
                elif client_msg.startswith('QUIT'):
                    break
        except Exception, e:
            print e

Where get_email_address() is defined as, for example, something like this:

def get_email_address (s):
    """Parse out the first email address found in the string and return it"""
    for token in s.split():
        if token.find('@') > -1:
            # token will be in the form:
            # 'FROM:' or 'TO:'
            # and with or without the <>
            for email_part in token.split(':'): 
                if email_part.find('@') > -1:
                    return email_part.strip('<>')

But this gets messy in a hurry. While some commands fit within the neat single-line /^CMND rest of data\r\n/ pattern, others do not.

RCPT, for example, can be repeated multiple times, and once DATA is seen, every subsequent line must be collected until the final /^\.$/ appears.

State Machines to the rescue

A state machine provides a much better way of handling SMTP requests. In his excellent article, David Mertz defines a state machine as:

a directed graph, consisting of a set of nodes and a corresponding set of transition functions. The machine “runs” by responding to a series of events. Each event is in the domain of the transition function belonging to the “current” node, where the function’s range is a subset of the nodes. The function returns the “next” (perhaps the same) node. At least one of these nodes must be an end-state. When an end-state is reached, the machine stops.

And that corresponds exactly to what happens when a client interacts with an SMTP server:

SMTP State Diagram

Brass Tacks

Creating a state machine in Python is simple, since Python allows you to pass functions as higher-order objects. The statemachine.py implementation in Mertz’s article was done in just a few lines of code.

To handle each SMTP node, I defined a series of functions, one for each server response or command.

Here are the function prototypes, where the cargo parameter is a tuple, containing both the stream from/to requests are read and responses written, and a dict of data collected from the request:

def greeting (cargo):
def helo (cargo):
def mail (cargo):
def rcpt (cargo):
def data (cargo):
def process (cargo):

The state machine is defined within the SMTPRequestHandler class like this:

class SMTPRequestHandler (SocketServer.StreamRequestHandler):
    def handle (self):
        try:
            m = StateMachine()
            m.add_state('greeting', greeting)
            m.add_state('helo', helo)
            m.add_state('mail', mail)
            m.add_state('rcpt', rcpt)
            m.add_state('data', data)
            m.add_state('process', process)
            m.add_state('done', None, end_state=1)
            m.set_start('greeting')
            m.run((self, {}))
        except Exception, e:
            print e

So that each function knows how to recognize its assigned command, I defined and compiled these regular expressions. These are created as globals, since it’s more efficient to initiate them once, and have each subsequent method call use the already-existing version.

import re
helo_pattern = re.compile('^HELO', re.IGNORECASE)
ehlo_pattern = re.compile('^EHLO', re.IGNORECASE)
mail_pattern = re.compile('^MAIL', re.IGNORECASE)
rcpt_pattern = re.compile('^RCPT', re.IGNORECASE)
data_pattern = re.compile('^DATA', re.IGNORECASE)
end_pattern = re.compile('^.$')

The greeting() function, which begins the interaction with the client, sends a simple message and passes control to the helo() function. It looks like this:

def greeting (cargo):
    stream = cargo[0]
    stream.wfile.write('220 localhost SMTP'+cr_lf)
    return ('helo', cargo)

Later in the sequence, the mail() function, which is the first node from which data is collected (in this case, the email address of the sender), is the first to save information in the cargo’s dict. It looks like this:

def mail (cargo):
    stream = cargo[0]
    client_msg = stream.rfile.readline()
    if mail_pattern.search(client_msg):
        sender = get_email_address(client_msg)
        if sender is None:
            stream.wfile.write(bad_request+cr_lf)
            return ('done', cargo)
        else:
            email_data = cargo[1]
            email_data['sender'] = sender
            return ('rcpt', (stream, email_data))
    else:
        stream.wfile.write(bad_request+cr_lf)
        return ('done', cargo)        

Here, if the request is not recognized or invalid, the client sees the bad_request message, and the connection is closed, since control passes to the done end-state.

I followed Tyma’s example and defined bad_request as “550 No such user” (which, as he notes, is ironic, since Mailinator accepts email sent to any user).

It also doesn’t conform to the protocol, since I’m supposed to give different error messages at different nodes, but since clients are always disconnected after any type of invalid request, it hardly matters what they see in that scenario.

If a client is well-behaved, the final method called is process() which decides what to do with the client’s email. The data dict will contain three parameters: ‘sender’ (the email address of the sender), ‘recipients’ (a list of email addresses), and ‘data’ (the contents which followed the DATA command ahead of the final ‘.’).

def process (cargo):
    email_data = cargo[1]
    # do something with the email_data dict here
    return ('done', cargo)

Basically, this is where the data can be saved to disk/db (so that it can be served by a web browser later, e.g.), MIME-parsed (to remove attachments, etc.), or just trashed (if you have reason to believe the sender is a spambot or zombie network, e.g.).

Tyma describes various measures for dealing with attacks from spambots and zombies which I haven’t implemented here, but would be relatively easy to add to both the data() and process() functions.

Obtaining the ip address of the client is done using the stream.client_address[0] attribute.

 

Node.js and MongoDB: A Simple Example

Thursday, September 22nd, 2011

Update, October 14, 2012:
If you are interested in creating highly scalable applications with mongoDB, you should really consider using Go (#golang) instead, and check out the Go version of this post.


I’ve been learning Node.js. After getting through an excellent basic tutorial, I wanted to experiment with connecting to MongoDB, since it would be helpful for some enhancements I have planned for Stealth Mode Watch and BookHunch.

There are several drivers available, but the most commonly used and recommended one is node-mongodb-native.

Despite the examples, though, it wasn’t clear how to collect and return the results of a single query.

Both the simple example and the queries example just dump the result to the console within the cursor, and the best answer on the normally reliable StackOverflow site was problematic, too, because the server code was written in a blocking style without callbacks, which defeats the purpose of using Node.js in the first place.

Fortunately, though, there is a terrific article about control flow on How to Node which explains how to do multiple asynchronous tasks (either in parallel or serially), and collect all the results.

The examples in the control flow article dealt with accessing files from different folders, which was simple enough to change to running MongoDB queries instead.

The first step is to install node-mongodb-native using npm:

npm config set loglevel info
npm install mongodb

The first line essentially sets npm in a verbose mode, to let you know what it’s doing as it runs (a tip from the guys at Nodejitsu.com), and the second actually installs the node-mongodb-native module (it seems that the native parser is obsolete, so there is no need to use the --mongodb:native switch in the second line).

Here’s how to execute multiple queries and collect their results.

var Db = require('./node_modules/mongodb').Db,
    Connection = require('./node_modules/mongodb').Connection,
    Server = require('./node_modules/mongodb').Server;

var host = process.env['MONGO_NODE_DRIVER_HOST'] != null ? 
           process.env['MONGO_NODE_DRIVER_HOST'] : 'localhost';
var port = process.env['MONGO_NODE_DRIVER_PORT'] != null ?
           process.env['MONGO_NODE_DRIVER_PORT'] : Connection.DEFAULT_PORT;

These lines just load the db module and prepare for a connection; no connection has been opened yet.

This function executes the query in the db, collects the result documents in a list, and passes them to a callback function when it’s done iterating through the query cursor:

function runQuery (db, myCollection query, nextFn) {
    // perform the {query} on the collection and invoke the nextFn when done
    db.open(function(err, db) {
	db.collection(myCollection, function(err, collection) {
	    collection.find(query, function(err, cursor) {
		cursor.toArray(function(err, docs) {
		    console.log("Found " + docs.length + " documents");
		    var queryResults = [];
		    for(var i=0; i<docs.length; i++) {
			queryResults[queryResults.length] = docs[i]; 
		    }
		    db.close();
		    nextFn(queryResults);
		});
	    });
	});
    });
}

So that leaves opening the connection, defining the query or queries, and calling runQuery().

Suppose we have a database consisting of two collections, people and companies, and that we want to search for a given string anywhere among a person’s name, a company’s name, or even a company’s address.

Our search function would look like this:

function search (personName, companyName, address, nextFn) {
    var data = [], count = 3;
    var doneFn = function(results) {
	data = data.concat(results);
	count--;
	if( count <= 0 ) {
	    var uniqueResults = [];
	    for(var i=0; i<data.length; i++) {
		if( ! uniqueResults[data[i]['_id']] ) {
		    uniqueResults[uniqueResults.length] = data[i];
		    uniqueResults[data[i]['_id']] = true;
		}
	    }
	    nextFn(uniqueResults);
	}
    };
    runQuery(new Db('mydb', new Server(host, port, {})),
	     'people',
	     {'name':new RegExp('^'+personName, 'i')},
	     doneFn);
    runQuery(new Db('mydb', new Server(host, port, {})),
	     'companies',
	     {'name':new RegExp('^'+companyName, 'i')},
	     doneFn);
    runQuery(new Db('mydb', new Server(host, port, {})),
	     'companies',
	     {'address':new RegExp(address, 'i')},
	     doneFn);
}

Just like the control flow article, we use a counter in our callback function — doneFn — to check when there are no more queries to run, and once that’s the case, we iterate through the combined list of results and use the document ObjectId to ensure the list of results is unique.

That unique list of results is then passed to the callback function given to search(), i.e., nextFn, which ultimately decides what to do with the combined results.

Each call to runQuery() needs its own db connection, opened like this:

new Db('mydb', new Server(host, port, {}))

because each query will run asynchronously and independently of each other.

It’s also possible to define a single connection, once, before each of the runQuery() calls happen:

var db = new Db('mydb', new Server(host, port, {}));

and just pass the db variable to runQuery(), but then the db.close() line would have to be removed from inside the runQuery() function, and placed inside the doneFn instead.

As for the queries themselves, the beautiful thing about using Javascript and MongoDB together is that any native Javascript object doubles as the query input to MongoDB.

So any query that works in the MongoDB command line, will work in Javascript without much or any transformation necessary, unlike the hoops you have to jump through in Python+pymongo.

Finally, the function that calls search() has to decide what to do with the results.

If we’re writing an API and want the results sent back over HTTP as json, all we have to do is this (within the larger context of an http server in Node.js, of course):

apiSearch('Jones', 'Jones', 'California', function(results) {
    var replyJson = {"warning":"No matches found"};
    if( results.length > 0 ) {
	replyJson = {"result":{ "matched":results.length, "matches":results}};
    }
    response.writeHead(200, {"Content-Type": "application/json"});
    response.write(JSON.stringify(replyJson));
    response.end();
});

This example searches the database for people or companies named Jones, or companies located in California.

The final callback here gets passed the unique list of results from the search.doneFn — i.e., it is the nextFn passed into search() — and generates an http response in json format.

 

Illustrated ebooks with epub: using the svg image element

Wednesday, June 15th, 2011

All things being equal, the epub format is preferable to pdf for reading on devices like the iPad, Nook, Sony, Kobo, and other dedicated e-readers.

But for some types of books (such as manga, comics, or graphic novels), epub doesn’t seem to be able to handle large images that should fill the screen.

Apple has gone so far as to create its own fixed layout format for such ebooks.

It is possible, though, to stick with epub and get perfect results for illustrated ebooks, using the forgotten (or perhaps simply overlooked) svg image element.

Here’s a peek under the hood of how we do it at eBookBurn.com, as part of our new illustrated ebook publishing feature.

This is an example of the markup to use in your epub’s xhtml files for each image:

<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="100%" height="100%" viewBox="0 0 592 900" preserveAspectRatio="xMidYMid meet">
<image width="592" height="900" xlink:href="images/page01.jpeg" />
</svg>

What this markup does is take a JPEG image sized 592 pixels wide by 900 pixels tall, and frame it in the center of a 592×900 svg element.

It turns out that 592×900 is the right size and aspect ratio for “standard-sized” six inch e-ink screens found on the regular Nook, and Sony Reader.

So why use svg at all?

Wouldn’t it be simpler to define it with the plain img tag (as this epub template does), like this?

<img src="images/page01.jpeg" width="592" height="900" alt="Page 1"/>

Unfortunately, many devices, such as the iPad and the Nook Color, have screens larger than six inches.

So on those devices, using the plain img tag in your epub’s xhtml files leaves an embarrassing whitespace gap, from where the image stops to where the actual screen ends.

The svg element shown earlier, though, is different: it stretches the image to fill the entire screen, while preserving the aspect ratio.

It’s also important to note that 592 width and 900 height specified within the svg and image elements should not be thought of as pixel sizes, since no units are defined, but as a width-to-height ratio.

So any image with the same aspect ratio as 592×900 will work well, regardless of its actual size. Scaling up to larger screens, though, also means the dpi count should be reasonably high, at least 72 dpi (and more for images whose base size is smaller than 592×900 pixels).

For most devices, that’s enough, but some e-readers insist on adding margins and other padding to each page, so it’s helpful to define this in the xhtml file’s head block:

<style type="text/css">
@page { margin: 0.000000pt; padding: 0.000000pt; }
</style>

And these css classes in the stylesheet:

.svg_outer {
display: block;
margin-bottom: 0;
margin-left: 0;
margin-right: 0;
margin-top: 0;
padding-bottom: 0;
padding-left: 0;
padding-right: 0;
padding-top: 0;
text-align: left;
}

.svg_inner {
display: block;
text-align: center;
}

So the final xhtml for each page image looks like this:

<div class="svg_outer">
<div class="svg_inner">
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="100%" height="100%" viewBox="0 0 592 900" preserveAspectRatio="xMidYMid meet">
<image width="592" height="900" xlink:href="images/page01.jpeg" />
</svg>
</div>
</div>

Just repeat that pattern for every image in book, for every chapter that contains full-page illustrations.


Update, May 1, 2012: Moriah Jovan has been kind enough to provide sample epub files created using this technique:


Repairing the MythTV Database

Saturday, May 7th, 2011

I’ve been pretty happy with MythDora over KnoppMyth overall (it’s actively maintained, for one thing), but like most MythTV setups, the MySQL database tends to crash with regular use, despite running the DB Optimize Script regularly.

After having to type (and re-type) a series of terminal commands, I finally just wrote a shell script to do everything automatically:


#!/bin/sh

service mythbackend stop
service mysqld stop
cd /var/lib/mysql/mythconverg
myisamchk *.MYI > crash.log 2>&1

grep corrupt crash.log | grep table | \
sed -e 's/ is corrupted//' | \
sed -e 's/MyISAM-table //' | \
sed -e "s/'//g" | \
sed -e 's/^/myisamchk -r /' > repair.sh

grep fixed crash.log | \
sed -e 's/ is usable but should be fixed//' | \
sed -e 's/MyISAM-table //' | \
sed -e "s/'//g" | \
sed -e 's/^/myisamchk -r /' >> repair.sh

sh repair.sh > repair.log 2>&1
myisamchk -c *.MYI > after.log 2>&1

echo "**** After repair ****"
grep -i fix after.log
grep -i corrupt after.log
mv *.log /tmp
mv *.sh /tmp

echo "**** Repair done ****"
echo "Check /tmp/after.log and if ok, run:"
echo " service mysqld start"
echo " service mythbackend start"

Ok, not quite everything, since the repair results should be checked (a simple grep for “fix” and “corrupt” suffices) before restarting the mythv services, but recovery is much easier now.