Created
July 27, 2014 20:11
-
-
Save Peetz0r/07cf9728c863606d73cb to your computer and use it in GitHub Desktop.
Ben mobile data usage parser and grapher
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2 | |
from __future__ import print_function | |
from pdfminer.converter import TextConverter | |
from pdfminer.layout import LAParams | |
from pdfminer.pdfinterp import PDFResourceManager, process_pdf | |
from calendar import monthrange | |
import StringIO, re, glob, os, pygal, locale, datetime, sys | |
locale.setlocale(locale.LC_ALL, '') | |
all_months_data = [] | |
all_months_rendered = [] | |
filenames = glob.glob("FACT*_*.pdf") | |
filenames.sort() | |
out = '<!doctype html>' | |
out += '<html>' | |
out += '<head>' | |
out += '<meta charset="utf-8">' | |
out += '<title>Mobile data usage by day and by month</title>' | |
out += '<style>' | |
out += '* {' | |
out += 'background-color: black;' | |
out += 'padding: 0;' | |
out += 'margin: 0;' | |
out += 'text-align: center;' | |
out += 'line-height: 200%;' | |
out += 'color: white;' | |
out += 'font-family: sans-serif;' | |
out += '}' | |
out += '</style>' | |
out += '</head>' | |
out += '<body>' | |
for file_nr, filename in enumerate(filenames): | |
print('[%s>%s]' % ('='*file_nr*2, ' '*(len(filenames)*2-file_nr*2)), end='\r') | |
sys.stdout.flush() | |
rsrcmgr = PDFResourceManager() | |
retstr = StringIO.StringIO() | |
codec = 'utf-8' | |
laparams = LAParams() | |
device = TextConverter(rsrcmgr, retstr, codec=codec) | |
fp = file(filename, 'rb') | |
process_pdf(rsrcmgr, device, fp) | |
fp.close() | |
device.close() | |
m = re.findall('Internet(\d{2}-\d{2}-\d{4})\d{2}:\d{2}:\d{2}\s*(\d+) kb', retstr.getvalue()) | |
retstr.close() | |
total = 0.0 | |
now = datetime.datetime.strptime(m[0][0], '%d-%m-%Y') | |
data_list = [0]*monthrange(now.year, now.month)[1] | |
for day in m: | |
day_nr = datetime.datetime.strptime(day[0], '%d-%m-%Y').day-1 | |
data_list[day_nr] += int(day[1])/1024 | |
total += int(day[1]) | |
all_months_data.append([now.strftime('%b%y'), round(total/1024/1024, 2)]) | |
bar_chart = pygal.Bar() | |
bar_chart.title = now.strftime('Mobile data usage by day in %B %Y (total: '+'%.2f GB' % (total/1024/1024)+')') | |
bar_chart.x_labels = map(str, range(1,len(data_list)+1)) | |
bar_chart.add('MB', data_list) | |
all_months_rendered.append(bar_chart.render()) | |
all_months_rendered.reverse() | |
bar_chart = pygal.Bar() | |
bar_chart.title = 'Mobile data usage by month' | |
bar_chart.add('GB', list(map(list.pop, all_months_data))) | |
bar_chart.x_labels = list(map(list.pop, all_months_data)) | |
out += bar_chart.render() | |
out += ''.join(all_months_rendered) | |
out += '<hr>Generated at ' | |
out += datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') | |
out += '</body>' | |
out += '</html>' | |
with open('mobile-data-usage.html', 'w') as fp: | |
fp.write(out) | |
print('[%s]' % ('='*(len(filenames)*2+1))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment