Skip to content

Instantly share code, notes, and snippets.

@nkmathew
Last active August 6, 2017 17:28
Show Gist options
  • Save nkmathew/2b57fc7e1252f9514c13e60b839c72d2 to your computer and use it in GitHub Desktop.
Save nkmathew/2b57fc7e1252f9514c13e60b839c72d2 to your computer and use it in GitHub Desktop.
Get your Safaricom data bundle balance without leaving your prompt
#!/usr/bin/env python3
'''
@date Aug 06, 2017
@author nkmathew
bundles - Get your Safaricom data bundle balance without leaving your prompt
Saves you the effort of going to this page every time : http://www.safaricom.com/bundles/
Example output:
| Account Types | Prepaid |
+--------------------------------------------------+
| On-net SMS Expiry Date | 06-Aug-2017 23:18 |
+--------------------------------------------------+
| Data Bundle | 7.53 MBs |
+--------------------------------------------------+
| Data Bundle Expiry Date | 01-Jan-2037 00:00 |
+--------------------------------------------------+
| Bonga Balance | 727.87 Points |
+--------------------------------------------------+
| Airtime Balances | 0.68 |
+--------------------------------------------------+
| Airtime Expiry Date | 01-Jan-2037 00:00 |
+--------------------------------------------------+
'''
import urllib.request
import warnings
from bs4 import BeautifulSoup
def main():
''' Entry point '''
warnings.filterwarnings('ignore', category=BytesWarning, module='bs4')
url = 'http://www.safaricom.com/bundles/GetSubDetails'
user_agent = {'User-Agent': 'Mozilla/5.0'}
req = urllib.request.Request(url, headers=user_agent)
html = urllib.request.urlopen(req).read()
soup = BeautifulSoup(html, 'html.parser')
table_rows = soup.find_all('tr')
print('')
for row in table_rows:
if len(row.contents) < 2:
continue
name = row.contents[0].get_text().strip().ljust(25)
value = row.contents[1].get_text().strip().ljust(20)
line = '| {0} | {1} |'.format(name, value)
print(line)
print('+' + '-' * (len(line) - 2) + '+')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment