EsaDev

nfc.py contex only

Jul 7th, 2020
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. import argparse
  2. import logging
  3. import sys
  4. import time
  5.  
  6. import pychromecast
  7. from pychromecast.controllers.spotify import SpotifyController
  8. import spotify_token as st
  9. import spotipy
  10. from spotipy.oauth2 import SpotifyOAuth
  11.  
  12. DEFAULT_SONG_URI = 'spotify:track:5WvDO1ZKap15ehQeNRHJ9H'
  13. SP_DC = 'sp_dc cookie was here'
  14. SP_KEY = 'sp_key cookie was here'
  15. CLIENT_ID = 'Client ID was here'
  16. CLIENT_SECRET = 'Client Secret was here'
  17. SPOTIPY_REDIRECT_URI = 'http://localhost'
  18. SPOTIFY_USERNAME = 'Username was here'
  19.  
  20. parser = argparse.ArgumentParser(description="Cast songs/albums to Google Home via nfc controls.")
  21. parser.add_argument("--show-debug", help="Enable debug log", action="store_true")
  22. parser.add_argument(
  23. "--uri",
  24. help='Spotify uri(s) (default: "%(default)s")',
  25. default=DEFAULT_SONG_URI,
  26. nargs="+",
  27. )
  28. args = parser.parse_args()
  29.  
  30. if args.show_debug:
  31. logging.basicConfig(level=logging.DEBUG)
  32.  
  33. def n():
  34. print("\n")
  35.  
  36.  
  37. spotify_device_id = None
  38.  
  39. # Create a spotify token
  40. data = st.start_session(SP_DC, SP_KEY)
  41. access_token = data[0]
  42. expires = data[1] - int(time.time())
  43.  
  44. # Create a spotify client
  45. scope = "user-library-read"
  46.  
  47. 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))
  48. if args.show_debug:
  49. spotipy.trace = True
  50. spotipy.trace_out = True
  51.  
  52. # Launch the spotify app on the google home
  53. sp = SpotifyController(access_token, expires)
  54. home.register_handler(sp) #home was defined previously but taken out for context only
  55. sp.launch_app()
  56.  
  57. n()
  58. print(home.status)
  59. n()
  60.  
  61. if not sp.is_launched and not sp.credential_error:
  62. print("Failed to launch spotify controller due to timeout")
  63. sys.exit(1)
  64. if not sp.is_launched and sp.credential_error:
  65. print("Failed to launch spotify controller due to credential error")
  66. sys.exit(1)
  67.  
  68. # Query spotify for active devices
  69. devices_available = client.devices()
  70.  
  71. # Match active spotify devices with the spotify controller's device id
  72. for device in devices_available["devices"]:
  73. if device["id"] == sp.device:
  74. spotify_device_id = device["id"]
  75. break
  76.  
  77. if not spotify_device_id:
  78. print('No device with id "{}" known by Spotify'.format(sp.device))
  79. print("Known devices: {}".format(devices_available["devices"]))
  80. sys.exit(1)
  81.  
  82. # Start playback
  83. if args.uri[0].find("track") > 0:
  84. client.start_playback(device_id=spotify_device_id, uris=args.uri)
  85. else:
  86. client.start_playback(device_id=spotify_device_id, context_uri=args.uri[0])
Advertisement
Add Comment
Please, Sign In to add comment