objectified network and playlist

This commit is contained in:
aj 2019-04-30 16:10:41 +01:00
parent 804b0b607e
commit d4b39fe9f2
8 changed files with 120 additions and 109 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
env
__pycache__
*.csv
.idea

View File

@ -1,14 +1,13 @@
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 sys, datetime, os
if __name__ == '__main__':
user = userclass.User()
playlists = playlist.getUserPlaylists(user)
network = networkclass.network(userclass.User())
playlists = network.getUserPlaylists()
path = sys.argv[1]
@ -20,5 +19,5 @@ if __name__ == '__main__':
os.makedirs(totalpath)
for play in playlists:
csvwrite.exportPlaylist(user, play['id'], play['name'], totalpath)
print(play['name'] + ' exported')
csvwrite.exportPlaylist(play, totalpath)
print(play.name + ' exported')

30
main.py
View File

@ -1,32 +1,10 @@
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__':
print('hello world')
user = userclass.User()
network = networkclass.network(userclass.User())
#playlists = playlist.getPlaylists(user)
#for playlister in playlists:
#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)
network.getPlaylist('000Eh2vXzYGgrEFlgcWZj3')

View File

@ -1,21 +1,24 @@
import csv
import spotframework.net.playlist as playlistpull
import spotframework.net.network as network
import datetime
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())
if name is None:
name = playlist.name
with open('{}/{}_{}.csv'.format(path, name.replace('/', '_'), date.split('.')[0]), 'w') as fileobj:
writer = csv.DictWriter(fileobj, fieldnames = headers)
writer.writeheader()
for track in playlist:
for track in playlist.tracks:
trackdict = {
'name':track['track']['name'],

View File

View 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

View 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))

View File

@ -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))