EsaDev

github issue code

Jul 6th, 2020
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 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.  
  11. DEFAULT_SONG_URI = 'spotify:track:5WvDO1ZKap15ehQeNRHJ9H'
  12. CAST_NAME = 'Bedroom speaker'
  13.  
  14. parser = argparse.ArgumentParser(description="Cast songs/albums to Google Home via nfc controls.")
  15. parser.add_argument("--show-debug", help="Enable debug log", action="store_true")
  16. parser.add_argument(
  17. "--uri",
  18. help='Spotify uri(s) (default: "%(default)s")',
  19. default=DEFAULT_SONG_URI,
  20. nargs="+",
  21. )
  22. parser.add_argument("--cast", help='Name of cast device (default: "%(default)s")', default=CAST_NAME)
  23. parser.add_argument("--user", help="Spotify username", required=True)
  24. parser.add_argument("--password", help="Spotify password", required=True)
  25. args = parser.parse_args()
  26.  
  27. if args.show_debug:
  28. logging.basicConfig(level=logging.DEBUG)
  29.  
  30. def n():
  31. print("\n")
  32.  
  33.  
  34. chromecasts, browser = pychromecast.get_listed_chromecasts(friendly_names=[args.cast])
  35. home = None
  36. for _cast in chromecasts:
  37. if _cast.name == args.cast:
  38. home = _cast
  39. break
  40.  
  41. if not home:
  42. print('No chromecast with name "{}" discovered'.format(args.cast))
  43. print("Discovered casts: {}".format(chromecasts))
  44. sys.exit(1)
  45.  
  46. print("cast {}".format(home))
  47.  
  48. home.wait() # Start a new worker thread/wait for connection
  49.  
  50. # Log device and status
  51. n()
  52. print(home.device)
  53. n()
  54. print(home.status)
  55. n()
  56.  
  57. spotify_device_id = None
  58.  
  59. # Create a spotify token
  60. data = st.start_session(args.user, args.password)
  61. access_token = data[0]
  62. expires = data[1] - int(time.time())
  63.  
  64. # Create a spotify client
  65. client = spotipy.Spotify(auth=access_token)
  66. if args.show_debug:
  67. spotipy.trace = True
  68. spotipy.trace_out = True
  69.  
  70. # Launch the spotify app on the home
  71. sp = SpotifyController(access_token, expires)
  72. home.register_handler(sp)
  73. sp.launch_app()
  74.  
  75. n()
  76. print(home.status)
  77.  
  78. if not sp.is_launched and not sp.credential_error:
  79. print("Failed to launch spotify controller due to timeout")
  80. sys.exit(1)
  81. elif not sp.is_launched and sp.credential_error:
  82. print("Failed to launch spotify controller due to credential error")
  83. sys.exit(1)
  84.  
  85. # Query spotify for active devices
  86. devices_available = client.devices()
  87.  
  88. # Match active spotify devices with the spotify controller's device id
  89. for device in devices_available["devices"]:
  90. if device["id"] == sp.device:
  91. spotify_device_id = device["id"]
  92. break
  93.  
  94. if not spotify_device_id:
  95. print('No device with id "{}" known by Spotify'.format(sp.device))
  96. print("Known devices: {}".format(devices_available["devices"]))
  97. sys.exit(1)
  98.  
  99. # Start playback
  100. if args.uri[0].find("track") > 0:
  101. client.start_playback(device_id=spotify_device_id, uris=args.uri)
  102. else:
  103. client.start_playback(device_id=spotify_device_id, context_uri=args.uri[0])
  104.  
  105. # Shut down discovery
  106. pychromecast.discovery.stop_discovery(browser)
Add Comment
Please, Sign In to add comment