import json, requests
from secrets import spotify_user_id
from refresh import Refresh

class PlaylistGrab:
    def __init__(self):
        self.user_id = spotify_user_id
        self.spotify_token = ''
        self.playlist_id = ''
        
    def get_the_playlists(self):
        params={'limit':30}
        query = 'https://api.spotify.com/v1/users/{}/playlists'.format(self.user_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.name = (i['name'])
            ## self.playlist_id = (i['id'])
            self.total = (i['tracks']['total'])
            check = (self.name[:4])
            ## print(check)
            if check == 'RADI':
                print('[04] Tracks in ' + str(self.name) + ' =  ' + str(self.total))
                with open('tracks_tot.txt', 'w') as file:
                    file.write('%d' % self.total)

            elif check == 'FEED':
                print('     Tracks in ' + str(self.name) + ' = ' + str(self.total))
                with open('feed_tot.txt', 'w') as file:
                    file.write('%d' % self.total)
                ## print('')
                
    def call_refresh(self):
        refreshCaller = Refresh()
        self.spotify_token = refreshCaller.refresh()
        self.get_the_playlists()
        
a = PlaylistGrab()
a.call_refresh()
