Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spotify #176

Open
wants to merge 26 commits into
base: develop
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
231ccea
feat(package/math): create directory structure
gustafholst Mar 9, 2020
c09aef9
feat(package/spotify): create directory structure
gustafholst Mar 11, 2020
b5a37d6
feat(package/spotify): login, play track
gustafholst Mar 11, 2020
b8b690a
feat(package/spotify): refactor expressions map to individual functions
gustafholst Mar 12, 2020
457d41d
feat(package/spotify): fixed bug logging in after token expired
gustafholst Mar 12, 2020
0952449
feat(package/spotify): play album
gustafholst Mar 12, 2020
d119f19
feat(package/spotify): play artist
gustafholst Mar 12, 2020
d68df29
feat(package/spotify): fixed bug preventing album names containing th…
gustafholst Mar 12, 2020
c6916e6
feat(package/spotify): add readme with instructions
gustafholst Mar 12, 2020
cade1d8
feat(package/spotify): pause functionality
gustafholst Mar 12, 2020
28e4cb2
feat(package/spotify): DRY refactor
gustafholst Mar 13, 2020
0ade789
feat(package/spotify): bugfix + more DRY
gustafholst Mar 13, 2020
6d61883
feat(package/spotify): play playlist
gustafholst Mar 13, 2020
702aba0
feat(package/spotify): another DRY refactor
gustafholst Mar 13, 2020
dcb6fd8
feat(package/spotify): show track/album
gustafholst Mar 13, 2020
388f1ff
feat(package/spotify): add spotify authorization endpoint to server.js
gustafholst Mar 13, 2020
79ab60c
feat(package/spotify): show playlist
gustafholst Mar 13, 2020
e69a12f
feat(package/spotify): bugfix deleted track in playlist
gustafholst Mar 13, 2020
a2261dd
feat(package/spotify): show artist
gustafholst Mar 13, 2020
eb50ece
feat(package/spotify): various fixes
gustafholst Mar 14, 2020
6a58d7e
feat(package/spotify): update readme
gustafholst Mar 14, 2020
f5c9bc1
feat(package/spotify): add shebangs
gustafholst Mar 16, 2020
e9e8d91
feat(package/spotify): correct config.sample.json
gustafholst Mar 17, 2020
162089f
feat(package/spotify): added scope to config.sample.json
gustafholst Mar 18, 2020
b6c4895
feat(package/spotify): minor fixes
gustafholst Mar 18, 2020
e76d858
feat(package/spotify): add workaround for authorization bug
gustafholst Mar 22, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat(package/spotify): refactor expressions map to individual functions
gustafholst committed Mar 12, 2020
commit b8b690a1a90fa354d78abe065a3f51b5fda055ea
7 changes: 7 additions & 0 deletions packages/spotify/authorize.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import base64
import json
import time

import requests
@@ -23,6 +24,12 @@ def run(string, entities):
results = requests.post('https://accounts.spotify.com/api/token', data=payload, headers=headers)

token = results.json()

file = open("access_token.txt", 'w')
file.write(json.dumps(token))
file.close()

# add info fields to token object
token['expires_at'] = int(time.time()) + token['expires_in']
token['client_id'] = utils.config('client_id')
token['client_secret'] = utils.config('client_secret')
3 changes: 3 additions & 0 deletions packages/spotify/data/answers/en.json
Original file line number Diff line number Diff line change
@@ -18,6 +18,9 @@
"Please login to Spotify first",
"You are not logged in to Spotify"
],
"already_logged_in": [
"You are already logged in to Spotify"
],
"login": [
"Please sign in and then give me the whole URL to which you were redirected."
]
65 changes: 57 additions & 8 deletions packages/spotify/data/expressions/en.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,36 @@
{
"spotify": {
"play": {
"play_current_track": {
"expressions": [
"Play the song stairway to heaven by led zeppelin",
"Play",
"Resume",
"Resume playing"
]
},
"play_track": {
"expressions": [
"Play smells like teen spirit",
"Play the song stairway to heaven",
"Play the track lucy in the sky with diamonds by the beatles",
"Play me the song roxanne by the police",
"Play the artist Prince"
"Play me the song roxanne by the police"
],
"entities": [
{
"type": "trim",
"name": "track",
"conditions": [
{
"type": "after",
"from": "play"
},
{
"type": "after",
"from": "song"
},
{
"type": "after",
"from": "track"
},
{
"type": "between",
"from": "song",
@@ -45,24 +60,58 @@
"from": "by"
}
]
},
}
]
},
"play_album": {
"expressions": [
"Play album foo",
"Play the album derp by foo fighters"
],
"entities": [
{
"type": "trim",
"name": "playlist",
"name": "album",
"conditions": [
{
"type": "after",
"from": "playlist"
}
]
},
{
"type": "trim",
"name": "artist",
"conditions": [
{
"type": "after",
"from": "artist"
},
{
"type": "after",
"from": "group"
},
{
"type": "after",
"from": "by"
}
]
}
]
},
"play_playlist": {
"expressions": [
"Play playlist foo",
"Play the playlist derp"
],
"entities": [
{
"type": "trim",
"name": "album",
"name": "playlist",
"conditions": [
{
"type": "after",
"from": "album"
"from": "playlist"
}
]
}
59 changes: 46 additions & 13 deletions packages/spotify/spotify.py
Original file line number Diff line number Diff line change
@@ -13,16 +13,22 @@
def logged_in():
db = utils.db()['db'].all()
if len(db) > 0:
db = get_database_content()
db = get_database_content() # token info
now = int(time.time())
return int(db['expires_at']) > now
file = open("isloggedin.txt", 'w')
file.write("Expires at: " + str(db['expires_at']) + '\t' + "Now: " + str(now))
file.close()
return now < db['expires_at']

return False

def get_database_content():
return utils.db()['db'].all()[0]

def login(string, entities):
if logged_in():
return utils.output('end', 'message', utils.translate('already_logged_in'))

config = {
'redirect_uri': utils.config('redirect_uri'),
'response_type': 'code',
@@ -36,13 +42,21 @@ def login(string, entities):

utils.output('end', 'success', utils.translate('login'))

def play(string, entities):
def can_play(device):
if not logged_in():
return utils.output('end', 'error', utils.translate('not_logged_in'))
utils.output('end', 'error', utils.translate('not_logged_in'))
return False

device = get_device()
if not device:
return utils.output('end', 'error', utils.translate('no_device'))
utils.output('end', 'error', utils.translate('no_device'))
return False

return True

def play(string, entities):
device = get_device()
if not can_play(device):
return

track=''
album=''
@@ -73,19 +87,38 @@ def play(string, entities):
play_current_track(device)


def play_current_track(device):
spotify_request('PUT', 'me/player/play', {'device_id': device})
def play_current_track(string, entities):
device_id = get_device()
if not can_play(device_id):
return
spotify_request('PUT', 'me/player/play', {'device_id': device_id})
utils.output('end', 'success', utils.translate('resume_playing'))


def play_track(device_id, track_name, artist=None):
def play_track(string, entities):
device_id = get_device()
if not can_play(device_id):
return

track_name = ''
artist_name = ''

for item in entities:
if item['entity'] == 'track':
track_name=item['sourceText']
if item['entity'] == 'artist':
artist_name = item['sourceText']

if not track_name:
return utils.output('end', 'info', "no track name....faaan!")

params = {
'q': track_name,
'type': 'track'
}

if artist:
params['artist'] = artist
if artist_name:
params['artist'] = artist_name

results = spotify_request('GET', 'search', params)

@@ -97,10 +130,10 @@ def play_track(device_id, track_name, artist=None):
return utils.output('end', 'info', utils.translate('no_search_result'))

chosen_track = None
if artist:
if artist_name:
for t in results['tracks']['items']:
for a in t['artists']:
if a['name'].lower() == artist.lower():
if a['name'].lower() == artist_name.lower():
chosen_track = t
break