import json, requests
from refresh import Refresh

refreshCaller = Refresh() 

print('BBC Radio 2 "Live playlist" Sounds of the 60s.')
print('Tony Blackburn, every Saturday, finishes @ 8am.')
print('Updates "Sounds of the 60s" playlist.')
print('The Pysmiths ~ 17-JULY-22.')
print('')

class CopyTracks:
    def __init__(self):
        self.user_id = 'HDDD-1960'              ## Works ok without Spotify ID      ##
        self.live_id = '6QiYdcp66TpiYC95V3kz8Q' ## BBC Radio 2 Live playlist        ##
        self.sots_id = '1S0J3hNQYqzbrby8Y8n1R7' ## Your "Sound of the 60s" playlist ##
        self.spotify_token = ''
        self.tracks = ''

    def get_tracks_from_live(self):
        params={'limit':32}
        query = 'https://api.spotify.com/v1/playlists/{}/tracks'.format(self.live_id)
        response = requests.request('get', url=query, headers={'Content-type': 'application/json','Authorization':'Bearer {}'.format(self.spotify_token)},params=params)
        response_json = response.json()

        for i in response_json['items']:
            self.tracks += i['track']['uri'] + ','
            ## print(i['track']['name'])
            ## print(i['track']['uri'])
        self.tracks = self.tracks[:-1]
        self.add_tracks_to_sots() 
   
    def add_tracks_to_sots(self):
        query = 'https://api.spotify.com/v1/playlists/{}/tracks?uris={}'.format(self.sots_id, self.tracks)
        response = requests.request('put', url=query, headers={'Content-type': 'application/json','Authorization':'Bearer {}'.format(self.spotify_token)})
        print('Done')

    def call_refresh(self):
        refreshCaller = Refresh()
        self.spotify_token = refreshCaller.refresh()
        self.get_tracks_from_live()

a = CopyTracks()
a.call_refresh()
