Showing posts with label snippets. Show all posts
Showing posts with label snippets. Show all posts

2012-12-07

Swedish post tracking cron job

/usr/local/bin/post-check
#!/bin/bash

# Script arguments
# argument 1 - package number, i.e. 012345670001234567
# argument 2 - mail address for package status updates
nr=$1
to=$2

# Swedish post perma-link
link="http://server.logistik.posten.se/servlet/PacTrack"
link="$link?lang=GB&xslURL=/xsl/pactrack/standard.xsl&kolliid=$1"

# Script saves status updates into temp dir
cd /tmp/

# Send the mail
# argument 1 - subject
# argument 2 - message
function send_mail() {
 # arg1 subject
 echo "$2" | mail -s "$1" \
    -a "MIME-Version: 1.0" \
    -a "Content-Type: text/html" \
    "$to"
}

# Download package latest status
wget "$link" -q -O post-status.new
if [ $? -ne 0 ]; then
 error="Could not download package status."
 echo "$error Mail sent."
 send_mail "Package $nr status error" "$error"
 exit
fi
# Make sure both current and previous status file exists
touch post-status.new
touch post-status.old

# Check if there is a difference in two last status sizes
new_size=$(stat -c%s post-status.new)
old_size=$(stat -c%s post-status.old)
if [ $new_size -ne $old_size ]; then
 echo "Package status updated. Mail sent."
 send_mail "Package $nr status updated" "`cat post-status.new`"
fi

# Save the status
cp post-status.new post-status.old

crontab -e
*/15 * * * * /usr/local/bin/post-check 012345670001234567 your@mail.com

2009-05-21

PostgreSQL binary timestamp functions

While writing libpq wrapper I've run into problems with binary timestamps, which required a lot of debugging and headaches. To avoid your own migraine, feel free to use my code below. Note, however, this code is not perfect, as it's uses long long for timestamps. This will work if your machine is 32bit and your server was compiled with `--enable-integer-datetimes` (highly recommended). If not, You're welcome to patch this code and post it here, so other people will thank you.

2009-05-05

Lua constants

By Lua's nature, it is not possible to create constants. There is a workaround, though, using metatables:

function protect(tbl)
    return setmetatable({}, {
        __index = tbl,
        __newindex = function(t, key, value)
            error("attempting to change constant " ..
                   tostring(key) .. " to " .. tostring(value), 2)
        end
    })
end

constants = {
    x = 1,
    y = 2,
}
constants = protect(constants)

print(constants.x) --> 3
constants.x = 3 --> error: attempting to change constant x to 3

2009-04-16

C Lua snippets

Get currently executed chunk name:
lua_Debug lua_info;
lua_getstack(L, 0, &lua_info);
lua_getinfo(L, "n", &lua_info);
string name = lua_info.name;
Opposite to lua_register:
lua_pushnil(L);
lua_setglobal(L, name.c_str());
stack_dump in C++ flavour ;)
static void stack_dump (lua_State *L) {
    int top = lua_gettop(L);
    for(int i = 1; i <= top; i++) {
        int t = lua_type(L, i);
        switch(t) {
            case LUA_TSTRING:
                cout << "'" << lua_tostring(L, i) << "'";
                break;
            case LUA_TBOOLEAN:
                cout << (lua_toboolean(L, i) ? "true" : "false");
                break;
            case LUA_TNUMBER:
                cout << lua_tonumber(L, i);
                break;
            default:
                cout << lua_typename(L, t);
                break;
        }
        cout << "  ";
    }
    cout << endl;
}

2009-04-01

Store an array in a session

// begin the session
session_start();

// create an array
$my_array = array('blonde', 'girls', 'are', 'stupid');

// put the array in a session variable
$_SESSION['truth'] = $my_array;