There's a simple key:value store associated with every repl. To work with it in Python, we first import the db
module from replit
:
from replit import db
Then, we can:
db['my_key'] = my_value
my_variable = db['my_key']
del db['my_key']
list(db.keys())
db.clear()
!!! warning "Access Token"
Fork the repl and copy your access_token
into auth.py
(you'll need to create a new one if it's more than an hour old).
from auth import access_token
access_token
from auth.py
(in which we copied our own token), and can now use access_token
in our subequent codefrom replit import db
db
module from replit
so we can use its methods to work with the databasefrom tracks import get_track_data, track_summary
tracks.py
which we'll use laterdb.clear()
.clear()
method will remove all content (keys and values) from the databasemain.py
without doing so...track_id = '2H7PHVdQ3mXqEHXcvclTB0' #'7azo4rpSUh8nXgtonC6Pkq'
track_data = get_track_data(access_token, track_id)
track_id
(the commented out one is an alternative for you to try), and then call the get_track_data()
function, using access_token
and track_id
as arguments, assigning the result to track_data
get_track_data()
function¶Take a look at tracks.py
; you'll see the function definitions for the two functions we imported from there into main.py
.
get_track_data()
will attempt to collect the track data from the Spotify API as we have seen previously, using the access_token
and track_id
supplied to it
track_data
in main.py
will hold the returned data track_data
value will be None
if track_data:
db[track_id] = track_data
track_data
value is 'truthy' (by this we mean not empty, zero, False
or None
), the following line will be executed, and track_data
will be written to the database, as the value associated with the key of track_id
get_track_data()
returned None
, track_data
would therefore be None
, and the line underneath the if
statement will be skippedprint(list(db.keys()))
print(track_summary(db[track_id]))
we used the .keys()
method to fetch a list of the database keys; the object returned by this method is an iterable, on which we need to use the Python built-in list()
function to get the keys themselves
we called the track_summary()
function, using the value from the database associated with the key equal to track_id
, i.e. the data previously fetched from the API and stored there
print()
the value returned by track_summary()
functiontrack_summary()
function¶We can see what track_summary()
will do by looking at the code in tracks.py
:
summary = {
'track_name': track_data['name'],
'artist_name': track_data['artists'][0]['name'],
'album_name': track_data['album']['name'],
'image_url': track_data['album']['images'][0]['url'],
}
track_data
(which contained everything in the response sent by the Spotify API to our request for the given track), and returns a dictionary We could have extracted these features before writing to the database and then only storing this smaller dataset - but by storing all of the data from the API response, we could change which features we want to use later on, without having to make another request to the API.