Last active
          November 16, 2024 17:14 
        
      - 
      
 - 
        
Save HorlogeSkynet/be9589cfe7e3734d5420aa28d5c6201c to your computer and use it in GitHub Desktop.  
    A simple Python script to fetch and print Internet usage in ÉTS residences (Montréal)
  
        
  
    
      This file contains hidden or 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 python3 | |
| """ | |
| @author : Samuel FORESTIER | |
| @date : created on 30/08/2017, updated on 11/16/2024 | |
| """ | |
| # /// script | |
| # dependencies = [ | |
| # "bs4", | |
| # "requests", | |
| # ] | |
| # /// | |
| # Install requirements : `pip3 install requests bs4` | |
| # Run : `python3 coopTel.py --help` | |
| import argparse | |
| import calendar | |
| import sys | |
| from datetime import datetime | |
| import requests | |
| from bs4 import BeautifulSoup | |
| def parseCommand(): | |
| parser = argparse.ArgumentParser(description='ÉTS Residences Internet ' | |
| 'usage monitor (CoopTel).', | |
| prog='CoopTel-Me (Coop, tell me)') | |
| parser.add_argument('-a', '--apartment', type=int, required=True, | |
| help='Apartment number') | |
| parser.add_argument('-r', '--room', type=str, required=True, | |
| choices=['a', 'b', 'c', 'd'], help='Room letter') | |
| parser.add_argument('-p', '--phase', type=int, required=True, | |
| choices=range(1, 5), help='Residence\'s phase number') | |
| parser.add_argument('-m', '--month', type=int, required=True, | |
| choices=range(1, 13), help='Month number') | |
| parser.add_argument('-v', '--version', action='version', | |
| version='%(prog)s\'s version : 1.5.1') | |
| args = parser.parse_args() | |
| return str(args.apartment), args.room, str(args.phase), args.month | |
| if __name__ == '__main__': | |
| # Fetch parameters from CLI here | |
| apartment, room, phase, month = parseCommand() | |
| # Do the POST request | |
| res = requests.post('http://www2.cooptel.qc.ca/services/temps/index.php', | |
| data={'mois': month, 'cmd': 'visualiser'}, | |
| auth=('ets-res' + phase + '-' + apartment + '-' + | |
| room, 'ets' + apartment)) | |
| if res.status_code != 200: | |
| sys.exit('\nBad response code. Exiting now.\n') | |
| data = BeautifulSoup(res.content, 'html.parser').findAll('table', | |
| {'border': '1'}) | |
| if not data: | |
| sys.exit('\nNo data could be retrieved. Exiting now.\n') | |
| # Consumption per day # | |
| rows = data[0].findAll('tr') | |
| print('\n### Internet usage of the room ' + apartment + '-' | |
| + room.upper() + ' (phase ' + phase + '), for ' | |
| + calendar.month_name[month] + ' ###\n') | |
| # Let's iterate only over the days here... | |
| for row in rows[1:-4]: | |
| values = [i.text.strip() for i in row.findAll('td')] | |
| try: | |
| # Let's change the date format | |
| values[1] = datetime.strptime(values[1], | |
| '%Y-%m-%d').strftime('%d/%m/%Y') | |
| except ValueError: | |
| # When `values[1]` is 'Journée en cours', we get here | |
| values[1] = 'Today' | |
| # Let's add a new value : The total consumed for each day | |
| values.append(str(round(float(values[2]) + float(values[3]), 2))) | |
| print('Socket : {0}\n' | |
| '⌚Date : {1}\n' | |
| '↑ Upload : {2} Mo\n' | |
| '↓ Download : {3} Mo\n' | |
| 'Total of the day : {4} Mo\n' | |
| .format(*values)) | |
| # Consumption for the month # | |
| used = float(rows[-3].findAll('td')[1].text.strip()) | |
| available = float(data[1].findAll('td')[1].text.strip()) | |
| print('\n--> Used this month : ' + | |
| str(round((used / available) * 100, 2)) + ' %\n\n') | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment