from flask import Blueprint, Response
import requests
import base64
import json

allsport_bp = Blueprint('allsport', __name__)

@allsport_bp.route('/allsport')
def allsportplaylist():
    headers = {
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:126.0) Gecko/20100101 Firefox/126.0',
        'Authorization': 'Bearer terimkc-bsdk-mhdtvworld-69gay420bsdk69gay420bsdk69copycat420bsdk69gay69terimkc',
    }

    response = requests.get(
        'https://allsport.games/api/hi-chutiye-mhdtvworld-gay-teri-mkc-ab-to-copy-mat-kar-bsdk-chutiye',
        headers=headers,
    ).json()

    channels = base64.b64decode(response["data"]).decode("utf8")

    channelsarr = json.loads(channels)

    m3uplaylist = '#EXTM3U\n\n'

    for channel in channelsarr:
        if channel['license_key'] == '':
            m3uplaylist += f'#EXTINF:-1,{channel["name"]}\n{channel["url"]}\n\n'
        else:
            m3uplaylist += f'#EXTINF:-1,{channel["name"]}\n#KODIPROP:inputstream.adaptive.license_type=clearkey\n#KODIPROP:inputstream.adaptive.license_key={channel["license_key"]}:{channel["license_key2"]}\n{channel["url"]}\n\n'

    return Response(m3uplaylist, mimetype='audio/x-mpegurl')
