summaryrefslogtreecommitdiff
path: root/getspot.py
blob: 13d832406c9c633c4ec944d20bd36f9fa7b6570b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Apr  3 22:42:36 2021

@author: fish

Try to get and parse spotify playlists

spotify:playlist:7hGGMcWOPgXj2qFw5DQyXP - likes_2021
spotify:playlist:2etpNxusBBUoXoxb3BOsF3 - likes_2020


Todo:
----
 - extract the wanted info (track name, artist(s), picture url,
                            track URI, etc) more elegantly
 - handle multiple artists
 - access token timeout? check if check is needed
 - find alternative to the kludge to prevent skipping first list

Done:
----
 2021-04-04: handle longer playlists

Outline:
-------
     - get playlist
     - extract playlist metadata
     - create playlist dictionary
     (---)
     - extract tracks & store
     - check if done, else iterate over next set of tracks
     (---)
     - store completed list, clean up.


Playlist format:
---------------
 Playlist = {'name': '',
             'image': '',
             'uri': '',
             'tracks': [
                 {'artist': '',
                  'name': ''
                  },
                 {'artist': ''
                  'name': ''
                  }
                 ]
             }

"""

import json
import requests

# GLOBALS

# kludge to avoid storing secrets in git repo
with open('spotifyids') as f:
    CLIENT_ID, CLIENT_SECRET = f.readline().rstrip().split(',')

AUTH_URL = 'https://accounts.spotify.com/api/token'
BASE_URL = 'https://api.spotify.com/v1/'
# base URL of all Spotify API endpoints

PLAYLIST_ID = '37i9dQZF1EMdXVibVNv347'  # temp


def get_auth_response(client_id=CLIENT_ID, client_secret=CLIENT_SECRET):
    """
    Get the auth response from the spotify servers, with token and timeout info
    """
    auth_response = requests.post(AUTH_URL, {
        'grant_type': 'client_credentials',
        'client_id': client_id,
        'client_secret': client_secret,
    })

    if not auth_response.ok:
        raise SystemExit('Response from server not OK:\n %s'
                         % auth_response.text)

    return auth_response.json()


def get_access_token():
    """
    Get the temporary access token from the Spotify servers
    (POST request)
    """

    auth_response_data = get_auth_response()

    # save the access token
    access_token = auth_response_data['access_token']
    return access_token, auth_response_data


def main():
    """ main function """
    token, auth_response = get_access_token()
    print(token)
    print("Token expires in {expires} seconds.".format(
        expires=auth_response['expires_in']))


if __name__ == '__main__':
    main()