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'
- CAST_NAME = 'Bedroom speaker'
- 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="+",
- )
- parser.add_argument("--cast", help='Name of cast device (default: "%(default)s")', default=CAST_NAME)
- #parser.add_argument("--user", help="Spotify username", required=True)
- #parser.add_argument("--password", help="Spotify password", required=True)
- args = parser.parse_args()
- if args.show_debug:
- logging.basicConfig(level=logging.DEBUG)
- def n():
- print("\n")
- chromecasts, browser = pychromecast.get_listed_chromecasts(friendly_names=[args.cast])
- home = None
- for _cast in chromecasts:
- if _cast.name == args.cast:
- home = _cast
- break
- if not home:
- print('No chromecast with name "{}" discovered'.format(args.cast))
- print("Discovered casts: {}".format(chromecasts))
- sys.exit(1)
- print("cast {}".format(home))
- home.wait() # Start a new worker thread/wait for connection
- # Log device and status
- n()
- print(home.device)
- n()
- print(home.status)
- 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 home
- sp = SpotifyController(access_token, expires)
- home.register_handler(sp)
- 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])
- # Shut down discovery
- pychromecast.discovery.stop_discovery(browser)
Advertisement
Add Comment
Please, Sign In to add comment