import sys, csv, json, os, re def emit_header(): str_s = '
' str_s += 'Location:
' str_s += 'Amsterdam
\n' str_s += '
\n' str_s += 'User-Agent:
' str_s += "Mozilla/5.0 (Windows NT 6.1; WOW64)
AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/43.0.2357.132 Safari/537.36 PTST/221\n" str_s += '
\n' return str_s def emit_name(name, date, time): str_s = '
' + name + '
\n' str_s += '
' + date + '
\n' str_s += '
' + time + '
\n' return str_s def string_format_percentage(pct): v = int(pct * 100) return str(v) def emit_table_row(elem, index, total): return '' + '' + index +': ' + ''+ str(int(elem[index])) + '' + string_format_percentage(elem[index] / total) + '%' + '\n' def emit_size(size): total = size['widget'] + size['ad'] + size['privacy'] + size['-'] + size['analytics'] + size['tracker'] if total == 0: total = 1 str_s = '
' str_s += '

Objects Size (bytes)

\n' str_s += '
\n' str_s += '\n' str_s += emit_table_row(size, 'ad', total).replace('ad', 'ads') str_s += emit_table_row(size, 'analytics', total) str_s += emit_table_row(size, 'tracker', total).replace('tracker', 'trackers') str_s += emit_table_row(size, 'widget', total).replace('widget', 'widgets') str_s += emit_table_row(size, '-', total).replace('-', 'other') str_s += '
\n' str_s += '
\n' str_s += '
\n' return str_s def emit_item(item): total = item['total'] total_junk = item['widget'] + item['ad'] + item['privacy'] + item['analytics'] + item['tracker'] if total == 0: total = 1 str_s = '
' str_s += '

Page Http Request Elements

\n' str_s += '
\n' str_s += '\n' str_s += emit_table_row(item, 'ad', total).replace('ad', 'ads') str_s += emit_table_row(item, 'analytics', total) str_s += emit_table_row(item, 'tracker', total).replace('tracker', 'trackers') str_s += emit_table_row(item, 'widget', total).replace('widget', 'widgets') str_s += emit_table_row(item, '-', total).replace('-', 'other') str_s += '
\n' str_s += '
\n' str_s += '
\n' return str_s def emit_time(time): total = time['widget'] + time['ad'] + time['privacy'] + time['-'] + time['analytics'] + time['tracker'] if total == 0: total = 1 str_s = '
' str_s += '

(Micro) Timing (ms)

\n' str_s += '
\n' str_s += '\n' str_s += emit_table_row(time, 'ad', total).replace('ad', 'ads') str_s += emit_table_row(time, 'analytics', total) str_s += emit_table_row(time, 'tracker', total).replace('tracker', 'trackers') str_s += emit_table_row(time, 'widget', total).replace('widget', 'widgets') str_s += emit_table_row(time, '-', total).replace('-', 'other') str_s += '
\n' str_s += '
\n' str_s += '
\n' return str_s if __name__ == '__main__': fp = sys.stdin try: stats = json.loads(fp.read()) except Exception, ee: sys.exit('Error loading data... Aborting.') try: template = open(os.path.join('.', 'index_template.html'), 'r+'); except: print('error opening template file. aborting...'); sys.exit(0); content = "" content += emit_header() for e in stats: card = "
\n" stats = e['stats'] card += "\t\t\t" + emit_name(stats['host'], stats['date'], stats['time']) card += "\t\t\t" + emit_item(stats['items']) card += "\t\t\t" + emit_size(stats['sizes']) card += "\t\t\t" + emit_time(stats['times']) card += "
\n" content += card html = template.read().replace('[[content]]', content); print html