Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import argparse
- import logging
- import sys
- import time
- import pychromecast
- from pychromecast.controllers.spotify import SpotifyController
- import spotify_token as st
- import spotipy
- from spotipy.oauth2 import SpotifyOAuth
- DEFAULT_SONG_URI = 'spotify:track:5WvDO1ZKap15ehQeNRHJ9H'
- SP_DC = 'sp_dc cookie was here'
- SP_KEY = 'sp_key cookie was here'
- CLIENT_ID = 'Client ID was here'
- CLIENT_SECRET = 'Client Secret was here'
- SPOTIPY_REDIRECT_URI = 'http://localhost'
- SPOTIFY_USERNAME = 'Username was here'
- parser = argparse.ArgumentParser(description="Cast songs/albums to Google Home via nfc controls.")
- parser.add_argument("--show-debug", help="Enable debug log", action="store_true")
- parser.add_argument(
- "--uri",
- help='Spotify uri(s) (default: "%(default)s")',
- default=DEFAULT_SONG_URI,
- nargs="+",
- )
- args = parser.parse_args()
- if args.show_debug:
- logging.basicConfig(level=logging.DEBUG)
- def n():
- print("\n")
- spotify_device_id = None
- # Create a spotify token
- data = st.start_session(SP_DC, SP_KEY)
- access_token = data[0]
- expires = data[1] - int(time.time())
- # Create a spotify client
- scope = "user-library-read"
- client = spotipy.Spotify(auth=access_token, auth_manager=SpotifyOAuth(client_id=CLIENT_ID, client_secret=CLIENT_SECRET, scope=scope, redirect_uri=SPOTIPY_REDIRECT_URI, username=SPOTIFY_USERNAME))
- if args.show_debug:
- spotipy.trace = True
- spotipy.trace_out = True
- # Launch the spotify app on the google home
- sp = SpotifyController(access_token, expires)
- home.register_handler(sp) #home was defined previously but taken out for context only
- sp.launch_app()
- n()
- print(home.status)
- n()
- 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])
Advertisement
Add Comment
Please, Sign In to add comment