import requests
import json


headers = {
    'authority': 'graphql.ott.sky.com',
    'accept': '*/*',
    'accept-language': 'en-GB,en;q=0.9,en-US;q=0.8',
    'dnt': '1',
    'origin': 'https://www.nowtv.it',
    'referer': 'https://www.nowtv.it/',
    'sec-ch-ua': '"Not_A Brand";v="8", "Chromium";v="120", "Microsoft Edge";v="120"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
    'sec-fetch-dest': 'empty',
    'sec-fetch-mode': 'cors',
    'sec-fetch-site': 'cross-site',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0',
    'x-skyott-device': 'COMPUTER',
    'x-skyott-language': 'it',
    'x-skyott-platform': 'PC',
    'x-skyott-proposition': 'NOWTV',
    'x-skyott-provider': 'NOWTV',
    'x-skyott-territory': 'IT',
}

response = requests.get('https://graphql.ott.sky.com/graphql?extensions=%7B%22persistedQuery%22:%7B%22version%22:1,%22sha256Hash%22:%22356b08a537f119347994744194e5c42f61fba5ccf5ffbd31bd25af626a2043b5%22%7D%7D&variables=%7B%22sectionNavigation%22:%22CINEMA%22,%22formatType%22:%22SD%22,%22defaultFormat%22:%22SD%22%7D', headers=headers)
jsondata = response.json()
collection = list()
for i, channel in enumerate(jsondata['data']['linearChannels']):
    channeljson = {
        'id': channel['name'],
        'channel_id': channel['id'],
        'channel_name': channel['name'],
        'channel_logo': channel['logos'][0]['template'].format(width=100, height=100),
        'live': {},
        'next': {},
    }
    channeljson['live']['title'] = channel['now']['title']
    channeljson['live']['imageUrl'] = channel['now']['imageUrl'][:-4] if channel['now']['imageUrl'] else channel['now']['imageUrl']
    channeljson['next']['title'] = channel['next']['title']
    channeljson['next']['imageUrl'] = channel['next']['imageUrl'][:-4] if channel['next']['imageUrl'] else channel['next']['imageUrl']
    collection.append(channeljson)

print()

with open('cinema.json', 'w+') as f:
    json.dump(collection, f, indent=2)