import requests
import json
import os
from dotenv import load_dotenv

# Load information for owencloud and telegram
load_dotenv()
#Get Token for OwenCloud
def get_token():
    login = os.getenv("LOGIN")
    password = os.getenv("PASSWORD")
    if not login or not password:
        raise ValueError("Логин или пароль не заданы в .env")

    data = {'login': login, 'password': password}
    headers = {
        "Content-Type": "application/json",
        "Accept": "application/json"
    }
    req = requests.post("https://api.owencloud.ru/v1/auth/open/", json=data, headers=headers)
    if req.status_code == 200:
        answer = req.json()
        token = "Bearer " + answer["token"]
        return token
    else:
        print(f"Ошибка авторизации: {req.status_code}, {req.text}")
        return None
#Device parsing
def main():
    token = get_token()
    if not token:
        print("Не удалось получить токен. Завершаем работу.")
        return

    device_id = "296613"
    headers = {'Authorization': token}
    data = json.dumps({'filter': ''})

    req = requests.post("https://api.owencloud.ru/v1/device/index/", data=data, headers=headers)

    if req.status_code != 200:
        print(f"Ошибка запроса устройства: {req.status_code}, {req.text}")
        return

    nswer = req.json()


    print(req.text)


if __name__ == "__main__":
    main()
