Soundcloud API list my stream - javascript

I'm using the JavaScript SDK for the Soundcloud API and trying to populate the top 10 tracks from authenticated users stream.
Currently I am implementing this with:
SC.get('/tracks', { limit: '10'}, function(tracks) {
//Do stuff with tracks
});
However, this only returns the most recent 10 tracks/sounds uploaded to SoundCloud. Is there an easy way to populate my stream?

I was looking for the same thing, but there is no way to get the complete user's stream. You can get the users activities from
https://api.soundcloud.com/me/activities.json
But there are not all songs listed from your stream. I hope the SoundCloud devs will make that thing better in the future.

Try to use this code
fetch: function () {
var self = this;
SC.get("/tracks", { limit: '10'}, function (data) {
self.tracks = $.map(data, function (track) {
return {
title: track.title
};
});
self.attachTemplate();
});
}
soundcloud.init({
template: $('#tracks-template').html(),
container: $('ul.soundcloud')
});
And show the item title in your HTML page.

Related

webshare API multiple instances on same page

Hopefuly someone can assist/direct me here.
I'm making use of the webshare API on my site. The site contains an array of posts that will have a share link. This is built using a foreach and all have unique urls to use. I want to add a share button to each of those images. I currently have it working on a singular instance but unable to get it to loop through all the share buttons.
Here is the current script:
const shareButton = document.querySelector('.share-button');
const url = document.querySelector('.post-link a').href;
shareButton.addEventListener('click', event => {
if (navigator.share) {
navigator.share({
title: 'Check out this ad I saw on ...',
url
}).then(() => {
console.log('Shared');
})
.catch(console.error);
}
});
I'm really struggling with how to get it to loop through all share buttons and not just be usable on the first instance.
Apologeis if this is simple.
For a start, you need to add a click listener to all buttons, not just the first. You can do this exclusively when the API is supported, else, you may want to hide the buttons. Here's the modified script (note that you need to get the URL of each post individually, see the comment):
const shareButtons = document.querySelectorAll('.share-button');
if ('share' in navigator) {
shareButtons.forEach((shareButton) => {
shareButton.addEventListener('click', () => {
// Get the URL from the dataset or query the DOM.
const url = shareButton.dataset(url);
navigator.share({
title: 'Check out this ad I saw on ...',
url
}).then(() => {
console.log('Shared');
}).catch(console.error);
});
});
} else {
shareButtons.forEach((shareButton) => {
shareButton.style.display = 'none';
});
}

Spotify api add track to playlist

I am using ReactJS and trying to make simple site using the Spotify api.
I am also using the js package spotify-web-api-js. I have succeeded to get the current song playing and able to show it in the browser.
getNowPlaying(){
spotifyApi.getMyCurrentPlaybackState()
.then((response) => {
this.setState({
nowPlaying: {
name: response.item.name,
albumArt: response.item.album.images[0].url
}
});
})
}
//this is from the github link above
Constr.prototype.getMyCurrentPlaybackState = function(options, callback) {
var requestData = {
url: _baseUri + '/me/player'
};
return _checkParamsAndPerformRequest(requestData, options, callback);
};
But the problem I am facing is to add a track to my playlist. I have tried to use the function replaceTracksInPlaylist from spotify-web-api-js but I cant manage to get it to work, I always get the 403 Forbidden error.
addToPlayList(){
spotifyApi.addTracksToPlaylist(listID, listOfSongID, callback);
}
//this is from the github link above
Constr.prototype.replaceTracksInPlaylist = function(playlistId, uris, callback) {
var requestData = {
url: _baseUri + '/playlists/' + playlistId + '/tracks',
type: 'PUT',
postData: { uris: uris }
};
return _checkParamsAndPerformRequest(requestData, {}, callback);
};
The first example also requires authentication as a access token but why does it not work when adding to a playlist?
I have also changed the scopes to the correct ones that the api documentation says i should use, playlist-modify-public and playlist-modify-private
var scope = 'playlist-modify-public playlist-modify-private user-read-private user-read-email user-read-playback-state';
I can test the api call from the api documentation site and insert my oauth token, track and the playlist id there, and that works fine, I also get this back from the call. I cant think of anything else to try and need some help figuring out the next step.

(Reddit API) How to get a list of subreddits user is subscribed to, using snoowrap?

So I'm using snoowrap to write a Chrome extension that gets a list of subreddits the user is subscribed, and subscribes to them on a different account.
I'm trying to get the list of subreddits currently but can't figure out how to do it. I've tried simply getting the JSON from https://www.reddit.com/subreddits/mine.json, which returns an empty object (persumably because no auth) and I have no idea how to do it via snoowrap. I looked through the documentation and can't find an option for it.
My code:
document.addEventListener('DOMContentLoaded', function() {
var login = document.getElementById('login');
login.addEventListener('click', function() {
const r = new snoowrap({
userAgent: '???',
clientId: '<id>',
clientSecret: '<clientsecret>',
username: '<username-here>',
password: '<password-here>'
});
r.getHot().map(post => post.title).then(console.log);
});
var getSubs = document.getElementById('get-subs');
getSubs.addEventListener('click', function() {
fetch('https://www.reddit.com/subreddits/mine.json')
.then(function(data) {
console.log(JSON.stringify(data));
})
.catch(function(error) {
console.log('error');
});
});
});
Not sure how else to try. Anyone have suggestions? I'd like to use snoowrap for this ideally.
When using snoowrap as API wrapper, after connecting to the api with:
const r = new snoowrap({...});
They provide a function for getting your own subscribed subreddits:
r.getSubscriptions();
This will return a Listing Object, which you can use like an Array.

Creating a Set With Soundcloud's API

I'm using Javascript to create a web app with Soundcloud's API for my portfolio. At my current stage I need to be able to create a new set (aka playlist). I was using the sample code from Soundcloud's docs:
SC.connect(function() {
var tracks = [22448500, 21928809].map(function(id) { return { id: id } });
SC.post('/playlists', {
playlist: { title: 'My Playlist', tracks: tracks }
});
});
But I'm getting a 422 error:
Unprocessable Entity - The request looks alright, but one or more of
the parameters looks a little screwy. It's possible that you sent data
in the wrong format (e.g. an array where we expected a string).
But it does not look like anything's missing.
The call to the SoundCloud API requires a callback function in addition to the playlist title and tracks. Your code should look like this:
SC.connect(function() {
var tracks = [22448500, 21928809].map(function(id) { return { id: id } });
SC.post('/playlists', {
playlist: { title: 'My Playlist', tracks: tracks }, function(response){
console.log(response)
}
});
});
Their example is, unfortunately, wrong.

How to Create a Soundcloud playlist ( set ) using javascript?

I am trying to use Soundcloud api for my application where user can create his/her own playlist of track . As a test case the example I am testing is almost exactly taken from the Soundcloud dev docs. below is my code
<script src="http://connect.soundcloud.com/sdk.js"></script>
<script>
// initialize client with app credentials
SC.initialize({
client_id: 'MY_CLIENT_ID',
redirect_uri: 'http://localhost.local/~****/sc/callback.html'
});
// initiate auth popup and create new playlist
SC.connect(function() {
SC.get('/me', function(me) {
console.log(me.username);
});
var tracks = [12573606].map(function(id) { return { id: id }; });
SC.post('/playlists', {
playlist: { title: 'My Playlist', tracks: tracks }
});
});
I already searched so many thing's in google but nothing helped me, actually i need temporary playlist so when user logout or close the browser playlist also delete . Any help would be appreciable.. thanx
Can you try sending the client_secret => 'YOUR_CLIENT_SECRET' as part of the initialize call
It doesn't look like there is such a thing in soundcloud to have a temporary playlist, you will need to delete the playlist on exit....
The best guess would be to see how to format your url to match when someone clicks on the delete button which you can find here: http://help.soundcloud.com/customer/portal/articles/282512-how-do-i-delete-tracks-from-my-account- I don't have an account so I can't test this part.
The key to creating the playlist is having the right track ids. It is possible if your having an issue that the track id doesn't exist and so it doesn't get added to the playlist.
<script src="http://connect.soundcloud.com/sdk.js"></script>
<script>
// initialize client with app credentials
SC.initialize({
client_id: 'MY_CLIENT_ID',
redirect_uri: 'http://localhost.local/~****/sc/callback.html'
});
// initiate auth popup and create new playlist
SC.connect(function() {
SC.get('/me', function(me) {
console.log(me.username);
});
var tracks = [12573606].map(function(id) { return { id: id }; });
SC.post('/playlists', {
playlist: { title: 'My Playlist', tracks: tracks }
});
//As you can see it is a simple array of ids of track
tracks = [21778201, 22448500, 21928809];
//To get all playlists remove limit and search each playlist name and grab id
SC.get('/me/playlists', { limit: 1 }, function(playlist) {
SC.put(playlist.uri, { playlist: { tracks: tracks } });
});
//Then to get all the tracks, please substitute the playlist id for 1234323:
SC.get('/playlists/1234323', function(playlist) {
for (var i = 0; i < playlist.tracks.length; i++) {
console.log(playlist.tracks[i].length);
}
});
});

Categories

Resources