From cfb4cad519930cf848c0514137b1aebf98d5061f Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Tue, 24 Jul 2012 20:25:01 -0400 Subject: [PATCH] UglifyJS; cache background information. --- README.md | 1 + build.py | 10 ++++++++++ static/js/cookies.js | 17 +++++++++++++---- static/js/potd.js | 21 +++++++++++++++++++++ 4 files changed, 45 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index eb45084..deff407 100644 --- a/README.md +++ b/README.md @@ -10,3 +10,4 @@ Dependencies * [earwigbot](https://github.com/earwig/earwigbot) * [mako](http://www.makotemplates.org/) * [oursql](http://packages.python.org/oursql/) +* [uglifyjs](https://github.com/mishoo/UglifyJS/) diff --git a/build.py b/build.py index 6853983..bb492f2 100755 --- a/build.py +++ b/build.py @@ -4,6 +4,7 @@ import logging import os import shutil +import subprocess page_src = """#! /usr/bin/env python # -*- coding: utf-8 -*- @@ -95,6 +96,15 @@ class Builder(object): logger.debug("copytree {0} -> {1}".format(self.static_dir, dest)) shutil.copytree(self.static_dir, dest) + for dirpath, dirnames, filenames in os.walk(dest): + for filename in filenames: + if filename.endswith(".js"): + name = os.path.join(dirpath, filename) + logger.debug("uglifyjs {0}".format(name)) + uglified = subprocess.check_output(["uglifyjs", name]) + os.remove(name) + with open(name, "w") as fp: + fp.write(uglified) def gen_pages(self): logger = self.root.getChild("pages") diff --git a/static/js/cookies.js b/static/js/cookies.js index 7a0ebab..62e52b9 100644 --- a/static/js/cookies.js +++ b/static/js/cookies.js @@ -18,18 +18,27 @@ function get_cookie(name) { return null; } -function set_cookie(name, value, days) { +function set_cookie_with_date(name, value, date) { value = window.btoa("--ets1" + value); var path = window.location.pathname.split("/", 2)[1]; + if (date) { + var expires = "; expires=" + date.toUTCString(); + } + else { + var expires = ""; + } + document.cookie = name + "=" + value + expires + "; path=/" + path; +} + +function set_cookie(name, value, days) { if (days) { var date = new Date(); date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); - var expires = "; expires=" + date.toGMTString(); + set_cookie_with_date(name, value, date); } else { - var expires = ""; + set_cookie_with_date(name, value); } - document.cookie = name + "=" + value + expires + "; path=/" + path; } function delete_cookie(name) { diff --git a/static/js/potd.js b/static/js/potd.js index 2095a24..02d5bb0 100644 --- a/static/js/potd.js +++ b/static/js/potd.js @@ -1,4 +1,20 @@ function potd_set_background() { + var cookie = get_cookie("EarwigBackgroundCache"); + if (cookie) { + try { + data = JSON.parse(cookie); + var url = data.url; + var descurl = data.descurl; + var imgwidth = data.imgwidth; + var imgheight = data.imgheight; + if (url && descurl && imgwidth && imgheight) { + set_background(url, descurl, imgwidth, imgheight); + return; + } + } + catch (SyntaxError) {} + } + var d = new Date(); var callback = "earwigpotd1"; var date = (d.getUTCFullYear()) + "-" + zero_pad(d.getUTCMonth() + 1, 2) + "-" + zero_pad(d.getUTCDate(), 2); @@ -55,7 +71,12 @@ function parse_file_url(data, filename) { imgwidth = r["width"]; imgheight = r["height"]; } + set_background(url, descurl, imgwidth, imgheight); + var data = {"url": url, "descurl": descurl, "imgwidth": imgwidth, "imgheight": imgheight}; + var now = new Date(); + var expires = new Date(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate() + 1); + set_cookie_with_date("EarwigBackgroundCache", JSON.stringify(data), expires); } function set_background(url, descurl, imgwidth, imgheight) {