trakt.tv
Trakt.tv API wrapper for Node.js, featuring:
- All Trakt.tv API v2 methods
- Promises
- Forget JSON: use Objects, Arrays and Strings directly
- Enhanced protection against: CSRF (session riding) and XSS (content spoofing) attacks, using Crypto and Sanitizer
- Plugin extension
For more information about the trakt.tv API, read http://docs.trakt.apiary.io/
Example usage
Setup
npm install trakt.tv
Initialize
const Trakt = require('trakt.tv');
let options = {
client_id: <the_client_id>,
client_secret: <the_client_secret>,
redirect_uri: null,
api_url: null,
useragent: null,
pagination: true
};
const trakt = new Trakt(options);
Add debug: true
to the options
object to get debug logs of the requests executed in your console.
OAUTH
- Generate Auth URL
const traktAuthUrl = trakt.get_url();
-
Authentication is done at that URL, it redirects to the provided uri with a code and a state
-
Verify code (and optionally state for better security) from returned auth, and get a token in exchange
trakt.exchange_code('code', 'csrf token (state)').then(result => {
});
Alternate OAUTH "device" method
trakt.get_codes().then(poll => {
return trakt.poll_access(poll);
}).catch(error => {
});
Refresh token
trakt.refresh_token().then(results => {
});
Storing token over sessions
const token = trakt.export_token();
trakt.import_token(token).then(newTokens => {
});
Revoke token
trakt.revoke_token();
Actual API requests
See methods in methods.json or the docs.
trakt.calendars.all.shows({
start_date: '2015-11-13',
days: '7',
extended: 'full'
}).then(shows => {
});
trakt.search.text({
query: 'tron',
type: 'movie,person'
}).then(response => {
});
trakt.search.id({
id_type: 'imdb',
id: 'tt0084827'
}).then(response => {
});
You can extend your calls with pagination: true
to get the extra pagination info from headers.
trakt.movies.popular({
pagination: true
}).then(movies => {
});
Note: this will contain data
and pagination
for all calls, even if no pagination is available (result.pagination
will be false
). it's typically for advanced use only
Load plugins
When calling new Trakt()
, include desired plugins in an object (must be installed from npm):
const trakt = new Trakt({
client_id: <the_client_id>,
client_secret: <the_client_secret>,
plugins: {
images: require('trakt.tv-images')
}
options: {
images: {
smallerImages: true
}
}
});
The plugin can be accessed with the key you specify. For example trakt.images.get()
.
Write plugins
See the documentation.
Notes
- You can use 'me' as username if the user is authenticated.
- Timestamps (such as token expires property) are Epoch in milliseconds.
LICENSE
The MIT License (MIT) - author: Jean van Kasteel vankasteelj@gmail.com
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.