objectified network and playlist
This commit is contained in:
parent
804b0b607e
commit
d4b39fe9f2
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,3 +1,4 @@
|
|||||||
env
|
env
|
||||||
__pycache__
|
__pycache__
|
||||||
*.csv
|
*.csv
|
||||||
|
.idea
|
11
backup.py
11
backup.py
@ -1,14 +1,13 @@
|
|||||||
import spotframework.net.user as userclass
|
import spotframework.net.user as userclass
|
||||||
import spotframework.net.playlist as playlist
|
import spotframework.net.network as networkclass
|
||||||
import spotframework.io.csv as csvwrite
|
import spotframework.io.csv as csvwrite
|
||||||
|
|
||||||
import sys, datetime, os
|
import sys, datetime, os
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
||||||
user = userclass.User()
|
network = networkclass.network(userclass.User())
|
||||||
|
playlists = network.getUserPlaylists()
|
||||||
playlists = playlist.getUserPlaylists(user)
|
|
||||||
|
|
||||||
path = sys.argv[1]
|
path = sys.argv[1]
|
||||||
|
|
||||||
@ -20,5 +19,5 @@ if __name__ == '__main__':
|
|||||||
os.makedirs(totalpath)
|
os.makedirs(totalpath)
|
||||||
|
|
||||||
for play in playlists:
|
for play in playlists:
|
||||||
csvwrite.exportPlaylist(user, play['id'], play['name'], totalpath)
|
csvwrite.exportPlaylist(play, totalpath)
|
||||||
print(play['name'] + ' exported')
|
print(play.name + ' exported')
|
||||||
|
30
main.py
30
main.py
@ -1,32 +1,10 @@
|
|||||||
import spotframework.net.user as userclass
|
import spotframework.net.user as userclass
|
||||||
import spotframework.net.playlist as playlist
|
import spotframework.net.network as networkclass
|
||||||
|
import spotframework.net.network as playlist
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
print('hello world')
|
print('hello world')
|
||||||
|
|
||||||
user = userclass.User()
|
|
||||||
|
|
||||||
#playlists = playlist.getPlaylists(user)
|
network = networkclass.network(userclass.User())
|
||||||
|
|
||||||
#for playlister in playlists:
|
network.getPlaylist('000Eh2vXzYGgrEFlgcWZj3')
|
||||||
#print(playlister['name'])
|
|
||||||
|
|
||||||
#print(playlists[0])
|
|
||||||
#print(len(playlists))
|
|
||||||
|
|
||||||
#print(user.username)
|
|
||||||
|
|
||||||
#moreplaylists = playlist.getUserPlaylists(user)
|
|
||||||
#print(len(moreplaylists))
|
|
||||||
|
|
||||||
#tracks = playlist.getPlaylistTracks(user, '000Eh2vXzYGgrEFlgcWZj3')
|
|
||||||
|
|
||||||
#print(tracks[0])
|
|
||||||
|
|
||||||
#print(len(tracks))
|
|
||||||
|
|
||||||
import spotframework.io.csv as csvwrite
|
|
||||||
|
|
||||||
csvwrite.exportPlaylist(user, '000Eh2vXzYGgrEFlgcWZj3', 'february', '')
|
|
||||||
|
|
||||||
print(user.access_token)
|
|
||||||
|
@ -1,21 +1,24 @@
|
|||||||
import csv
|
import csv
|
||||||
import spotframework.net.playlist as playlistpull
|
import spotframework.net.network as network
|
||||||
import datetime
|
import datetime
|
||||||
|
|
||||||
headers = ['name', 'artist', 'album', 'album artist', 'added', 'track id', 'album id', 'added by']
|
headers = ['name', 'artist', 'album', 'album artist', 'added', 'track id', 'album id', 'added by']
|
||||||
|
|
||||||
def exportPlaylist(user, playlistid, name, path):
|
def exportPlaylist(playlist, path, name=None):
|
||||||
|
|
||||||
playlist = playlistpull.getPlaylistTracks(user, playlistid)
|
#playlist = network.getPlaylistTracks(user, playlistid)
|
||||||
|
|
||||||
date = str(datetime.datetime.now())
|
date = str(datetime.datetime.now())
|
||||||
|
|
||||||
|
if name is None:
|
||||||
|
name = playlist.name
|
||||||
|
|
||||||
with open('{}/{}_{}.csv'.format(path, name.replace('/', '_'), date.split('.')[0]), 'w') as fileobj:
|
with open('{}/{}_{}.csv'.format(path, name.replace('/', '_'), date.split('.')[0]), 'w') as fileobj:
|
||||||
|
|
||||||
writer = csv.DictWriter(fileobj, fieldnames = headers)
|
writer = csv.DictWriter(fileobj, fieldnames = headers)
|
||||||
writer.writeheader()
|
writer.writeheader()
|
||||||
|
|
||||||
for track in playlist:
|
for track in playlist.tracks:
|
||||||
|
|
||||||
trackdict = {
|
trackdict = {
|
||||||
'name':track['track']['name'],
|
'name':track['track']['name'],
|
||||||
|
0
spotframework/model/__init__.py
Normal file
0
spotframework/model/__init__.py
Normal file
9
spotframework/model/playlist.py
Normal file
9
spotframework/model/playlist.py
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
|
||||||
|
|
||||||
|
class playlist:
|
||||||
|
|
||||||
|
def __init__(self, playlistid, name=None, userid=None):
|
||||||
|
self.tracks = []
|
||||||
|
self.name = name
|
||||||
|
self.playlistid = playlistid
|
||||||
|
self.userid = userid
|
94
spotframework/net/network.py
Normal file
94
spotframework/net/network.py
Normal file
@ -0,0 +1,94 @@
|
|||||||
|
import requests
|
||||||
|
from . import const
|
||||||
|
from spotframework.model.playlist import playlist as playlistclass
|
||||||
|
|
||||||
|
limit = 50
|
||||||
|
|
||||||
|
|
||||||
|
class network:
|
||||||
|
|
||||||
|
def __init__(self, user):
|
||||||
|
self.user = user
|
||||||
|
|
||||||
|
def getPlaylist(self, playlistid, tracksonly=False):
|
||||||
|
print('getting ' + playlistid)
|
||||||
|
|
||||||
|
tracks = self.getPlaylistTracks(playlistid)
|
||||||
|
|
||||||
|
playlist = playlistclass(playlistid)
|
||||||
|
playlist.tracks += tracks
|
||||||
|
|
||||||
|
if not tracksonly:
|
||||||
|
pass
|
||||||
|
|
||||||
|
return playlist
|
||||||
|
|
||||||
|
def getPlaylists(self, offset=0):
|
||||||
|
print('getting user playlists')
|
||||||
|
|
||||||
|
headers = {'Authorization': 'Bearer ' + self.user.access_token}
|
||||||
|
|
||||||
|
playlists = []
|
||||||
|
|
||||||
|
params = {'offset': offset, 'limit': limit}
|
||||||
|
req = requests.get(const.api_url + 'me/playlists', params=params, headers=headers)
|
||||||
|
|
||||||
|
#print(req.text)
|
||||||
|
|
||||||
|
if req.status_code == 200:
|
||||||
|
|
||||||
|
#print(req.text)
|
||||||
|
|
||||||
|
resp = req.json()
|
||||||
|
|
||||||
|
for responseplaylist in resp['items']:
|
||||||
|
|
||||||
|
playlist = self.getPlaylist(responseplaylist['id'], tracksonly=True)
|
||||||
|
playlist.name = responseplaylist['name']
|
||||||
|
playlist.userid = responseplaylist['owner']['id']
|
||||||
|
|
||||||
|
playlists.append(playlist)
|
||||||
|
|
||||||
|
#playlists = playlists + resp['items']
|
||||||
|
|
||||||
|
if resp['next']:
|
||||||
|
playlists += self.getPlaylists(offset + limit)
|
||||||
|
|
||||||
|
#print(req.text)
|
||||||
|
|
||||||
|
return playlists
|
||||||
|
|
||||||
|
else:
|
||||||
|
return None
|
||||||
|
|
||||||
|
def getUserPlaylists(self):
|
||||||
|
|
||||||
|
return list(filter(lambda x: x.userid == self.user.username, self.getPlaylists()))
|
||||||
|
|
||||||
|
def getPlaylistTracks(self, playlistid, offset=0):
|
||||||
|
|
||||||
|
headers = {'Authorization': 'Bearer ' + self.user.access_token}
|
||||||
|
|
||||||
|
tracks = []
|
||||||
|
|
||||||
|
params = {'offset': offset, 'limit': limit}
|
||||||
|
req = requests.get(const.api_url + 'playlists/{}/tracks'.format(playlistid), params=params, headers=headers)
|
||||||
|
|
||||||
|
#print(req.text)
|
||||||
|
|
||||||
|
if req.status_code == 200:
|
||||||
|
|
||||||
|
#print(req.text)
|
||||||
|
resp = req.json()
|
||||||
|
|
||||||
|
tracks += resp['items']
|
||||||
|
|
||||||
|
if resp['next']:
|
||||||
|
tracks += self.getPlaylistTracks(playlistid, offset + limit)
|
||||||
|
|
||||||
|
#print(req.text)
|
||||||
|
|
||||||
|
return tracks
|
||||||
|
|
||||||
|
else:
|
||||||
|
raise ValueError("Couldn't Pull Playlist " + str(playlistid) + ' ' + str(req.status_code))
|
@ -1,73 +0,0 @@
|
|||||||
import requests
|
|
||||||
from . import const
|
|
||||||
|
|
||||||
limit = 50
|
|
||||||
|
|
||||||
def getPlaylists(user, offset = 0):
|
|
||||||
|
|
||||||
headers = {'Authorization': 'Bearer ' + user.access_token}
|
|
||||||
|
|
||||||
playlists = []
|
|
||||||
|
|
||||||
params = {'offset': offset, 'limit': limit}
|
|
||||||
req = requests.get(const.api_url + 'me/playlists', params = params, headers = headers)
|
|
||||||
|
|
||||||
#print(req.text)
|
|
||||||
|
|
||||||
if req.status_code == 200:
|
|
||||||
|
|
||||||
#print(req.text)
|
|
||||||
|
|
||||||
resp = req.json()
|
|
||||||
|
|
||||||
playlists = playlists + resp['items']
|
|
||||||
|
|
||||||
if resp['next']:
|
|
||||||
playlists += getPlaylists(user, offset + limit)
|
|
||||||
|
|
||||||
#print(req.text)
|
|
||||||
|
|
||||||
return playlists
|
|
||||||
|
|
||||||
else:
|
|
||||||
return None
|
|
||||||
|
|
||||||
def getUserPlaylists(user):
|
|
||||||
|
|
||||||
playlists = getPlaylists(user)
|
|
||||||
|
|
||||||
returnlist = []
|
|
||||||
|
|
||||||
for playlist in playlists:
|
|
||||||
if playlist['owner']['id'] == user.username:
|
|
||||||
returnlist.append(playlist)
|
|
||||||
|
|
||||||
return returnlist
|
|
||||||
|
|
||||||
def getPlaylistTracks(user, playlistid, offset = 0):
|
|
||||||
|
|
||||||
headers = {'Authorization': 'Bearer ' + user.access_token}
|
|
||||||
|
|
||||||
tracks = []
|
|
||||||
|
|
||||||
params = {'offset': offset, 'limit': limit}
|
|
||||||
req = requests.get(const.api_url + 'playlists/{}/tracks'.format(playlistid), params = params, headers = headers)
|
|
||||||
|
|
||||||
#print(req.text)
|
|
||||||
|
|
||||||
if req.status_code == 200:
|
|
||||||
|
|
||||||
#print(req.text)
|
|
||||||
resp = req.json()
|
|
||||||
|
|
||||||
tracks += resp['items']
|
|
||||||
|
|
||||||
if resp['next']:
|
|
||||||
tracks += getPlaylistTracks(user, playlistid, offset + limit)
|
|
||||||
|
|
||||||
#print(req.text)
|
|
||||||
|
|
||||||
return tracks
|
|
||||||
|
|
||||||
else:
|
|
||||||
raise ValueError("Couldn't Pull Playlist " + str(playlistid) + ' ' + str(req.status_code))
|
|
Loading…
Reference in New Issue
Block a user