Command line Broadband Usage Meter (SLT)

සටහන: 2014-01-02 දින සිට SLT වෙබ්‍ අඩවියේ කරනලද වෙනස්කම් හේතුවෙන් මෙම කේතය භාවිතයෙන් තවදුරටත් ප්‍රතිපල ලබාගත නොහැක.

දැන් SLT භාවිත කරන හැම දෙනාගේම දත්ත භාවිතා කරන ප්‍රමාණය සීමා කරල නිසා පරිස්සමෙන් පාවිච්චි කරන්න වෙලා. දැනට කොච්චර පාවිච්චි කරලද කියල බලන්න නිතර Usage Meter එකට ලොග්වෙන්න තියන කම්මැලි කමට මේක හදා ගත්ත.

අවවාදයයි: වෙබ් අඩවියක ඇති තොරතුරු වානිජ්‍ය පරමාර්ථ සඳහා මේ ආකාරයෙන් (Screen Scraping) ලබාගැනීම, එම සේවාව අඩාල වන අයුරින් හෝ යම් පුද්ගලයින්ට හෝ ආයතනයකට හානි වන අයුරින් භාවිත කිරීම නීති විරෝධී වේ.

bbusage.py
  1. #!/usr/bin/env python
  2. #
  3. #  bbusage.py: Grab and display SLT ADSL Usage data from online usage meter
  4. #  Copyright 2012 Sudaraka Wijesinghe <sudaraka dot wijesinghe at gmail dot com>
  5. #
  6. #  This program is free software: you can redistribute it and/or modify
  7. #  it under the terms of the GNU General Public License as published by
  8. #  the Free Software Foundation, either version 3 of the License, or
  9. #  (at your option) any later version.
  10. #
  11. #  This program is distributed in the hope that it will be useful,
  12. #  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. #  GNU General Public License for more details.
  15. #
  16. #  You should have received a copy of the GNU General Public License
  17. #  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. #  USAGE: bbusage.py <broadband_username> [number_of_months]
  20. #
  21. #  DISCLAIMER:
  22. #  General public is encourage NOT to use this code for financial gain or any
  23. #  form of act that will result in harm to any person or company. Author will
  24. #  NOT be responsible of such usage.
  25. #
  26. #  METHODOLOGY:
  27. #  This program exploit the fact that SLT Broadband usage meter result page
  28. #  (http://webusage.slt.lk:8080/servlet/BmsWeb as of 11/30/2012) does not
  29. #  require a valid login session in order to generate the usage report.
  30. #
  31.  
  32.  
  33.  
  34. import sys
  35. import re
  36. import datetime
  37. import urllib
  38. import httplib2
  39.  
  40.  
  41.  
  42. USAGE_METER_URL     = 'http://webusage.slt.lk:8080/servlet/BmsWeb'
  43. USAGE_METER_SERVICE = 'SLTBB'
  44.  
  45.  
  46.  
  47. #  Print the usage information
  48. # ============================================================================
  49. def print_usage():
  50.   print '''
  51. USAGE: bbusage.py <broadband_username> [number_of_months]
  52.  
  53.   broadband_username    username assigned to you by the ISP
  54.   number_of_months      Number of previous months to display (default = 1)
  55. '''
  56.  
  57.  
  58.  
  59. #  Extract usage volume from CDR text
  60. # ============================================================================
  61. def get_bb_usage(user, year, month):
  62.   month_flag = datetime.date(year, month, 1).strftime('%b/%Y').upper()
  63.  
  64.   # Get CDR text
  65.   cdr_text = get_cdr(user, month_flag)
  66.  
  67.   try:
  68.     # Check for 'No Records' text and return if it's there
  69.     if 'No Records Found' in cdr_text:
  70.       return None
  71.  
  72.     # Find the last row with caption "Total" as a keyword
  73.     cdr_text = cdr_text.split('Total')[1]
  74.  
  75.     # Find the volume cdr_text nnn,nnn.nn MB
  76.     match = re.search(r'([\d,\.]+)\s+MB', cdr_text)
  77.     volume = match.group(1).replace(',', '')
  78.   except:
  79.     return None
  80.  
  81.   # Return volume in Giga bytes
  82.   return float(volume) / 1024
  83.  
  84.  
  85.  
  86. #  Get CDR text over HTTP POST
  87. # ============================================================================
  88. def get_cdr(user, month_flag):
  89.   http = httplib2.Http()
  90.  
  91.   try:
  92.     # Get text via HTTP POST
  93.     text = http.request(USAGE_METER_URL, 'POST',
  94.       urllib.urlencode({
  95.         'service':USAGE_METER_SERVICE,
  96.         'userName':user,
  97.         'mon1':month_flag,
  98.         'mon2':'N',
  99.         'mon3':'N',
  100.         'I1':'submit'
  101.       }),
  102.       {
  103.         'Content-Type':'application/x-www-form-urlencoded'
  104.       }
  105.     )
  106.   except:
  107.     return None
  108.  
  109.   return text[1]
  110.  
  111.  
  112.  
  113. #  Main program
  114. # ============================================================================
  115. if '__main__' == __name__:
  116.   if 2 > len(sys.argv):
  117.     print_usage()
  118.     sys.exit(1)
  119.  
  120.   # Sanitize the user name
  121.   bbuser = sys.argv[1]
  122.   if '@' in bbuser:
  123.     bbuser = bbuser.split('@')[0]
  124.  
  125.   if None == re.match('^[a-z]{2,3}\d{7,10}$', bbuser):
  126.     print 'Invalid broadband username:', sys.argv[1]
  127.     sys.exit(1)
  128.  
  129.   # Number of previous months to load data from
  130.   # Use the second command line parameter only if it is an integer
  131.   try:
  132.     nof_months = int(sys.argv[2])
  133.   except:
  134.     nof_months = 1
  135.  
  136.   asked_months = nof_months
  137.  
  138.   # Display usage starting from current month and nof_months  backwards
  139.   cdr_year = datetime.date.today().year
  140.   cdr_month = datetime.date.today().month
  141.  
  142.   print '+' + ('-' * 30) + '+'
  143.   print '| Year |', 'Month'.ljust(9), '|', 'Usage |'.rjust(11)
  144.   print '+' + ('-' * 30) + '+'
  145.  
  146.   try:
  147.     while 0 < nof_months:
  148.       volume = get_bb_usage(bbuser, cdr_year, cdr_month)
  149.  
  150.       if None == volume:
  151.         print \
  152.           '|', \
  153.            datetime.date(cdr_year, cdr_month, 1) \
  154.             .strftime('No data for %b,%Y') \
  155.             .center(28), \
  156.           '|'
  157.  
  158.         if 1 == asked_months or nof_months != asked_months:
  159.           break
  160.       else:
  161.         print \
  162.           '|', \
  163.           datetime.date(cdr_year, cdr_month, 1).strftime('%Y'), '|', \
  164.           datetime.date(cdr_year, cdr_month, 1).strftime('%B').ljust(9), '|', \
  165.           '{:.2f} GB'.format(volume).rjust(9), '|'
  166.  
  167.       # Prepare for the next round
  168.       nof_months = nof_months - 1
  169.       cdr_month = cdr_month -1
  170.  
  171.       if 1 > cdr_month:
  172.         cdr_month = 12
  173.         cdr_year = cdr_year - 1
  174.   except KeyboardInterrupt:
  175.     print ''
  176.   finally:
  177.     print '+' + ('-' * 30) + '+'