Last active
October 30, 2019 16:31
-
-
Save bertrandmartel/71825d8be746e482b776a977e0f38e53 to your computer and use it in GitHub Desktop.
Conso mobile depuis https://bouyguestelecom.fr en Python3
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
import requests | |
from bs4 import BeautifulSoup | |
import re | |
import base64 | |
import json | |
import sys | |
from random import choice | |
from string import ascii_uppercase | |
from urllib.parse import urlsplit, parse_qs | |
import jwt | |
username="0123456789" | |
password="your password" | |
base_url="https://www.mon-compte.bouyguestelecom.fr" | |
oauth2_url="https://oauth2.bouyguestelecom.fr" | |
api_url="https://api.bouyguestelecom.fr" | |
session = requests.Session() | |
r = session.get("{}/cas/login".format(base_url)) | |
soup = BeautifulSoup(r.text, "html.parser") | |
payload = dict([ | |
(t['name'],t.get('value','')) | |
for t in soup.select("input") | |
if t.has_attr('name') | |
]) | |
payload['username'] = username | |
payload['password'] = password | |
action = soup.select("form")[0].get('action') | |
r = session.post("{}{}".format(base_url, action), data = payload) | |
soup = BeautifulSoup(r.text, "html.parser") | |
if "SSOID" in session.cookies.get_dict(): | |
print("user is authenticated") | |
else: | |
print("authentication failed") | |
sys.exit() | |
r = session.get('{}/authorize'.format(oauth2_url), | |
params = { | |
"nonce": ''.join(choice(ascii_uppercase) for i in range(32)), | |
"state": ''.join(choice(ascii_uppercase) for i in range(32)), | |
"client_id": 'a360.bouyguestelecom.fr', | |
"redirect_uri": "https://www.bouyguestelecom.fr/mon-compte/", | |
"response_type": "id_token token" | |
}) | |
fragment = urlsplit(r.url).fragment | |
params = parse_qs(fragment) | |
token = jwt.decode(params['id_token'][0], verify=False) | |
id_bytel = token["sub"] | |
access_token = params["access_token"][0] | |
print("id bytel : {}".format(id_bytel)) | |
r = requests.get("{}/personnes/{}/contrats-signes".format(api_url, id_bytel), headers={ | |
"Authorization": "Bearer {}".format(access_token) | |
}) | |
for item in r.json()["items"]: | |
print(item["typeLigne"]) | |
print(item["numeroTel"]) | |
print(item["_links"]["suiviConsoMobile"]["href"]) | |
r = requests.get("{}{}".format(api_url, item["_links"]["suiviConsoMobile"]["href"]), headers={ | |
"Authorization": "Bearer {}".format(access_token) | |
}) | |
result = r.json() | |
print(result) | |
if (("compteursConsoDATA" in result) and len(result["compteursConsoDATA"])>0): | |
consumed = result["compteursConsoDATA"][0]["quantiteConsommee"] | |
total = result["compteursConsoDATA"][0]["quantiteTotale"] | |
print("quantiteConsommee : {}".format(consumed)) | |
print("quantiteTotale: {}".format(total)) | |
print("quantiteConsommee in Go: {}".format(round(consumed/1024/1024/1024*100)*100)) | |
print("quantiteTotale in Go: {}".format(round(total/1024/1024/1024*100)*100)) | |
print("---------------") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment