Does the Spotify platform plan to offer any webhooks for third party developers to listen in to changes in user libraries, playlists or the player?
What are some known workarounds if webhooks don't exist?
There are no webhooks in the Web API and I think it will be quite a while before the feature is added https://github.com/spotify/web-api/issues/538
Playlists on Spotify are versioned by a snapshot_id. When you make an API request to get a playlist, one of the returned fields is snapshot_id, which identifies the playlist version. If you store this version ID for each playlist, you can check if the playlist has changed remotely since the last time you checked (the snapshot_id would change if so)
It's important to remember that any changes you make will also affect the snapshot_id. So you will need to:
Get the Playlist
Does your locally saved snapshot_id differ from the current one for the playlist?
If it does, maybe make some changes
After you make your changes, get the playlist again
Store the snapshot_id from the Get Playlist after you made your changes
I don't think snapshot_id is supported for a user's library - you can check a subset of the user's library to detect any additions (e.g. if the first 50 songs in the library haven't changed, then no new songs have been added, and you only had to make one API request instead of umpteen), but you will miss deletions from the rest of the library if you only do this. You can also check the count of songs in the library, which would tell you when songs have been added or deleted, except in the edge case where they add and delete the same number of songs. But this means they have added a song and checking the first 50 songs in the library would catch this.
You can poll for currently playing and recently played songs. If you want to track every song a user listens to, you can achieve this by polling Recently Played at least every 30 * 50 seconds (25 mins). 30 is the number of seconds it takes to listen to a song before Spotify records it as a Listen and 50 is the max number of recently played songs it will serve you.
To know when a track finished playing I set an await function to wait for the duration of that track, and then continue after thet
const delay = ms => new Promise(resolve => setTimeout(resolve, ms))
//start playing a track
let device_status = await spotifyApi.getMyCurrentPlaybackState().then(result =>{
return result.body;
})
let time_left = device_status.item.duration_ms - device_status.progress_ms;
await delay(time_left)
//whatever needs to be done after
Related
component I'm making:
I'm making a booking component with a modal. It has a timesheet and options to choose various locations. every time user changes the location, another timesheet is fetched for each location.
network flow of the page:
download location: it fetches for all locations customers can choose from in the db and stores locations state. (this takes about 900ms).
then download timesheet: once locations is set, it selects a default location information then fetches other requests to download 7 days worth of timesheets. (this takes another 900ms, but only 6 async calls are made at a time. the last one is executive after some delay. in total the whole fetch takes about 1800ms). this piece executes after locations update using
useEffect(()=>{ //some code// }, [locations])
page loads up
problem
evidently, this is too slow. the whole thing takes about 2.5 seconds to 3 seconds to load. i am thinking about ways to improve the time it takes for the page to load.
solutions I was thinking
I could just start downloading all the timesheet data before location data is downloaded. I think I can store the default location information somewhere in the site.
option 1) I think I can store default location data in the redux (stored in user schema and gets updated to redux when a user logs in so that it can be used when timesheet component needs to load without having to wait for location data to be downloaded). the problem is i'm now replicating the same location data within the db. maybe it's not a problem, but my OCD wants to do without replicating anything.
option 2) I can get rid of the user specific default data. I think I can just store sth like var defaultLocation = { id: // objectId of a location// } in the component code itself but i'm losing the user specificity, which would be a very nice feature to have.
option 3) ??? I feel like I'm facing a trade off here, but I'm hoping if there would be any other solutions. Any tips and helps would be much appreciated!
When I'm fetching my listening history with this URL : https://api.spotify.com/v1/me/player/recently-played?limit=50
I'm getting a "next" attribute from the response to fetch the previous data.
When I'm fetching API with this "next" url (i.e https://api.spotify.com/v1/me/player/recently-played?before=1557334757252&limit=50), it returns an empty tracks array.
I don't understand why I'm getting an empty array.
PS : I obviously listened to more than 50 songs
Returns the most recent 50 tracks played by a user. Note that a track
currently playing will not be visible in play history until it has
completed. A track must be played for more than 30 seconds to be
included in play history.
Spotify stores 50 recent tracks information only. Since you already retrieved 50 records there is no use for using after or before cursor.Use limit which is not 50 then you can use the cursors.
I am trying to remove a track from a playlist and the information I know about the track is the spotify URI, the name of the track and the artist. However, I don't know the position of the track in the playlist.
The function to remove the track is the following:
spotify.Playlists.remove_tracks(tracks)
Parameters: tracks (list of int) – A list of track positions to be removed from the playlist
I haven't found any callbacks or class method that would provide this information except upon adding the track to the playlist, which may have happened in another session.
Has anyone done this successfully and if so, could you point me to how you were able to do it?
Thanks
I haven't used PySpotify myself, but looking at the docs, it says that a Playlist is an iterable sequence of Tracks. I think you should be able to iterate over the tracks and use str(spotify.Link.from_track(track)) to get the link for each track. Then you can easily enough look for the indices that match your track URI.
I'm working on an instagram scraper for something and I'm trying to figure out if it's possible to get all photos for a tag that have an id or timestamp later than the last one I have.
The instagram API docs are useless in that they don't have any real info on pagination (which I presume I'll have to abuse).
Does anyone have any ideas?
I've been slogging through the Instagram API for the last couple of days so here's my 2 cents worth:
As far as I can see it if you call the api with /tags/tag-name/media/recent it only return a list if items. If the amount exceeds about 25 you have to make another request with the pagination value returned in the previous request.
In order to gain some control I am initially iterating through all images and storing the results (just the URL not the actual image) to a database. Now I can manipulate however I want. When I feel like updating (I'm doing it manually now but could be a cron job or use the real-time api) I re-read all the images, compare to what I have in my DB and add possible new images. My app then reads out the url and info from my DB (which btw is a heck of a lot faster than going through the instagram api, which will only return about 25 images per request - regardless of any 'count' parameter value you put in the request url) and displays it.
I am developing this for a client who is afraid of people posting nsfw or whatever pics using their dedicated hashtag (for a contest) - with the above set up I can offer them an interface where they can check and mark images that are then displayed in the app.
One thing to watch out for is when a user deletes his picture; you will have to find a way to check for this. Currently (since I'm lazy) I load all images and use jquery to check for an error loading the image. If there is one I delete the image from the DB (via ajax).
I'm not sure the pagination is going to help you: as far as I can see the pagination response has no relation to the id's of the actual image objects on each page - so theoretically a pagination id that jumps to a certain page (i.e. date) might not work tomorrow if enough images have been deleted in the mean time.
to get all images instead of latest 20, just append &count=-1 to your api call - it's that simple.
In either case, there is a timestamp on each json object - or if you prefer, you can use max_tag_id
check out my post here: there any way to show more than 20 photos of the instagram API?
* Update April 2014: count=-1 is no longer available.
I'm trying to use the existing example browse.c to get the number of albums of an artist by given an artist uri. The return value of sp_artistbrowse_num_albums(browse) is different every time when I run the command browse spotify:artist:3fMbdgg4jU18AjLCKBhRSm. Why?
Do I understand correctly that the callback artistbrowse_complete_cb is invoked once only when browsing is complete, but callback metadata_updated is invoked whenever metadata is updated?
Thanks.
Do I understand correctly that the callback artistbrowse_complete_cb is invoked once only when browsing is complete, but callback metadata_updated is invoked whenever metadata is updated?
That's correct. As I've said before, metadata_updated means "some metadata has been updated somewhere", and there are more specific callbacks like artistbrowse_complete_cb for more targeted operations like artist browsing.
As to why it keeps changing — it's a bit odd as the number of albums for an artist doesn't normally oscillate much, but albums can be added and removed on a daily basis. It's best to just accept what you're told and keep your UI up-to-date.