pep 8 code style
This commit is contained in:
parent
cc41e949a9
commit
35633f1396
24
alarm.py
24
alarm.py
@ -1,11 +1,12 @@
|
||||
import spotframework.net.user as userclass
|
||||
import spotframework.net.network as networkclass
|
||||
from spotframework.net.user import User
|
||||
from spotframework.net.network import Network
|
||||
import spotframework.log.log as log
|
||||
|
||||
import os
|
||||
import datetime
|
||||
|
||||
def checkPhone():
|
||||
|
||||
def check_phone():
|
||||
|
||||
response = os.system("ping -c 1 -w5 " + os.environ['PHONEIP'] + " > /dev/null 2>&1")
|
||||
log.log('checking for phone')
|
||||
@ -14,16 +15,17 @@ def checkPhone():
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
try:
|
||||
|
||||
network = networkclass.network(userclass.User())
|
||||
network = Network(User())
|
||||
|
||||
found = False
|
||||
|
||||
for i in range(0, 36):
|
||||
if checkPhone():
|
||||
if check_phone():
|
||||
found = True
|
||||
break
|
||||
|
||||
@ -31,17 +33,17 @@ if __name__ == '__main__':
|
||||
|
||||
date = datetime.datetime.now()
|
||||
|
||||
playlists = network.getUserPlaylists()
|
||||
playlists = network.get_user_playlists()
|
||||
|
||||
playlisturi = next((i.uri for i in playlists if i.name == date.strftime("%B %-y").lower()), os.environ['SPOTALARMURI'])
|
||||
|
||||
network.play(playlisturi, network.getDeviceID(os.environ['SPOTALARMDEVICENAME']))
|
||||
network.play(playlisturi, network.get_device_id(os.environ['SPOTALARMDEVICENAME']))
|
||||
|
||||
network.setShuffle(True)
|
||||
network.setVolume(os.environ['SPOTALARMVOLUME'])
|
||||
network.set_shuffle(True)
|
||||
network.set_volume(os.environ['SPOTALARMVOLUME'])
|
||||
network.next()
|
||||
|
||||
log.dumpLog()
|
||||
log.dump_log()
|
||||
|
||||
except:
|
||||
log.dumpLog()
|
||||
log.dump_log()
|
||||
|
16
backup.py
16
backup.py
@ -1,5 +1,5 @@
|
||||
import spotframework.net.user as userclass
|
||||
import spotframework.net.network as networkclass
|
||||
from spotframework.net.user import User
|
||||
from spotframework.net.network import Network
|
||||
import spotframework.io.csv as csvwrite
|
||||
import spotframework.log.log as log
|
||||
|
||||
@ -11,11 +11,11 @@ if __name__ == '__main__':
|
||||
|
||||
try:
|
||||
|
||||
network = networkclass.network(userclass.User())
|
||||
playlists = network.getUserPlaylists()
|
||||
network = Network(User())
|
||||
playlists = network.get_user_playlists()
|
||||
|
||||
for playlist in playlists:
|
||||
playlist.tracks = network.getPlaylistTracks(playlist.playlistid)
|
||||
playlist.tracks = network.get_playlist_tracks(playlist.playlistid)
|
||||
|
||||
path = sys.argv[1]
|
||||
|
||||
@ -27,9 +27,9 @@ if __name__ == '__main__':
|
||||
os.makedirs(totalpath)
|
||||
|
||||
for play in playlists:
|
||||
csvwrite.exportPlaylist(play, totalpath)
|
||||
csvwrite.export_playlist(play, totalpath)
|
||||
|
||||
log.dumpLog()
|
||||
log.dump_log()
|
||||
|
||||
except:
|
||||
log.dumpLog()
|
||||
log.dump_log()
|
||||
|
@ -1,6 +1,6 @@
|
||||
import spotframework.net.const as const
|
||||
import spotframework.net.network as network
|
||||
import spotframework.net.user as user
|
||||
from spotframework.net.network import Network
|
||||
from spotframework.net.user import User
|
||||
import spotframework.log.log as log
|
||||
import spotframework.io.json as json
|
||||
import spotframework.util.monthstrings as monthstrings
|
||||
@ -21,9 +21,9 @@ if __name__ == '__main__':
|
||||
try:
|
||||
if os.path.exists(os.path.join(const.config_path, 'playlists.json')):
|
||||
|
||||
data = json.loadJson(os.path.join(const.config_path, 'playlists.json'))
|
||||
data = json.load_json(os.path.join(const.config_path, 'playlists.json'))
|
||||
|
||||
net = network.network(user.User())
|
||||
net = Network(User())
|
||||
|
||||
engine = PlaylistEngine(net)
|
||||
engine.load_user_playlists()
|
||||
@ -80,9 +80,9 @@ if __name__ == '__main__':
|
||||
if 'SLACKHOOK' in os.environ:
|
||||
requests.post(os.environ['SLACKHOOK'], json={"text": "spot playlists: config json not found"})
|
||||
|
||||
log.dumpLog()
|
||||
except Exception:
|
||||
log.dump_log()
|
||||
except:
|
||||
log.log("exception occured")
|
||||
if 'SLACKHOOK' in os.environ:
|
||||
requests.post(os.environ['SLACKHOOK'], json={"text": "spot playlists: exception occured"})
|
||||
log.dumpLog()
|
||||
log.dump_log()
|
||||
|
@ -1,8 +1,8 @@
|
||||
import spotframework.net.user as userclass
|
||||
import spotframework.net.network as networkclass
|
||||
from spotframework.net.user import User
|
||||
from spotframework.net.network import Network
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
network = networkclass.network(userclass.User())
|
||||
network = Network(User())
|
||||
|
||||
print(network.user.access_token)
|
||||
|
2
main.py
2
main.py
@ -11,7 +11,7 @@ if __name__ == '__main__':
|
||||
|
||||
# print(data)
|
||||
|
||||
network = networkclass.network(userclass.User())
|
||||
network = networkclass.Network(userclass.User())
|
||||
|
||||
# tracks = network.getPlaylistTracks("76ynkbkyc4uq11u1FcpOyG")
|
||||
|
||||
|
@ -14,14 +14,14 @@ class PlaylistEngine:
|
||||
self.net = net
|
||||
|
||||
def load_user_playlists(self):
|
||||
self.playlists = self.net.getUserPlaylists()
|
||||
self.playlists = self.net.get_user_playlists()
|
||||
|
||||
def append_user_playlists(self):
|
||||
self.playlists += self.net.getUserPlaylists()
|
||||
self.playlists += self.net.get_user_playlists()
|
||||
|
||||
def get_playlist_tracks(self, playlist):
|
||||
log.log("pulling tracks for {}".format(playlist.name))
|
||||
playlist.tracks = self.net.getPlaylistTracks(playlist.playlistid)
|
||||
playlist.tracks = self.net.get_playlist_tracks(playlist.playlistid)
|
||||
|
||||
def make_playlist(self, playlist_parts, processors=[]):
|
||||
|
||||
@ -65,7 +65,7 @@ class PlaylistEngine:
|
||||
return self.make_playlist(recent_playlist_parts + [this_month, last_month], processors)
|
||||
|
||||
def execute_playlist(self, tracks, playlist_id):
|
||||
self.net.replacePlaylistTracks(playlist_id, [i['track']['uri'] for i in tracks])
|
||||
self.net.replace_playlist_tracks(playlist_id, [i['track']['uri'] for i in tracks])
|
||||
|
||||
def change_description(self, playlistparts, playlist_id):
|
||||
self.net.changePlaylistDetails(playlist_id, description=' / '.join(playlistparts))
|
||||
self.net.change_playlist_details(playlist_id, description=' / '.join(playlistparts))
|
||||
|
@ -5,7 +5,8 @@ import spotframework.log.log as log
|
||||
|
||||
headers = ['name', 'artist', 'album', 'album artist', 'added', 'track id', 'album id', 'added by']
|
||||
|
||||
def exportPlaylist(playlist, path, name=None):
|
||||
|
||||
def export_playlist(playlist, path, name=None):
|
||||
|
||||
log.log('exportPlaylist', playlist.name, path, name)
|
||||
|
||||
|
@ -3,7 +3,8 @@ import os
|
||||
|
||||
import spotframework.log.log as log
|
||||
|
||||
def loadJson(path):
|
||||
|
||||
def load_json(path):
|
||||
|
||||
log.log("load json", path)
|
||||
|
||||
|
@ -5,6 +5,7 @@ import spotframework.net.const as const
|
||||
|
||||
logentries = []
|
||||
|
||||
|
||||
def log(entry, *args):
|
||||
|
||||
timestamp = datetime.datetime.now()
|
||||
@ -15,7 +16,8 @@ def log(entry, *args):
|
||||
|
||||
print(output)
|
||||
|
||||
def dumpLog():
|
||||
|
||||
def dump_log():
|
||||
|
||||
if not os.path.exists(os.path.join(const.config_path, 'log')):
|
||||
os.makedirs(os.path.join(const.config_path, 'log'))
|
||||
|
@ -1,19 +1,19 @@
|
||||
import requests
|
||||
from . import const
|
||||
from spotframework.model.playlist import Playlist as playlistclass
|
||||
from spotframework.model.playlist import Playlist
|
||||
import spotframework.log.log as log
|
||||
|
||||
limit = 50
|
||||
|
||||
|
||||
class network:
|
||||
class Network:
|
||||
|
||||
def __init__(self, user):
|
||||
self.user = user
|
||||
|
||||
def _makeGetRequest(self, method, url, params=None, headers={}):
|
||||
def _make_get_request(self, method, url, params=None, headers={}):
|
||||
|
||||
headers['Authorization'] = 'Bearer ' + self.user.access_token
|
||||
headers['Authorization'] = 'Bearer ' + self.user.accesstoken
|
||||
|
||||
req = requests.get(const.api_url + url, params=params, headers=headers)
|
||||
|
||||
@ -25,9 +25,9 @@ class network:
|
||||
|
||||
return None
|
||||
|
||||
def _makePostRequest(self, method, url, params=None, json=None, headers={}):
|
||||
def _make_post_request(self, method, url, params=None, json=None, headers={}):
|
||||
|
||||
headers['Authorization'] = 'Bearer ' + self.user.access_token
|
||||
headers['Authorization'] = 'Bearer ' + self.user.accesstoken
|
||||
|
||||
req = requests.post(const.api_url + url, params=params, json=json, headers=headers)
|
||||
|
||||
@ -39,9 +39,9 @@ class network:
|
||||
|
||||
return None
|
||||
|
||||
def _makePutRequest(self, method, url, params=None, json=None, headers={}):
|
||||
def _make_put_request(self, method, url, params=None, json=None, headers={}):
|
||||
|
||||
headers['Authorization'] = 'Bearer ' + self.user.access_token
|
||||
headers['Authorization'] = 'Bearer ' + self.user.accesstoken
|
||||
|
||||
req = requests.put(const.api_url + url, params=params, json=json, headers=headers)
|
||||
|
||||
@ -53,14 +53,13 @@ class network:
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def getPlaylist(self, playlistid, tracksonly=False):
|
||||
def get_playlist(self, playlistid, tracksonly=False):
|
||||
|
||||
log.log("getPlaylist", playlistid)
|
||||
|
||||
tracks = self.getPlaylistTracks(playlistid)
|
||||
tracks = self.get_playlist_tracks(playlistid)
|
||||
|
||||
playlist = playlistclass(playlistid)
|
||||
playlist = Playlist(playlistid)
|
||||
playlist.tracks += tracks
|
||||
|
||||
if not tracksonly:
|
||||
@ -68,7 +67,7 @@ class network:
|
||||
|
||||
return playlist
|
||||
|
||||
def getPlaylists(self, offset=0):
|
||||
def get_playlists(self, offset=0):
|
||||
|
||||
log.log("getPlaylists", offset)
|
||||
|
||||
@ -76,13 +75,13 @@ class network:
|
||||
|
||||
params = {'offset': offset, 'limit': limit}
|
||||
|
||||
resp = self._makeGetRequest('getPlaylists', 'me/playlists', params=params)
|
||||
resp = self._make_get_request('getPlaylists', 'me/playlists', params=params)
|
||||
|
||||
if resp:
|
||||
|
||||
for responseplaylist in resp['items']:
|
||||
|
||||
playlist = playlistclass(responseplaylist['id'], responseplaylist['uri'])
|
||||
playlist = Playlist(responseplaylist['id'], responseplaylist['uri'])
|
||||
playlist.name = responseplaylist['name']
|
||||
playlist.userid = responseplaylist['owner']['id']
|
||||
|
||||
@ -91,20 +90,20 @@ class network:
|
||||
# playlists = playlists + resp['items']
|
||||
|
||||
if resp['next']:
|
||||
playlists += self.getPlaylists(offset + limit)
|
||||
playlists += self.get_playlists(offset + limit)
|
||||
|
||||
return playlists
|
||||
|
||||
else:
|
||||
return None
|
||||
|
||||
def getUserPlaylists(self):
|
||||
def get_user_playlists(self):
|
||||
|
||||
log.log("getUserPlaylists")
|
||||
|
||||
return list(filter(lambda x: x.userid == self.user.username, self.getPlaylists()))
|
||||
return list(filter(lambda x: x.userid == self.user.username, self.get_playlists()))
|
||||
|
||||
def getPlaylistTracks(self, playlistid, offset=0):
|
||||
def get_playlist_tracks(self, playlistid, offset=0):
|
||||
|
||||
log.log("getPlaylistTracks", playlistid, offset)
|
||||
|
||||
@ -112,35 +111,32 @@ class network:
|
||||
|
||||
params = {'offset': offset, 'limit': limit}
|
||||
|
||||
resp = self._makeGetRequest('getPlaylistTracks', 'playlists/{}/tracks'.format(playlistid), params=params)
|
||||
resp = self._make_get_request('getPlaylistTracks', 'playlists/{}/tracks'.format(playlistid), params=params)
|
||||
|
||||
tracks += resp['items']
|
||||
|
||||
if resp['next']:
|
||||
tracks += self.getPlaylistTracks(playlistid, offset + limit)
|
||||
tracks += self.get_playlist_tracks(playlistid, offset + limit)
|
||||
|
||||
return tracks
|
||||
|
||||
|
||||
def getAvailableDevices(self):
|
||||
def get_available_devices(self):
|
||||
|
||||
log.log("getAvailableDevices")
|
||||
|
||||
return self._makeGetRequest('getAvailableDevices', 'me/player/devices')
|
||||
return self._make_get_request('getAvailableDevices', 'me/player/devices')
|
||||
|
||||
|
||||
def getPlayer(self):
|
||||
def get_player(self):
|
||||
|
||||
log.log("getPlayer")
|
||||
|
||||
return self._makeGetRequest('getPlayer', 'me/player')
|
||||
return self._make_get_request('getPlayer', 'me/player')
|
||||
|
||||
|
||||
def getDeviceID(self, devicename):
|
||||
def get_device_id(self, devicename):
|
||||
|
||||
log.log("getDeviceID", devicename)
|
||||
|
||||
return next((i for i in self.getAvailableDevices()['devices'] if i['name'] == devicename), None)['id']
|
||||
return next((i for i in self.get_available_devices()['devices'] if i['name'] == devicename), None)['id']
|
||||
|
||||
def play(self, uri, deviceid=None):
|
||||
|
||||
@ -153,7 +149,7 @@ class network:
|
||||
|
||||
payload = {'context_uri': uri}
|
||||
|
||||
req = self._makePutRequest('play', 'me/player/play', params=params, json=payload)
|
||||
req = self._make_put_request('play', 'me/player/play', params=params, json=payload)
|
||||
|
||||
def pause(self, deviceid=None):
|
||||
|
||||
@ -164,8 +160,7 @@ class network:
|
||||
else:
|
||||
params = None
|
||||
|
||||
req = self._makePutRequest('pause', 'me/player/pause', params=params)
|
||||
|
||||
req = self._make_put_request('pause', 'me/player/pause', params=params)
|
||||
|
||||
def next(self, deviceid=None):
|
||||
|
||||
@ -176,10 +171,9 @@ class network:
|
||||
else:
|
||||
params = None
|
||||
|
||||
req = self._makePostRequest('next', 'me/player/next', params=params)
|
||||
req = self._make_post_request('next', 'me/player/next', params=params)
|
||||
|
||||
|
||||
def setShuffle(self, state, deviceid=None):
|
||||
def set_shuffle(self, state, deviceid=None):
|
||||
|
||||
log.log("setShuffle", state, deviceid)
|
||||
|
||||
@ -188,10 +182,9 @@ class network:
|
||||
if deviceid is not None:
|
||||
params['device_id'] = deviceid
|
||||
|
||||
req = self._makePutRequest('setShuffle', 'me/player/shuffle', params=params)
|
||||
req = self._make_put_request('setShuffle', 'me/player/shuffle', params=params)
|
||||
|
||||
|
||||
def setVolume(self, volume, deviceid=None):
|
||||
def set_volume(self, volume, deviceid=None):
|
||||
|
||||
log.log("setVolume", volume, deviceid)
|
||||
|
||||
@ -202,12 +195,12 @@ class network:
|
||||
if deviceid is not None:
|
||||
params['device_id'] = deviceid
|
||||
|
||||
req = self._makePutRequest('setVolume', 'me/player/volume', params=params)
|
||||
req = self._make_put_request('setVolume', 'me/player/volume', params=params)
|
||||
|
||||
else:
|
||||
log.log("setVolume", volume, "not allowed")
|
||||
|
||||
def makePlaylist(self, name, description=None, public=True, collaborative=False):
|
||||
def make_playlist(self, name, description=None, public=True, collaborative=False):
|
||||
|
||||
log.log("makePlaylist", name, 'description:{}'.format(description), 'public:{}'.format(public),
|
||||
'collaborative:{}'.format(collaborative))
|
||||
@ -219,19 +212,19 @@ class network:
|
||||
if description is not None:
|
||||
json["description"] = description
|
||||
|
||||
req = self._makePostRequest('makePlaylist', 'users/{}/playlists'.format(self.user.username), json=json,
|
||||
req = self._make_post_request('makePlaylist', 'users/{}/playlists'.format(self.user.username), json=json,
|
||||
headers=headers)
|
||||
|
||||
if req is not None:
|
||||
resp = req.json()
|
||||
|
||||
if resp is not None:
|
||||
playlist = playlistclass(resp["id"], uri=resp['uri'], name=resp['name'], userid=resp['owner']['id'])
|
||||
playlist = Playlist(resp["id"], uri=resp['uri'], name=resp['name'], userid=resp['owner']['id'])
|
||||
return playlist
|
||||
|
||||
return None
|
||||
|
||||
def replacePlaylistTracks(self, playlistid, uris):
|
||||
def replace_playlist_tracks(self, playlistid, uris):
|
||||
|
||||
log.log("replacePlaylistTracks", playlistid)
|
||||
|
||||
@ -239,16 +232,16 @@ class network:
|
||||
|
||||
json = {"uris": uris[:100]}
|
||||
|
||||
req = self._makePutRequest('replacePlaylistTracks', 'playlists/{}/tracks'.format(playlistid), json=json,
|
||||
req = self._make_put_request('replacePlaylistTracks', 'playlists/{}/tracks'.format(playlistid), json=json,
|
||||
headers=headers)
|
||||
|
||||
if req is not None:
|
||||
resp = req.json()
|
||||
|
||||
if len(uris) > 100:
|
||||
self.addPlaylistTracks(playlistid, uris[100:])
|
||||
self.add_playlist_tracks(playlistid, uris[100:])
|
||||
|
||||
def changePlaylistDetails(self, playlistid, name=None, public=None, collaborative=None, description=None):
|
||||
def change_playlist_details(self, playlistid, name=None, public=None, collaborative=None, description=None):
|
||||
|
||||
log.log("changePlaylistDetails", playlistid)
|
||||
|
||||
@ -268,11 +261,11 @@ class network:
|
||||
if description is not None:
|
||||
json['description'] = description
|
||||
|
||||
req = self._makePutRequest('changePlaylistDetails', 'playlists/{}'.format(playlistid), json=json,
|
||||
req = self._make_put_request('changePlaylistDetails', 'playlists/{}'.format(playlistid), json=json,
|
||||
headers=headers)
|
||||
return req
|
||||
|
||||
def addPlaylistTracks(self, playlistid, uris):
|
||||
def add_playlist_tracks(self, playlistid, uris):
|
||||
|
||||
log.log("addPlaylistTracks", playlistid)
|
||||
|
||||
@ -280,7 +273,7 @@ class network:
|
||||
|
||||
json = {"uris": uris[:100]}
|
||||
|
||||
req = self._makePostRequest('addPlaylistTracks', 'playlists/{}/tracks'.format(playlistid), json=json,
|
||||
req = self._make_post_request('addPlaylistTracks', 'playlists/{}/tracks'.format(playlistid), json=json,
|
||||
headers=headers)
|
||||
|
||||
if req is not None:
|
||||
@ -288,4 +281,4 @@ class network:
|
||||
|
||||
if len(uris) > 100:
|
||||
|
||||
self.addPlaylistTracks(playlistid, uris[100:])
|
||||
self.add_playlist_tracks(playlistid, uris[100:])
|
||||
|
@ -5,34 +5,32 @@ from base64 import b64encode
|
||||
client_id = os.environ['SPOTCLIENT']
|
||||
client_secret = os.environ['SPOTSECRET']
|
||||
|
||||
|
||||
class User:
|
||||
|
||||
def __init__(self):
|
||||
self.access_token = os.environ['SPOTACCESS']
|
||||
self.refresh_token = os.environ['SPOTREFRESH']
|
||||
self.accesstoken = os.environ['SPOTACCESS']
|
||||
self.refreshtoken = os.environ['SPOTREFRESH']
|
||||
|
||||
self.refreshToken()
|
||||
self.refresh_token()
|
||||
|
||||
self.username = self.getInfo()['id']
|
||||
self.username = self.get_info()['id']
|
||||
|
||||
def refreshToken(self):
|
||||
def refresh_token(self):
|
||||
|
||||
idsecret = b64encode(bytes(client_id + ':' + client_secret, "utf-8")).decode("ascii")
|
||||
headers = {'Authorization': 'Basic %s' % idsecret}
|
||||
|
||||
data = {"grant_type": "refresh_token", "refresh_token": self.refresh_token}
|
||||
data = {"grant_type": "refresh_token", "refresh_token": self.refreshtoken}
|
||||
|
||||
req = requests.post('https://accounts.spotify.com/api/token', data=data, headers=headers)
|
||||
|
||||
#print(req.status_code)
|
||||
#print(req.text)
|
||||
|
||||
if req.status_code is 200:
|
||||
self.access_token = req.json()['access_token']
|
||||
self.accesstoken = req.json()['access_token']
|
||||
|
||||
def getInfo(self):
|
||||
def get_info(self):
|
||||
|
||||
headers = { 'Authorization' : 'Bearer %s' % self.access_token }
|
||||
headers = {'Authorization': 'Bearer %s' % self.accesstoken}
|
||||
|
||||
req = requests.get('https://api.spotify.com/v1/me', headers=headers)
|
||||
return req.json()
|
||||
|
Loading…
Reference in New Issue
Block a user