# -*- coding: utf-8 -*-
# Author: Parrot Developers
# License: MPL 2.0 https://www.mozilla.org/en-US/MPL/2.0/

import requests
from ..utils.browser import Firefox
from ..utils.resolve import Resolver
from ..utils.common import randomString

class MazanaTV(Resolver):
    def __init__(self):
        self.firefox = Firefox()
        self.firefox.addHeader("Referer", "https://mazana.tv/")
        # Shitty devs
        self.session = self.login("pass", "pass")

    def login(self, username, password):
        return randomString(32, digits=True, upper=False)

    def grab(self, channel):
        try:
            codecs = requests.get(f"https://soap-001.mazana.tv/stream/resource/{channel}.json.gz", headers=self.firefox.headers).json()["codecs"]
        except:
            return "", {}


        for k, v in codecs.items():
            data = codecs[k]
            if data["codec_state"] != "running": continue
            return data["chunked_uri_http"].format(session=self.session), self.firefox.headers
        return "", {}
        