Guest User

Untitled

a guest
Feb 11th, 2018
656
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. const Client = require('castv2-client').Client;
  2. const Spotify = require('./Spotify');
  3. const mdns = require('mdns');
  4.  
  5. const browser = mdns.createBrowser(mdns.tcp('googlecast'));
  6.  
  7. browser.on('serviceUp', function(service) {
  8. console.log('found device "%s" at %s:%d', service.txtRecord.fn, service.addresses[0], service.port);
  9. if (service.txtRecord.fn === process.env.SPOTFIY_DEVICE) {
  10. ondeviceup(service.addresses[0], service.txtRecord.fn);
  11. browser.stop();
  12. }
  13. });
  14.  
  15. browser.start();
  16.  
  17. function ondeviceup(host, device_name) {
  18. var client = new Client();
  19. client.connect(host, function() {
  20. console.log('connected, launching Spotify app on ' + device_name + '...');
  21. client.launch(Spotify, async(err, player) => {
  22.  
  23. await player.authenticate({
  24. username: process.env.SPOTIFY_USERNAME,
  25. password: process.env.SPOTIFY_PASSWORD,
  26. device_name: process.env.SPOTIFY_DEVICE
  27. });
  28.  
  29. console.log('Authentication OK');
  30.  
  31. const artist = (await player.getAPI().searchArtists('Arcade Fire')).body.artists.items;
  32. await player.play({
  33. context_uri: artist[0].uri
  34. });
  35. console.log('Play OK');
  36.  
  37. setTimeout(() => {
  38. player.pause();
  39. console.log('Pause OK');
  40. }, 10 * 1000);
  41. });
  42. });
  43.  
  44. client.on('error', function(err) {
  45. console.log('Error: %s', err.message);
  46. client.close();
  47. });
  48. }
Add Comment
Please, Sign In to add comment