1.1 --- a/tiny.py Tue Jul 16 19:02:57 2013 +0200
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,28 +0,0 @@
1.4 -#!/usr/bin/env python
1.5 -
1.6 -# See: https://answers.atlassian.com/questions/87971/what-is-the-algorithm-used-to-create-the-tiny-links
1.7 -
1.8 -from base64 import b64decode, b64encode
1.9 -from struct import pack, unpack
1.10 -import sys
1.11 -
1.12 -def tiny_url(s):
1.13 - return b64encode(pack("<I", int(s)).rstrip("\x00")).rstrip("=").replace("+", "_").replace("/", "-")
1.14 -
1.15 -def identifier(s):
1.16 - bytes = b64decode(s.replace("-", "/").replace("_", "+") + "=" * (6 - len(s)))
1.17 - return unpack("<I", bytes + "\x00" * (4 - len(bytes)))[0]
1.18 -
1.19 -arg = sys.argv[1]
1.20 -reverse = len(sys.argv) > 2 and sys.argv[2] in ("-r", "--reverse")
1.21 -fn = reverse and identifier or tiny_url
1.22 -
1.23 -if arg == "-":
1.24 - for line in sys.stdin.readlines():
1.25 - line = line.strip()
1.26 - if line:
1.27 - print "%s\t%s" % (fn(line), line)
1.28 -else:
1.29 - print fn(arg)
1.30 -
1.31 -# vim: tabstop=4 expandtab shiftwidth=4