Merge branch 'master' into looksirdroids

This commit is contained in:
Joseph Schorr 2013-11-22 18:22:29 -05:00
commit 43f2dd80a0
38 changed files with 752 additions and 400 deletions

View file

@ -1,5 +1,7 @@
from datetime import datetime
from jinja2 import Environment, FileSystemLoader
from xhtml2pdf import pisa
import StringIO
jinja_options = {
"loader": FileSystemLoader('util'),
@ -7,6 +9,20 @@ jinja_options = {
env = Environment(**jinja_options)
def renderInvoiceToPdf(invoice, user):
""" Renders a nice PDF display for the given invoice. """
sourceHtml = renderInvoiceToHtml(invoice, user)
output = StringIO.StringIO()
pisaStatus = pisa.CreatePDF(sourceHtml, dest=output)
if pisaStatus.err:
return None
value = output.getvalue()
output.close()
return value
def renderInvoiceToHtml(invoice, user):
""" Renders a nice HTML display for the given invoice. """
def get_price(price):

37
util/phantomjs-runner.js Normal file
View file

@ -0,0 +1,37 @@
var system = require('system');
var url = system.args[1] || '';
if(url.length > 0) {
var page = require('webpage').create();
page.open(url, function (status) {
if (status == 'success') {
var delay, checker = (function() {
var html = page.evaluate(function () {
var found = document.getElementsByTagName('html')[0].outerHTML || '';
if (window.__isLoading && !window.__isLoading()) {
return found;
}
if (found.indexOf('404 Not Found') > 0) {
return found;
}
return null;
});
if (html) {
if (html.indexOf('404 Not Found') > 0) {
console.log('Not Found');
phantom.exit();
return;
}
clearTimeout(delay);
console.log(html);
phantom.exit();
}
});
delay = setInterval(checker, 100);
} else {
console.log('Not Found');
phantom.exit();
}
});
}

23
util/seo.py Normal file
View file

@ -0,0 +1,23 @@
import subprocess
import logging
from bs4 import BeautifulSoup
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.DEBUG)
def render_snapshot(url):
logger.info('Snapshotting url: %s' % url)
out_html = subprocess.check_output(['phantomjs', '--ignore-ssl-errors=yes',
'util/phantomjs-runner.js', url])
if not out_html or out_html.strip() == 'Not Found':
return None
# Remove script tags
soup = BeautifulSoup(out_html.decode('utf8'))
to_extract = soup.findAll('script')
for item in to_extract:
item.extract()
return str(soup)