-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetPlaylistData.py
77 lines (56 loc) · 1.99 KB
/
getPlaylistData.py
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
# Use deezer-python library
import deezer
# For creating data directory
import os
# For removing special chars in file name
import re
# Specify data directory
dataDirectory = "PlayListData"
# Setup client for making requests
client = deezer.Client()
### Download the specified users playlist data
def downloadPlayListData(publicUserID):
print()
# Get specified user object
publicDeezerUser = client.get_user(publicUserID)
# List user names
print("Deezer Profile Name:")
print(publicDeezerUser)
print()
# Get users public playlists
usersPublicPlaylists = publicDeezerUser.get_playlists()
# Print name of all playlists found
print("Playlists found:")
for playlist in usersPublicPlaylists:
print(playlist.title)
print()
# Create directory to hold playlist data
# Continue if new directory is created
# Stop if directory already exists
if not _createDirectory():
print("Data already exists!")
print("Remove directory '" + dataDirectory + "' to re-download data!")
return
# Loop through each song in each playlist and save to file
for playlist in usersPublicPlaylists:
# Format the playlist title
# (No special characters)
# (Replace space with underscore)
formattedTitle = re.sub(r'\W+', ' ', playlist.title)
formattedTitle = re.sub(r'\s+', '_', formattedTitle)
# Create file (append mode)
playlistFile = open(dataDirectory + "/" + formattedTitle, "a")
print("\nCreating file: " + playlist.title)
# Write the song data to the file
for song in playlist.get_tracks():
playlistFile.write(str(song.id) + ", " + song.title + ", " + song.artist.name + "\n")
print(".", end="")
print()
# Create a directory to hold the playlist data
def _createDirectory():
isExist = os.path.exists(dataDirectory)
if not isExist:
os.makedirs(dataDirectory)
return True
else:
return False