Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const Client = require('castv2-client').Client;
- const Spotify = require('./Spotify');
- const mdns = require('mdns');
- const browser = mdns.createBrowser(mdns.tcp('googlecast'));
- browser.on('serviceUp', function(service) {
- console.log('found device "%s" at %s:%d', service.txtRecord.fn, service.addresses[0], service.port);
- if (service.txtRecord.fn === process.env.SPOTFIY_DEVICE) {
- ondeviceup(service.addresses[0], service.txtRecord.fn);
- browser.stop();
- }
- });
- browser.start();
- function ondeviceup(host, device_name) {
- var client = new Client();
- client.connect(host, function() {
- console.log('connected, launching Spotify app on ' + device_name + '...');
- client.launch(Spotify, async(err, player) => {
- await player.authenticate({
- username: process.env.SPOTIFY_USERNAME,
- password: process.env.SPOTIFY_PASSWORD,
- device_name: process.env.SPOTIFY_DEVICE
- });
- console.log('Authentication OK');
- const artist = (await player.getAPI().searchArtists('Arcade Fire')).body.artists.items;
- await player.play({
- context_uri: artist[0].uri
- });
- console.log('Play OK');
- setTimeout(() => {
- player.pause();
- console.log('Pause OK');
- }, 10 * 1000);
- });
- });
- client.on('error', function(err) {
- console.log('Error: %s', err.message);
- client.close();
- });
- }
Add Comment
Please, Sign In to add comment