EsaDev

nfc.py full code

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