Skip to content

Commit

Permalink
Merge pull request #25 from MartijnVogelaar/Feature/Auto-play-links
Browse files Browse the repository at this point in the history
Implemented autoplay if spotify is not yet opened.
  • Loading branch information
MartijnVogelaar authored Mar 15, 2021
2 parents 76cf598 + 4293daa commit 7636630
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 36 deletions.
1 change: 0 additions & 1 deletion KRunner-Spotify.config
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,3 @@ REWIND_COMMAND = Rewind
SEEK_COMMAND = Seek
SET_VOLUME_COMMAND = SetVol
SHUFFLE_COMMAND = Shuffle
START_SPOTIFY_COMMAND = Spotify
3 changes: 0 additions & 3 deletions USAGE.MD
Original file line number Diff line number Diff line change
Expand Up @@ -307,9 +307,6 @@ Seek 01:01:40
## **Shuffle**
The `Shuffle` command can be used to toggle shuffling of songs.

## **Start Spotify**
The `Spotify` command can be used to start Spotify in your default browser.
<br><br>

## **TrackInfo**
The `TrackInfo` command can be used to retrieve information about the current track playing. The info being shown
Expand Down
2 changes: 0 additions & 2 deletions src/KRunnerSpotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ def Match(self, query: str):
except RuntimeError as e:
if(str(e) == "Not logged in!"):
return [(getCommandName("LOGIN_COMMAND"), "Not logged in, click to login", "Spotify", 100, 100, {})]
elif(str(e) == "No playback device available!"):
return [(getCommandName("START_SPOTIFY_COMMAND"), "No playback device available, click to open Spotify!", "Spotify", 100, 100, {})]

@dbus.service.method(iface, in_signature="ss")
def Run(self, data: str, action_id: str):
Expand Down
7 changes: 7 additions & 0 deletions src/Util.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ def parseSearchQuery(query):
page = int(result[1][1:])
return result[0], page

def parsePage(query):
query = query.lstrip(" ")
page = 1
if (query == ""):
return page
m = re.search('p(\d+)', query)
return m.group(1)

def parseArtists(results):
parsedResults = []
Expand Down
4 changes: 1 addition & 3 deletions src/commands/Command.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ def __init__(self, command, spotify):
self.command = command
if(not os.path.isfile(getSetting("CACHE_PATH"))):
raise RuntimeError("Not logged in!")
if(not self.spotify.current_playback()):
raise RuntimeError("No playback device available!")


def Match(self, query: str):
pass

Expand Down
3 changes: 0 additions & 3 deletions src/commands/Commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from .ReloadConfig import ReloadConfig
from .Repeat import Repeat
from .Resume import Resume
from .StartSpotify import StartSpotify
from .Shuffle import Shuffle
from .FastForward import FastForward
from .Rewind import Rewind
Expand All @@ -38,8 +37,6 @@ def executeCommand(command, spotify):
return Play(spotify)
elif(command == getCommandName("ADD_TO_QUEUE_COMMAND")):
return AddToQueue(spotify)
elif(command == getCommandName("START_SPOTIFY_COMMAND")):
return StartSpotify(spotify)
elif(command == getCommandName("LOGOUT_COMMAND")):
return Logout(spotify)
elif(command == getCommandName("LOGIN_COMMAND")):
Expand Down
4 changes: 2 additions & 2 deletions src/commands/Episode.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from .Command import Command
from Config import getCommandName
from Util import parseSearchQuery
from Util import parseSearchQuery, parsePage
from Config import getSetting, getCommandName


Expand All @@ -9,7 +9,7 @@ def __init__(self, spotify):
super().__init__(getCommandName("EPISODE_COMMAND"), spotify)

def Match(self, query: str):
query, page = parseSearchQuery(query)
page = int(parsePage(query))
episodeOffset = int(getSetting("MAX_RESULTS")) * (page - 1)
playbackDetails = self.spotify.currently_playing(
additional_types="episode")
Expand Down
21 changes: 20 additions & 1 deletion src/commands/Play.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
from .TopArtist import TopArtist
from Config import getCommandName, getSetting

import webbrowser
import re
import time

class Play(Command):
def __init__(self, spotify):
Expand All @@ -34,7 +37,23 @@ def Match(self, query: str):
return [("", "Invalid command", "Spotify", 100, 100, {})]

def Run(self, data: str):
if("track" in data or "episode" in data):
if(not self.spotify.current_playback()):
# Spotify only supports uris to be autoplayed if it is a track. Other source such as:
# Podcasts, Albums, Artists and Playlists cannot be outplayed at this point in time.
# As we want these source to be autoplayed the following workaround has been made:
# We first start a "bank" track without sound, we then wait untill the device is playing".
# When the device is playing we play the actual "wanted" source which now plays automatically.

webbrowser.open("https://open.spotify.com/track/1KMgqD5AFo1hCjEpv50LwR?si=768a514adc444958")
hasToEndBy = time.time() + 5
while(not self.spotify.current_playback() ):
time.sleep(0.1)
if(time.time() > hasToEndBy):
raise RuntimeError("Playback device not started in the specified time.")
print("test")
return
self.Run(data)
elif("track" in data or "episode" in data):
self.spotify.start_playback(uris=[data])
elif("show" in data or "artist" in data or "playlist" in data):
self.spotify.start_playback(context_uri=data)
Expand Down
21 changes: 0 additions & 21 deletions src/commands/StartSpotify.py

This file was deleted.

0 comments on commit 7636630

Please sign in to comment.