Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Huge refactoring #35

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 88 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
# TidalAPI
# TidalPromise

[![Build Status](https://img.shields.io/travis/lucaslg26/TidalAPI.svg)](https://travis-ci.org/lucaslg26/TidalAPI) [![npm version](http://img.shields.io/npm/v/tidalapi.svg)](https://npmjs.org/package/tidalapi) [![npm downloads](https://img.shields.io/npm/dm/tidalapi.svg)](https://npmjs.org/package/tidalapi) [![NPM](https://img.shields.io/npm/l/tidalapi.svg)](https://github.com/lucaslg26/TidalAPI/blob/master/LICENSE.md) [![David](https://img.shields.io/david/lucaslg26/TidalAPI.svg)](https://david-dm.org/lucaslg26/TidalAPI)
[![Build Status](https://img.shields.io/travis/deters/TidalPromise.svg)](https://travis-ci.org/deters/TidalPromise) [![npm version](http://img.shields.io/npm/v/deters.svg)](https://npmjs.org/package/tidalpromise) [![npm downloads](https://img.shields.io/npm/dm/deters.svg)](https://npmjs.org/package/tidalpromise) [![NPM](https://img.shields.io/npm/l/deters.svg)](https://github.com/deters/TidalPromise/blob/master/LICENSE.md) [![David](https://img.shields.io/david/lucaslg26/TidalAPI.svg)](https://david-dm.org/lucaslg26/TidalAPI)

## About

node.js TIDAL API. Use the TIDAL Web API simply using this module ;)
node.js TIDAL API, with Promises and playlist manipulation support.

Created by [Lucas Vasconcelos](https://github.com/lucaslg26)
Mantainer [Lucas Deters](https://github.com/deters)

This is a fork of node.js Tidal API by [Lucas Vasconcelos](https://github.com/lucaslg26)

**NOTE:** Currently not supporting facebook login.

## How to use
Run the following:

``` javascript
npm install tidalapi
npm install tidalpromise
```

## Obtain the Tidal token needed to use this API
Expand All @@ -35,7 +37,7 @@ As well as a TIDAL username and password, the Tidal API needs an API `token` whi
Simple usage searching and querying a track list

```javascript
var TidalAPI = require('tidalapi');
var TidalAPI = require('tidalpromise');

var api = new TidalAPI({
username: 'your-username-here',
Expand All @@ -49,43 +51,43 @@ var api = new TidalAPI({
### Search

```javascript
api.search({type: 'artists', query: 'Dream Theater', limit: 1}, function(data){
api.search({type: 'artists', query: 'Dream Theater', limit: 1}).then( function(data){
console.log(data.artists);
})
}).catch(console.log)

api.search({type: 'albums', query: 'Dream Theater', limit: 1}, function(data){
api.search({type: 'albums', query: 'Dream Theater', limit: 1}).then( function(data){
console.log(data.albums);
})
}).catch(console.log)

api.search({type: 'tracks', query: 'Dream Theater', limit: 1}, function(data){
api.search({type: 'tracks', query: 'Dream Theater', limit: 1}).then( function(data){
console.log(data.tracks);
})
}).catch(console.log)

api.search({type: 'tracks,albums,artists', query: 'Dream Theater', limit: 1}, function(data){
api.search({type: 'tracks,albums,artists', query: 'Dream Theater', limit: 1}).then( function(data){
console.log(data.tracks);
console.log(data.albums);
console.log(data.artists);
})
}).catch(console.log)
```

### Track info

```javascript
api.getTrackInfo({id: 22560696 }, function(data){
api.getTrackInfo({id: 22560696 }).then( function(data){
console.log(data)
})
}).catch(console.log)
```

### Streams

```javascript
api.getStreamURL({id: 22560696}, function(data){
api.getStreamURL({id: 22560696}).then( function(data){
console.log(data)
})
}).catch(console.log)

api.getVideoStreamURL({id: 25470315}, function(data){
api.getVideoStreamURL({id: 25470315}).then( function(data){
console.log(data)
})
}).catch(console.log)
```

### Album Art
Expand All @@ -97,18 +99,80 @@ console.log(api.getArtURL('24f52ab0-e7d6-414d-a650-20a4c686aa57', 1280)) //cover
### Videos

```javascript
api.getArtistVideos({id: 14670, limit: 2}, function(data){
api.getArtistVideos({id: 14670, limit: 2}).then( function(data){
console.log(data)
})
}).catch(console.log)
```

### FLAC tags

```javascript
api.genMetaflacTags({id: 22560696, coverPath: './albumart.jpg', songPath: './song.flac'}, function(data){
api.genMetaflacTags({id: 22560696, coverPath: './albumart.jpg', songPath: './song.flac'}).then( function(data){
console.log(data)
/* --remove-all-tags --set-tag="ARTIST=Dream Theater" --set-tag="TITLE=Along For The Ride" --set-tag="ALBUM=Dream Theater" --set-tag="TRACKNUMBER=8" --set-tag="COPYRIGHT=2013 Roadrunner Records, Inc." -set-tag="DATE=2013" --import-picture-from="./albumart.jpg" "./song.flac" --add-replay-gain */
})
}).catch(console.log)
```

### Playlist manipulation (example using async/await for clarity)

```javascript
// get the id of the current user
let user_id = await api.getMyID();
console.log(user_id);

// query more information about current user
let user = await api.getUser({id: user_id});
console.log(user);

// get all playlists of current user
let result = await api.getPlaylists({id: user_id});
let current_playlists = result.items;
let playlists_resume = current_playlists.map(playlist => playlist.title).filter((value, index) => index < 5);
console.log('listing first 5 playlists of current user: \n',playlists_resume)

// search some musics
let track_results = await api.search({type: 'tracks', query: 'The Beatles', limit: 10});
let beatles_tracks = track_results.tracks.items;
let beatles_tracks_resume = beatles_tracks.map(playlist => playlist.title);
console.log('list of beatles tracks:',beatles_tracks_resume);

// lookup for a specific playlist
let TEST_PLAYLIST = 'Test playlist'
let filtered_playlists = current_playlists.filter( playlist => playlist.title == TEST_PLAYLIST );
let test_playlist = filtered_playlists[0]

// create a new playlist if necessary
if (test_playlist == undefined){
test_playlist = await api.createPlaylist({title: TEST_PLAYLIST, description: 'Automatically created playlist.'});
console.log('created a new playlist.')
}
console.log('test playlist uuid is:' + test_playlist.uuid)

// add musics to the beginning of the playlist
result = await api.addPlaylistTracks({playlist: test_playlist, tracks: beatles_tracks, toIndex: 0});
console.log(beatles_tracks.length + ' tracks added to playlist ', result);

// get tracks in the playlist
result = await api.getPlaylistTracks({playlist: test_playlist})
let musics_on_test_playlist = result.items;
let number_of_musics_on_test_playlist = result.totalNumberOfItems;
console.log('Musics on the playlist now : \n',number_of_musics_on_test_playlist);

// we added new music to the beginning. so we can delete old musics from the end.
let number_of_musics_to_keep = beatles_tracks.length;
let number_of_musics_to_delete = number_of_musics_on_test_playlist - number_of_musics_to_keep;
let positions_to_delete = [...Array(number_of_musics_to_delete).keys()].map(v => v+number_of_musics_to_keep);
result = await api.deletePlaylistTracks({playlist: test_playlist, trackPositions: positions_to_delete});
console.log(number_of_musics_to_delete + ' tracks deleted from the playlist');

// get tracks in the playlist again
result = await api.getPlaylistTracks({playlist: test_playlist})
musics_on_test_playlist = result.items;
number_of_musics_on_test_playlist = result.totalNumberOfItems;
console.log('Musics on the playlist now : \n',number_of_musics_on_test_playlist);

result = await api.deletePlaylist(test_playlist);
console.log('Playlist deleted. \n')
```

## Troubleshooting
Expand Down
8 changes: 8 additions & 0 deletions examples/.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"tidal": {
"username": "",
"password": "",
"token": "",
"quality": "LOSSLESS"
}
}
72 changes: 72 additions & 0 deletions examples/playlists.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
let TidalClient = require('../');
let credentials = require('./.config.json');

let api = new TidalClient(credentials.tidal);

async function test_playlists(delete_playlist_after_testing) {

// get the id of the current user
let user_id = await api.getMyID();
console.log(user_id);

// query more information about current user
let user = await api.getUser({id: user_id});
console.log(user);

// get all playlists of current user
let result = await api.getPlaylists({id: user_id});
let current_playlists = result.items;
let playlists_resume = current_playlists.map(playlist => playlist.title).filter((value, index) => index < 5);
console.log('listing first 5 playlists of current user: \n',playlists_resume)

// search some musics
let track_results = await api.search({type: 'tracks', query: 'The Beatles', limit: 10});
let beatles_tracks = track_results.tracks.items;
let beatles_tracks_resume = beatles_tracks.map(playlist => playlist.title);
console.log('list of beatles tracks:',beatles_tracks_resume);

// lookup for a specific playlist
let TEST_PLAYLIST = 'Test playlist'
let filtered_playlists = current_playlists.filter( playlist => playlist.title == TEST_PLAYLIST );
let test_playlist = filtered_playlists[0]

// create a new playlist if necessary
if (test_playlist == undefined){
test_playlist = await api.createPlaylist({title: TEST_PLAYLIST, description: 'Automatically created playlist.'});
console.log('created a new playlist.')
}
console.log('test playlist uuid is:' + test_playlist.uuid)

// add musics to the beginning of the playlist
result = await api.addPlaylistTracks({playlist: test_playlist, tracks: beatles_tracks, toIndex: 0});
console.log(beatles_tracks.length + ' tracks added to playlist ', result);

// get tracks in the playlist
result = await api.getPlaylistTracks({playlist: test_playlist})
let musics_on_test_playlist = result.items;
let number_of_musics_on_test_playlist = result.totalNumberOfItems;
console.log('Musics on the playlist now : \n',number_of_musics_on_test_playlist);

// we added new music to the beginning. so we can delete old musics from the end.
let number_of_musics_to_keep = beatles_tracks.length;
let number_of_musics_to_delete = number_of_musics_on_test_playlist - number_of_musics_to_keep;
let positions_to_delete = [...Array(number_of_musics_to_delete).keys()].map(v => v+number_of_musics_to_keep);
result = await api.deletePlaylistTracks({playlist: test_playlist, trackPositions: positions_to_delete});
console.log(number_of_musics_to_delete + ' tracks deleted from the playlist');

// get tracks in the playlist again
result = await api.getPlaylistTracks({playlist: test_playlist})
musics_on_test_playlist = result.items;
number_of_musics_on_test_playlist = result.totalNumberOfItems;
console.log('Musics on the playlist now : \n',number_of_musics_on_test_playlist);

if (delete_playlist_after_testing) {
result = await api.deletePlaylist(test_playlist);
console.log('Playlist deleted. \n')
}

}

let DELETE_PLAYLIST_AFTER_TEST = true;

test_playlists(DELETE_PLAYLIST_AFTER_TEST);
79 changes: 41 additions & 38 deletions examples/search.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,53 @@
var TidalAPI = require('../');
let TidalClient = require('../');
let credentials = require('./.config.json');

let api = new TidalClient(credentials.tidal);

var api = new TidalAPI({
username: '',
password: '',
token: '_KM2HixcUBZtmktH',
clientVersion: '2.2.1--7',
quality: 'LOSSLESS'
});
async function test_search(playlist_name, query_musics) {

api.search({type: 'artists', query: 'Dream Theater', limit: 1}, function(data){
console.log(data.artists);
})

api.search({type: 'albums', query: 'Dream Theater', limit: 1}, function(data){
console.log(data.albums);
})
api.search({type: 'artists', query: 'Dream Theater', limit: 1}).then( data => {
console.log(data.artists);
})

api.search({type: 'tracks', query: 'Dream Theater', limit: 1}, function(data){
console.log(data.tracks);
})
api.search({type: 'albums', query: 'Dream Theater', limit: 1}).then( data => {
console.log(data.albums);
})

api.search({type: 'tracks,albums,artists', query: 'Dream Theater', limit: 1}, function(data){
console.log(data.tracks);
console.log(data.albums);
console.log(data.artists);
})
api.search({type: 'tracks', query: 'Dream Theater', limit: 1}).then( data => {
console.log(data.tracks);
})

api.getTrackInfo({id: 22560696 }, function(data){
console.log(data)
})
api.search({type: 'tracks,albums,artists', query: 'Dream Theater', limit: 1}).then( data => {
console.log(data.tracks);
console.log(data.albums);
console.log(data.artists);
})

api.getStreamURL({id: 22560696}, function(data){
console.log(data)
})
api.getTrackInfo({id: 22560696 }).then( data => {
console.log(data)
})

api.getVideoStreamURL({id: 25470315}, function(data){
console.log(data)
})
api.getStreamURL({id: 22560696}).then( data => {
console.log(data)
})

console.log(api.getArtURL('24f52ab0-e7d6-414d-a650-20a4c686aa57', 1280)) //coverid
api.getVideoStreamURL({id: 25470315}).then( data => {
console.log(data)
})

api.getArtistVideos({id: 14670, limit: 2}, function(data){
console.log(data)
})
console.log(api.getArtURL('24f52ab0-e7d6-414d-a650-20a4c686aa57', 1280)) //coverid

api.genMetaflacTags({id: 22560696, coverPath: './albumart.jpg', songPath: './song.flac'}, function(data){
console.log(data)
})
api.getArtistVideos({id: 14670, limit: 2}).then( data => {
console.log(data)
})

api.genMetaflacTags({id: 22560696, coverPath: './albumart.jpg', songPath: './song.flac'}).then( data => {
console.log(data)
})



}

test_search();
Loading