Commits

Gregory Petukhov committed a2c7f91

Initial import

  • Participants

Comments (0)

Files changed (6)

+syntax:glob
+
+# Common
+*.pyc
+*.pyo
+*.swp
+*.swo
+*.orig
+
+# Project specific
+pip-log.txt
+key.txt
+title.txt
+target.dbm
+text.dbm

File convert_lines.py

+#!/usr/bin/env python
+# Copyright: 2011, Grigoriy Petukhov
+# Author: Grigoriy Petukhov (http://lorien.name)
+# License: BSD
+import sys
+import anydbm
+
+def main():
+    infile = open(sys.argv[1], 'r')
+    out = anydbm.open(sys.argv[2], 'n')
+    count = 0
+    for row in infile:
+        row = row.strip()
+        if len(row) > 10:
+            out[str(count)] = row
+        count += 1
+    out.close()
+    infile.close()
+
+
+if __name__ == '__main__':
+    main()

File convert_urlkey.py

+#!/usr/bin/env python
+# Copyright: 2011, Grigoriy Petukhov
+# Author: Grigoriy Petukhov (http://lorien.name)
+# License: BSD
+import sys
+import anydbm
+import csv
+
+def main():
+    infile = open(sys.argv[1], 'r')
+    reader = csv.reader(infile)
+    out = anydbm.open(sys.argv[2], 'n')
+    count = 0
+    for row in reader:
+        key, url, blank = row
+        if len(url) > 1:
+            out[str(count)] = '%s|%s' % (url, key)
+            count += 1
+    out.close()
+    infile.close()
+
+
+if __name__ == '__main__':
+    main()

File typhoon/__init__.py

Empty file added.

File typhoon/templatetags/__init__.py

Empty file added.

File typhoon/templatetags/typhoon_tags.py

+# Copyright: 2011, Grigoriy Petukhov
+# Author: Grigoriy Petukhov (http://lorien.name)
+# License: BSD
+import logging
+import anydbm
+import hashlib
+from random import randint
+from fcntl import flock, LOCK_EX, LOCK_UN
+import tempfile
+import os
+
+from django import template
+from django.conf import settings
+from django.utils.safestring import mark_safe
+
+register = template.Library()
+
+LOCK_FILE = os.path.join(tempfile.gettempdir(), hashlib.sha1(settings.TYPHOON_PAGE_DB).hexdigest()) 
+
+@register.filter
+def typhoon_link(request):
+    """
+    Output the random link build with following algorithm.
+
+    If link was already built for current URL just display it.
+    In other case construct the link and save it for future usage.
+
+    Link construction:
+    * Take two random strings from `settings.TYPHOON_TEXT_DB` dbm file.
+    * Take random (url, anchor) pair from `settings.TYPHOON_TARGET_DB`.
+    * Construct the A tag with (url, anchor) pair and insert
+      this tag between two texts.
+    """
+
+    try:
+        dbtext = anydbm.open(settings.TYPHOON_TEXT_DB, 'r')
+        dbtext_size = len(dbtext)
+        dbtarget = anydbm.open(settings.TYPHOON_TARGET_DB, 'r')
+        dbtarget_size = len(dbtarget)
+        dbpage = anydbm.open(settings.TYPHOON_PAGE_DB, 'c')
+        urlhash = hashlib.md5(request.build_absolute_uri()).hexdigest()
+
+        try:
+            item = dbpage[urlhash]
+        except KeyError:
+            # Avoid multithreaded access to page database
+            lock = open(LOCK_FILE, 'w')
+            flock(lock.fileno(), LOCK_EX)
+
+            text1 = dbtext[str(randint(0, dbtext_size - 1))] 
+            text2 = dbtext[str(randint(0, dbtext_size - 1))] 
+            target = dbtarget[str(randint(0, dbtarget_size - 1))]
+            url, key = target.split('|', 1)
+            item = ' %s <a href="%s">%s</a> %s. ' % (text1, url, key, text2)
+            dbpage[urlhash] = item
+
+            # Release the lock
+            flock(lock.fileno(), LOCK_UN)
+        return mark_safe(item)
+    except Exception, ex:
+        logging.error('TYPHOON ERROR', exc_info=ex)