# -*- coding: utf-8 -*-
"""
Created on January 2023
@author: Enrique Orduña-Malea
"""

import requests
import json
import time
import glob
import os
def connect_to_endpoint(broadcaster_id, after):  
    head = {
    'Client-ID' : Client_ID,
    'Authorization' :  "Bearer " + access_token
    }
    
    # replace appropriate start and end times below
    if (after is not None):
        url = "https://api.twitch.tv/helix/clips?first={}&broadcaster_id={}&after={}".format(first, broadcaster_id, after)
    else:
        url = "https://api.twitch.tv/helix/clips?first={}&broadcaster_id={}".format(first, broadcaster_id)
    
    time.sleep(1)
    response = requests.get(url, headers=head)
    
    if response.status_code == 429:
        time.sleep(10)
        print('waiting')
    if response.status_code != 200 and response.status_code != 429:
        raise Exception(response.status_code, response.text)
    
    return response.json()

origin= os.getcwd() + "\\1.json"
destination = os.getcwd() + "\\final.json"
count = 1
page = 0
flag = True
# Replace with your own bearer token from your App created
access_token= "insert your access token"
Client_ID= "insert your client-ID"

# add your customised query; game_id and broadcaster_id are incompatible: one or another
#game_id = xxxx
#broadcaster_id = xxx
first= 100
after=None

# first iteration obtaining the first page of results and getting the next token from results
while flag:
    json_response = connect_to_endpoint(broadcaster_id, after)
    p1 = json_response['data']
    with open(origin, 'w', newline='', encoding='utf-8') as f1:
        json.dump(p1, f1, indent=4)
    f1.close()
    result_count = json_response['pagination']['cursor']
    for serp in json_response['data']:
        page= page+1
        print(page)
    flag = False

# all the remaining iterations, until no 'next token' exists    
while 'cursor' in json_response['pagination']:
    after = json_response['pagination']['cursor']
    after =str(after)
    print(after)
    if result_count is not None and after is not None:
        time.sleep(1)
        json_response = connect_to_endpoint(broadcaster_id, after)
        for serp in json_response['data']:
            page= page+1
            print(page)
        p2 = json_response['data']
        count=count+1
        address= os.getcwd()+"\\"+str(count)+".json"
        with open(address, 'a+', newline='', encoding='utf-8') as f2:
            json.dump(p2, f2, indent=4)
        f2.close()

    else:
        print('Iteration finished')

# joining json obtained into one file in final.csv
glob_data = []
for file in glob.glob(os.getcwd()+'\\*.json'):
    with open(file) as json_file:
        data = json.load(json_file)

        i = 0
        while i < len(data):
            glob_data.append(data[i])
            i += 1
with open(destination, 'w') as f:
    json.dump(glob_data, f, indent=4)
    
print(f'Total records saved: {page}')
