Skip to content
This repository has been archived by the owner on Sep 17, 2020. It is now read-only.

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
stickpin committed Jan 14, 2020
0 parents commit 250124a
Show file tree
Hide file tree
Showing 147 changed files with 18,878 additions and 0 deletions.
Binary file added .DS_Store
Binary file not shown.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 stickpin

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
146 changes: 146 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
# node-appletv-x

> A node module for interacting with an Apple TV (4th-generation or later) over the Media Remote Protocol.
[![License][license-image]][license-url]

![](images/pairing.gif)

## Disclaimer

Original project by [@edc1591](https://twitter.com/edc1591).
Code includes some fixes made by: [GioCirque](https://github.com/GioCirque/node-appletv), [casey-chow](https://github.com/casey-chow/node-appletv) and [SeppSTA](https://github.com/SeppSTA/node-appletv)
The code delivered AS-IS, due to lack of time I am not planning to provide any support, feel free to do with it whatever you want. :)

## Overview

`node-appletv-x` is a `node.js` implementation of the Media Remote Protocol which shipped with the 4th-generation Apple TV. This is the protocol that the Apple TV remote app uses, so this should enable the creation of an Apple TV remote app for various platforms. It can also be used in a `homebridge` plugin to connect Apple TV events to HomeKit and vice versa. `node-appletv-x` can be used as a standalone command line application, or as a module in your own node app. Keep reading for installation and usage instructions.

This version adding following funtionality:
* Works with latest NodeJS (v12.x and newer) & Modules.
* Fix duplicate AppleTV entries
* Additinal buttons are added: select, tv, longtv
* Support for tvOS 13.3 and newer
* Various fixes

## Documentation

Developer documentation for `node-appletv-x` can be found /docs/ folder.

## Usage

### As a standalone cli

```bash
# Install
$ sudo apt-get install libtool autoconf automake libavahi-compat-libdnssd-dev
$ sudo npm install -g node-appletv-x --unsafe-perm

# Display built-in help
$ appletv --help
```

The `appletv` cli supports several commands, such as:

`pair`: Scans for Apple TVs on the local network and initiates the pairing process

`command <command>`: Execute a command on an Apple TV (play, pause, menu, etc.)
Example: `appletv --credentials '<your_pairing_token>' command tv`

`state`: Logs state changes from an Apple TV (now playing info)

`queue`: Requests the current playback queue from an Apple TV

`messages`: Logs all raw messages from an Apple TV

`help <command>`: Get help for a specific command


### As a node module

```bash
$ npm install --save node-appletv-x
```

`node-appletv-x` makes heavy use of Promises. All functions, except for the observe functions, return Promises.

### Examples

#### Scan for Apple TVs and pair

```typescript
import { scan } from 'node-appletv';

return scan()
.then(devices => {
// devices is an array of AppleTV objects
let device = devices[0];
return device.openConnection()
.then(device => {
return device.pair();
})
.then(callback => {
// the pin is provided onscreen from the Apple TV
return callback(pin);
});
})
.then(device => {
// you're paired!
let credentials = device.credentials.toString();
console.log(credentials);
})
.catch(error => {
console.log(error);
});
```

#### Connect to a paired Apple TV

```typescript
import { scan, parseCredentials, NowPlayingInfo } from 'node-appletv';

// see example above for how to get the credentials string
let credentials = parseCredentials(credentialsString);

return scan(uniqueIdentifier)
.then(devices => {
let device = devices[0];
return device.openConnection(credentials);
})
.then(device => {
// you're connected!
// press menu
return device.sendKeyCommand(AppleTV.Key.Menu);
})
.then(device => {
console.log("Sent a menu command!");

// monitor now playing info
device.on('nowPlaying', (info: NowPlayingInfo) => {
console.log(info.toString());
});
})
.catch(error => {
console.log(error);
});
```

The `uniqueIdentifier` is advertised by each Apple TV via Bonjour. Use an app like [Bonjour Browser](http://www.tildesoft.com) to find it. The identifier is also the first value in the string value of the `Credentials` object.

See [homebridge-theater-mode](https://github.com/edc1591/homebridge-theater-mode) for a more practical use of this module.

## Acknowledgments

`node-appletv-x` would not have been possible without the work of these people:

* [Jean Regisser](https://github.com/jeanregisser) who reversed the protobuf [spec of the MediaRemoteTV protocol](https://github.com/jeanregisser/mediaremotetv-protocol)
* [Pierre Ståhl](https://github.com/postlund) who [implemented the protocol in Python](https://github.com/postlund/pyatv)
* [Khaos Tian](https://github.com/KhaosT) for [reversing the HomeKit protocol](https://github.com/KhaosT/HAP-NodeJS) which also uses SRP encryption
* [Zach Bean](https://github.com/forty2) for [implementing the HAP client spec](https://github.com/forty2/hap-client)

## Meta

Distributed under the MIT license. See ``LICENSE`` for more information.

[license-image]: https://img.shields.io/badge/License-MIT-blue.svg
[license-url]: LICENSE
Binary file added bin/.DS_Store
Binary file not shown.
3 changes: 3 additions & 0 deletions bin/appletv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env node

require('../dist/bin/index.js');
Binary file added dist/.DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions dist/bin/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {};
180 changes: 180 additions & 0 deletions dist/bin/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const caporal = require("caporal");
let cli = caporal;
const appletv_1 = require("../lib/appletv");
const credentials_1 = require("../lib/credentials");
const scan_1 = require("./scan");
const pair_1 = require("./pair");
cli
.version('1.0.11')
.command('pair', 'Pair with an Apple TV')
.option('--timeout <timeout>', 'The amount of time (in seconds) to scan for Apple TVs', cli.INTEGER)
.action((args, options, logger) => {
scan_1.scan(logger, options.timeout)
.then(device => {
device.on('debug', (message) => {
logger.debug(message);
});
device.on('error', (error) => {
logger.error(error.message);
logger.debug(error.stack);
});
return pair_1.pair(device, logger)
.then(keys => {
logger.info("Credentials: " + device.credentials.toString());
process.exit();
});
})
.catch(error => {
logger.error(error.message);
logger.debug(error.stack);
process.exit();
});
});
cli
.command('command', 'Send a command to an Apple TV')
.argument('<command>', 'The command to send', /^up|down|left|right|menu|play|pause|next|previous|suspend|select|tv|longtv$/)
.option('--credentials <credentials>', 'The device credentials from pairing', cli.STRING)
.action((args, options, logger) => {
if (!options.credentials) {
logger.error("Credentials are required. Pair first.");
process.exit();
}
let credentials = credentials_1.Credentials.parse(options.credentials);
scan_1.scan(logger, null, credentials.uniqueIdentifier)
.then(device => {
device.on('debug', (message) => {
logger.debug(message);
});
device.on('error', (error) => {
logger.error(error.message);
logger.debug(error.stack);
});
return device
.openConnection(credentials)
.then(() => {
return device
.sendKeyCommand(appletv_1.AppleTV.key(args["command"]))
.then(result => {
logger.info("Success!");
process.exit();
});
});
})
.catch(error => {
logger.error(error.message);
logger.debug(error.stack);
process.exit();
});
});
cli
.command('state', 'Logs the playback state from the Apple TV')
.option('--credentials <credentials>', 'The device credentials from pairing', cli.STRING)
.action((args, options, logger) => {
if (!options.credentials) {
logger.error("Credentials are required. Pair first.");
process.exit();
}
let credentials = credentials_1.Credentials.parse(options.credentials);
scan_1.scan(logger, null, credentials.uniqueIdentifier)
.then(device => {
device.on('debug', (message) => {
logger.debug(message);
});
device.on('error', (error) => {
logger.error(error.message);
logger.debug(error.stack);
});
return device
.openConnection(credentials);
})
.then(device => {
device.on('nowPlaying', (info) => {
logger.info(info.toString());
});
})
.catch(error => {
logger.error(error.message);
logger.debug(error.stack);
process.exit();
});
});
cli
.command('queue', 'Request the playback state from the Apple TV')
.option('--credentials <credentials>', 'The device credentials from pairing', cli.STRING)
.option('--location <location>', 'The location in the queue', cli.INTEGER)
.option('--length <length>', 'The length of the queue', cli.INTEGER)
.option('--metadata', 'Include metadata', cli.BOOLEAN)
.option('--lyrics', 'Include lyrics', cli.BOOLEAN)
.option('--languages', 'Include language options', cli.BOOLEAN)
.action((args, options, logger) => {
if (!options.credentials) {
logger.error("Credentials are required. Pair first.");
process.exit();
}
let credentials = credentials_1.Credentials.parse(options.credentials);
scan_1.scan(logger, null, credentials.uniqueIdentifier)
.then(device => {
device.on('debug', (message) => {
logger.debug(message);
});
device.on('error', (error) => {
logger.error(error.message);
logger.debug(error.stack);
});
return device
.openConnection(credentials);
})
.then(device => {
return device
.requestPlaybackQueue({
location: options.location || 0,
length: options.length || 1,
includeMetadata: options.metadata,
includeLyrics: options.lyrics,
includeLanguageOptions: options.languages
});
})
.then(message => {
logger.info(message);
})
.catch(error => {
logger.error(error.message);
logger.debug(error.stack);
process.exit();
});
});
cli
.command('messages', 'Log all messages sent from the Apple TV')
.option('--credentials <credentials>', 'The device credentials from pairing', cli.STRING)
.action((args, options, logger) => {
if (!options.credentials) {
logger.error("Credentials are required. Pair first.");
process.exit();
}
let credentials = credentials_1.Credentials.parse(options.credentials);
scan_1.scan(logger, null, credentials.uniqueIdentifier)
.then(device => {
device.on('debug', (message) => {
logger.debug(message);
});
device.on('error', (error) => {
logger.error(error.message);
logger.debug(error.stack);
});
return device
.openConnection(credentials);
})
.then(device => {
device.on('message', (message) => {
logger.info(JSON.stringify(message.toObject(), null, 2));
});
})
.catch(error => {
logger.error(error.message);
logger.debug(error.stack);
process.exit();
});
});
cli.parse(process.argv);
2 changes: 2 additions & 0 deletions dist/bin/pair.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { AppleTV } from '../lib/appletv';
export declare function pair(device: AppleTV, logger: Logger): Promise<AppleTV>;
Loading

0 comments on commit 250124a

Please sign in to comment.