1.1 --- a/mappings.sh Tue Jul 16 19:02:57 2013 +0200
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,15 +0,0 @@
1.4 -#!/bin/sh
1.5 -
1.6 -MAPPINGS=$*
1.7 -ID_MAPPING="mapping-id-to-page.txt"
1.8 -TINY_ID_MAPPING="mapping-tiny-to-id.txt"
1.9 -TINY_MAPPING="mapping-tiny-to-page.txt"
1.10 -TAB=`printf '\t'`
1.11 -
1.12 -# Combine the space mappings into a common mapping from page identifiers to
1.13 -# page names.
1.14 -sort -n -u $MAPPINGS > "$ID_MAPPING"
1.15 -
1.16 -# Produce a common mapping from tiny URL identifiers to page names.
1.17 -cut -f 1 "$ID_MAPPING" | uniq | python tiny.py - > "$TINY_ID_MAPPING"
1.18 -join -t "$TAB" -1 2 -2 1 "$TINY_ID_MAPPING" "$ID_MAPPING" | cut -f 2,3 | LC_ALL=C sort > "$TINY_MAPPING"
2.1 --- a/tiny.py Tue Jul 16 19:02:57 2013 +0200
2.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
2.3 @@ -1,28 +0,0 @@
2.4 -#!/usr/bin/env python
2.5 -
2.6 -# See: https://answers.atlassian.com/questions/87971/what-is-the-algorithm-used-to-create-the-tiny-links
2.7 -
2.8 -from base64 import b64decode, b64encode
2.9 -from struct import pack, unpack
2.10 -import sys
2.11 -
2.12 -def tiny_url(s):
2.13 - return b64encode(pack("<I", int(s)).rstrip("\x00")).rstrip("=").replace("+", "_").replace("/", "-")
2.14 -
2.15 -def identifier(s):
2.16 - bytes = b64decode(s.replace("-", "/").replace("_", "+") + "=" * (6 - len(s)))
2.17 - return unpack("<I", bytes + "\x00" * (4 - len(bytes)))[0]
2.18 -
2.19 -arg = sys.argv[1]
2.20 -reverse = len(sys.argv) > 2 and sys.argv[2] in ("-r", "--reverse")
2.21 -fn = reverse and identifier or tiny_url
2.22 -
2.23 -if arg == "-":
2.24 - for line in sys.stdin.readlines():
2.25 - line = line.strip()
2.26 - if line:
2.27 - print "%s\t%s" % (fn(line), line)
2.28 -else:
2.29 - print fn(arg)
2.30 -
2.31 -# vim: tabstop=4 expandtab shiftwidth=4
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
3.2 +++ b/tools/mappings.sh Tue Jul 16 23:52:15 2013 +0200
3.3 @@ -0,0 +1,24 @@
3.4 +#!/bin/sh
3.5 +
3.6 +PROGNAME=`basename $0`
3.7 +THISDIR=`dirname $0`
3.8 +
3.9 +MAPPINGS=$*
3.10 +
3.11 +if [ ! "$MAPPINGS" ]; then
3.12 + echo "$PROGNAME <mapping file>..."
3.13 + exit 1
3.14 +fi
3.15 +
3.16 +ID_MAPPING="mapping-id-to-page.txt"
3.17 +TINY_ID_MAPPING="mapping-tiny-to-id.txt"
3.18 +TINY_MAPPING="mapping-tiny-to-page.txt"
3.19 +TAB=`printf '\t'`
3.20 +
3.21 +# Combine the space mappings into a common mapping from page identifiers to
3.22 +# page names.
3.23 +sort -n -u $MAPPINGS > "$ID_MAPPING"
3.24 +
3.25 +# Produce a common mapping from tiny URL identifiers to page names.
3.26 +cut -f 1 "$ID_MAPPING" | uniq | "$THISDIR/tiny.py" - > "$TINY_ID_MAPPING"
3.27 +join -t "$TAB" -1 2 -2 1 "$TINY_ID_MAPPING" "$ID_MAPPING" | cut -f 2,3 | LC_ALL=C sort > "$TINY_MAPPING"
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
4.2 +++ b/tools/tiny.py Tue Jul 16 23:52:15 2013 +0200
4.3 @@ -0,0 +1,28 @@
4.4 +#!/usr/bin/env python
4.5 +
4.6 +# See: https://answers.atlassian.com/questions/87971/what-is-the-algorithm-used-to-create-the-tiny-links
4.7 +
4.8 +from base64 import b64decode, b64encode
4.9 +from struct import pack, unpack
4.10 +import sys
4.11 +
4.12 +def tiny_url(s):
4.13 + return b64encode(pack("<I", int(s)).rstrip("\x00")).rstrip("=").replace("+", "_").replace("/", "-")
4.14 +
4.15 +def identifier(s):
4.16 + bytes = b64decode(s.replace("-", "/").replace("_", "+") + "=" * (6 - len(s)))
4.17 + return unpack("<I", bytes + "\x00" * (4 - len(bytes)))[0]
4.18 +
4.19 +arg = sys.argv[1]
4.20 +reverse = len(sys.argv) > 2 and sys.argv[2] in ("-r", "--reverse")
4.21 +fn = reverse and identifier or tiny_url
4.22 +
4.23 +if arg == "-":
4.24 + for line in sys.stdin.readlines():
4.25 + line = line.strip()
4.26 + if line:
4.27 + print "%s\t%s" % (fn(line), line)
4.28 +else:
4.29 + print fn(arg)
4.30 +
4.31 +# vim: tabstop=4 expandtab shiftwidth=4