diff options
author | Roger Gonzalez <roger@rogs.me> | 2023-08-17 09:29:25 -0300 |
---|---|---|
committer | Roger Gonzalez <roger@rogs.me> | 2023-08-17 09:29:25 -0300 |
commit | 5b8f75fb03aee5f6ba90b939fa1a93837509a547 (patch) | |
tree | 0bf2276b4de29a744e10a6c46f2794328444be8f | |
parent | fc1ac46c786aba12eab6dca4bfa1c48a45d0c93d (diff) |
Added methods for getting the account_id and account_info, refactored
make_request
-rw-r--r-- | .env.example | 1 | ||||
-rwxr-xr-x | script.py | 79 | ||||
-rw-r--r-- | utils.py | 42 |
3 files changed, 77 insertions, 45 deletions
diff --git a/.env.example b/.env.example index 8979a7c..8984ed0 100644 --- a/.env.example +++ b/.env.example @@ -1,3 +1,2 @@ -DEVICE_ID=12345 EMAIL=your@email.com PHONE_NUMBER=598123456 @@ -1,76 +1,61 @@ import os from datetime import datetime, timedelta from time import sleep -from typing import Optional +from typing import List, Optional -import requests from dotenv import load_dotenv -from utils import get_average_price +from utils import get_average_price, make_request load_dotenv() -DEVICE_ID = os.environ.get("DEVICE_ID") -EMAIL = os.environ.get("EMAIL") -PHONE_NUMBER = os.environ.get("PHONE_NUMBER") - -def make_request(method: str, url: str, authorization: str = None, data: Optional[dict] = None) -> requests.Response: +def login(email: str, phone_number: str) -> str: """ - Make a HTTP request + Login to UTE Args: - method (str): The HTTP method to use. Accepted methods are ``GET``, ``POST``. - url (str): The URL to use for the request. - authorization (str): Authorization token - data (dict): The data to send in the body of the request. + email (str): User email for authentication + phone_number (str): User phone number for authentication Returns: - requests.Response: The response object. - - Raises: - Exception: If the method is not supported. + str: Authorization token """ - headers = { - "X-Client-Type": "Android", - "User-Agent": "okhttp/3.8.1", - "Content-Type": "application/json; charset=utf-8", - "Connection": "Keep-Alive", - "User-Agent": "okhttp/3.8.1", + url = "https://rocme.ute.com.uy/api/v1/token" + data = { + "Email": email, + "PhoneNumber": phone_number, } - if authorization: - headers["Authorization"] = f"Bearer {authorization}" + return make_request("POST", url, data=data).text - if method == "GET": - return requests.get(url, headers=headers) - if method == "POST": - return requests.post(url, headers=headers, json=data) +def get_ute_device_list() -> List[dict]: + """ + Get UTE device list - raise Exception("Method not supported") + Returns: + List[dict]: List of devices + """ + return make_request("GET", "https://rocme.ute.com.uy/api/v1/accounts", authorization=authorization).json()["data"] -def login(email: str, phone_number: str) -> str: + +def get_ute_account_info(device_id: str) -> dict: """ - Login to UTE + Get UTE account info from device id Args: - email (str): User email for authentication - phone_number (str): User phone number for authentication + device_id (str): UTE Device id Returns: - str: Authorization token + dict: UTE info """ - url = "https://rocme.ute.com.uy/api/v1/token" - data = { - "Email": email, - "PhoneNumber": phone_number, - } - - return make_request("POST", url, data=data).text + return make_request( + "GET", f"https://rocme.ute.com.uy/api/v1/accounts/{device_id}", authorization=authorization + ).json()["data"] def get_ute_historic_info( @@ -191,8 +176,14 @@ def get_current_usage_info(device_id: str, authorization: str) -> dict: if __name__ == "__main__": date_start = "2023-08-01" date_end = "2023-08-31" + EMAIL = os.environ.get("EMAIL") + PHONE_NUMBER = os.environ.get("PHONE_NUMBER") + average_price = get_average_price("triple") authorization = login(EMAIL, PHONE_NUMBER) + device_list = get_ute_device_list() + device_id = device_list[0]["accountServicePointId"] + account_info = get_ute_account_info(device_id) - ute_historic_usage = get_ute_historic_info(DEVICE_ID, authorization, average_price, date_start, date_end) - ute_current_usage = get_current_usage_info(DEVICE_ID, authorization) + ute_historic_usage = get_ute_historic_info(device_id, authorization, average_price, date_start, date_end) + ute_current_usage = get_current_usage_info(device_id, authorization) @@ -1,3 +1,45 @@ +from typing import Optional + +import requests + + +def make_request(method: str, url: str, authorization: str = None, data: Optional[dict] = None) -> requests.Response: + """ + Make a HTTP request + + Args: + method (str): The HTTP method to use. Accepted methods are ``GET``, ``POST``. + url (str): The URL to use for the request. + authorization (str): Authorization token + data (dict): The data to send in the body of the request. + + Returns: + requests.Response: The response object. + + Raises: + Exception: If the method is not supported. + """ + + headers = { + "X-Client-Type": "Android", + "User-Agent": "okhttp/3.8.1", + "Content-Type": "application/json; charset=utf-8", + "Connection": "Keep-Alive", + "User-Agent": "okhttp/3.8.1", + } + + if authorization: + headers["Authorization"] = f"Bearer {authorization}" + + if method == "GET": + return requests.get(url, headers=headers) + + if method == "POST": + return requests.post(url, headers=headers, json=data) + + raise Exception("Method not supported") + + def get_average_price(plan: str) -> float: """ Get the average price for a plan |