Skip to content

Commit

Permalink
add spotify controller
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelneu committed Feb 5, 2019
1 parent 7258708 commit 993f916
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 2 deletions.
2 changes: 2 additions & 0 deletions honeycast/cast.py
Expand Up @@ -3,6 +3,7 @@
from .controllers.heartbeat import HeartbeatController
from .controllers.media import MediaController
from .controllers.receiver import ReceiverController
from .controllers.spotify import SpotifyController
from .log import logger
from pychromecast.cast_channel_pb2 import CastMessage
import json
Expand Down Expand Up @@ -58,6 +59,7 @@ def __init__(self, socket):
ConnectionController(),
ReceiverController(),
MediaController(),
SpotifyController(),
]

def close(self):
Expand Down
34 changes: 32 additions & 2 deletions honeycast/controllers/receiver.py
@@ -1,12 +1,14 @@
from ..config import config
from ..log import logger
from .base import BaseController
from pychromecast.socket_client import NS_RECEIVER, TYPE_GET_STATUS, TYPE_RECEIVER_STATUS
from pychromecast.socket_client import NS_RECEIVER, TYPE_GET_STATUS, TYPE_RECEIVER_STATUS, TYPE_LAUNCH
import uuid

class ReceiverController(BaseController):
def __init__(self):
super().__init__(NS_RECEIVER)
self._session_id = str(uuid.uuid4())
self._app_id = None

def get_reply(self, message):
message_type = message.data.get("type", "")
Expand All @@ -18,7 +20,7 @@ def get_reply(self, message):
"status": {
"applications": [
{
"appId": "CC1AD845",
"appId": self._app_id or "CC1AD845",
"displayName": "Default Media Receiver",
"iconUrl": "",
"isIdleScreen": False,
Expand Down Expand Up @@ -61,5 +63,33 @@ def get_reply(self, message):
}
},
})
elif message_type == TYPE_LAUNCH:
app_id = message.data.get("appId", None)
logger.info("launching app %s", app_id)
self._app_id = app_id

return self.create_default_reply(message, {
"requestId": message.data.get("requestId", 0),
"type": TYPE_RECEIVER_STATUS,
"status": {
"applications": [
{
"appId": self._app_id or "CC1AD845",
"displayName": "Default Media Receiver",
"iconUrl": "",
"isIdleScreen": False,
"launchedFromCloud": False,
"namespaces": [
{
"name": "urn:x-cast:com.spotify.chromecast.secure.v1"
},
],
"sessionId": self._session_id,
"statusText": "Default Media Receiver",
"transportId": self._session_id
}
],
}
})

return super().get_reply(message)
28 changes: 28 additions & 0 deletions honeycast/controllers/spotify.py
@@ -0,0 +1,28 @@
from ..log import logger
from .base import BaseController
from pychromecast.controllers.spotify import APP_NAMESPACE
from spotipy import Spotify
import pdb

class SpotifyController(BaseController):
def __init__(self):
super().__init__(APP_NAMESPACE)
self._spotify = None

def get_reply(self, message):
message_type = message.data.get("type", "")

if message_type == "setCredentials":
token = message.data.get("credentials", "")
logger.debug("received spotify token %s", token)

self._spotify = Spotify(auth=token)
user = self._spotify.me()
no_value = "[n/a]"
email = user.get("email", no_value)
name = user.get("display_name", no_value)
country = user.get("country", no_value)
birthday = user.get("birthdate", no_value)
logger.info("%s (%s from %s, born %s) launched spotify", email, name, country, birthday)

return super().get_reply(message)
2 changes: 2 additions & 0 deletions requirements.txt
Expand Up @@ -17,6 +17,8 @@ pycparser==2.19
PyYAML==4.2b1
requests==2.21.0
six==1.12.0
spotify-token==0.1.0
spotipy==2.4.4
urllib3==1.24.1
Werkzeug==0.14.1
zeroconf==0.21.3

0 comments on commit 993f916

Please sign in to comment.