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

import requests
from ..utils.resolve import Resolver

class TVPass(Resolver):
    def __init__(self):
        self.headers = {"User-Agent": "VLC/3.0.14 LibVLC/3.0.14 (x86_64-w64-mingw32) qt/5.12.8"}

    def grab(self, channel):
        stream_url = f"https://tvpass.org/live/{channel}/hd"

        attempts = 0
        while attempts < 10:
            try:
                resp = requests.get(stream_url, headers=self.headers, allow_redirects=False, timeout=3)
                if resp.status_code in range(300, 400):
                    return resp.headers["Location"], self.headers
                attempts += 1
            except Exception:
                attempts += 1
                
        return "", {}