-
Notifications
You must be signed in to change notification settings - Fork 380
/
Copy pathspotify_example.py
120 lines (98 loc) · 3.47 KB
/
spotify_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
"""
Example on how to use the Spotify Controller.
NOTE: You need to install the spotipy and spotify-token dependencies.
This can be done by running the following:
pip install spotify-token
pip install git+https://github.com/plamere/spotipy.git
"""
# pylint: disable=invalid-name
import argparse
import logging
import time
import sys
import zeroconf
import spotify_token as st # pylint: disable=import-error
import spotipy # pylint: disable=import-error
import pychromecast
from pychromecast.controllers.spotify import SpotifyController
CAST_NAME = "My Chromecast"
parser = argparse.ArgumentParser(
description="Example on how to use the Spotify Controller."
)
parser.add_argument(
"--cast", help='Name of cast device (default: "%(default)s")', default=CAST_NAME
)
parser.add_argument(
"--known-host",
help="Add known host (IP), can be used multiple times",
action="append",
)
parser.add_argument("--show-debug", help="Enable debug log", action="store_true")
parser.add_argument(
"--show-zeroconf-debug", help="Enable zeroconf debug log", action="store_true"
)
parser.add_argument("--user", help="Spotify username", required=True)
parser.add_argument("--password", help="Spotify password", required=True)
parser.add_argument(
"--uri",
help='Spotify uri(s) (default: "%(default)s")',
default=["spotify:track:3Zwu2K0Qa5sT6teCCHPShP"],
nargs="+",
)
args = parser.parse_args()
if args.show_debug:
logging.basicConfig(level=logging.DEBUG)
# Uncomment to enable http.client debug log
# http_client.HTTPConnection.debuglevel = 1
if args.show_zeroconf_debug:
print("Zeroconf version: " + zeroconf.__version__)
logging.getLogger("zeroconf").setLevel(logging.DEBUG)
chromecasts, browser = pychromecast.get_listed_chromecasts(
friendly_names=[args.cast], known_hosts=args.known_host
)
cast = list(chromecasts)[0]
if not cast:
print('No chromecast with name "{}" discovered'.format(args.cast))
print("Discovered casts: {}".format(chromecasts))
sys.exit(1)
print("cast {}".format(cast))
# Wait for connection to the chromecast
cast.wait()
spotify_device_id = None
# Create a spotify token
data = st.start_session(args.user, args.password)
access_token = data[0]
expires = data[1] - int(time.time())
# Create a spotify client
client = spotipy.Spotify(auth=access_token)
if args.show_debug:
spotipy.trace = True
spotipy.trace_out = True
# Launch the spotify app on the cast we want to cast to
sp = SpotifyController(access_token, expires)
cast.register_handler(sp)
sp.launch_app()
if not sp.is_launched and not sp.credential_error:
print("Failed to launch spotify controller due to timeout")
sys.exit(1)
if not sp.is_launched and sp.credential_error:
print("Failed to launch spotify controller due to credential error")
sys.exit(1)
# Query spotify for active devices
devices_available = client.devices()
# Match active spotify devices with the spotify controller's device id
for device in devices_available["devices"]:
if device["id"] == sp.device:
spotify_device_id = device["id"]
break
if not spotify_device_id:
print('No device with id "{}" known by Spotify'.format(sp.device))
print("Known devices: {}".format(devices_available["devices"]))
sys.exit(1)
# Start playback
if args.uri[0].find("track") > 0:
client.start_playback(device_id=spotify_device_id, uris=args.uri)
else:
client.start_playback(device_id=spotify_device_id, context_uri=args.uri[0])
# Shut down discovery
browser.stop_discovery()