#!/usr/bin/env python # nm-data-usage shows data usage from your mobile-broandband usage record # Copyright (C) 2009 Kushal Das # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public # License as published by the Free Software Foundation; either # version 2.1 of the License, or (at your option) any later version. # # This library is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA import os import sys import sqlite3 from datetime import datetime from optparse import OptionParser def main(argv): """ ./nm-data-usage will give you today's data usage record""" parser = OptionParser(usage=main.__doc__) parser.add_option('-m', '--month', action="store_true", help="To show current month's usage") options, throaway = parser.parse_args(argv[1:]) if options.month: monthly() else: today() def monthly(): db_file = os.path.join(os.path.expanduser("~"),".nm.db") con = sqlite3.connect(db_file) cursor = con.cursor() results = cursor.execute("SELECT * from mobile_broadband_usage") today = datetime.now().date() print "Access record for ", today.strftime("%B %Y") total_usage = 0 print "-" * 90 print " Connection\t Date\t Started\t Stopped\t Data IN\t\t Data OUT" print "-" * 90 for result in results: try: that_day = datetime.fromtimestamp(float(result[5])).date() if that_day.month == today.month and that_day.year == today.year: start = datetime.fromtimestamp(float(result[5])).strftime("%d/%m/%y\t%H:%M:%S") end = datetime.fromtimestamp(float(result[6])).strftime("%H:%M:%S") print "%12s" % (result[1]),"\t",start, "\t", end , "\t", data_in = int(result[3]) / 1024 if data_in < 1024: print "%5d KB \t\t" % data_in, else: print "%5.2f MB \t\t" % float(data_in/1024.0) , data_out = int(result[4]) / 1024 if data_out < 1024: print "%5d KB" % data_out else: print "%5.2f MB \t\t" % float(data_out/1024.0) total_usage += int(result[3]) + int(result[4]) except: pass print "-" * 90 print "\nTotal data usage : %5.2f MB in current month\n" % float(total_usage / (1024 * 1024)) def today(): db_file = os.path.join(os.path.expanduser("~"),".nm.db") con = sqlite3.connect(db_file) cursor = con.cursor() results = cursor.execute("SELECT * from mobile_broadband_usage") print "Today's access record" today = datetime.now().date() total_usage = 0 print "-" * 90 print " Connection\t Started\t Stopped\t Data IN\t\t Data OUT" print "-" * 90 for result in results: try: that_day = datetime.fromtimestamp(float(result[5])).date() if that_day == today: start = datetime.fromtimestamp(float(result[5])).strftime("%H:%M:%S") end = datetime.fromtimestamp(float(result[6])).strftime("%H:%M:%S") print "%12s" % (result[1]),"\t",start, "\t", end , "\t", data_in = int(result[3]) / 1024 if data_in < 1024: print "%5d KB \t\t" % data_in, else: print "%5.2f MB \t\t" % float(data_in/1024.0) , data_out = int(result[4]) / 1024 if data_out < 1024: print "%5d KB" % data_out else: print "%5.2f MB \t\t" % float(data_out/1024.0) total_usage += int(result[3]) + int(result[4]) except: pass print "-" * 90 print "\nTotal data usage : %5.2f MB in today\n" % float(total_usage / (1024 * 1024)) if __name__=='__main__': main(sys.argv)