[Custom API] Save current song to user's personal list

Hello. So this was more of a self-challenge I did just to get back to coding after a loooong break - mostly to get a bit of understanding of how modern things like oauth2, bootstraps, etc work. But I thought that maybe it’ll be useful for someone else, so I tried to quickly pack it in shareable form.

What it does:
Allows you to add a custom comand to your nightbot that would allow your chat to save current song to their personal list. And not just for stream owner, but for the viewers as well (depending on permissions).

Why would they need it? Well, as someone who mostly hangs out on multiple creative/music streams, I personally always wanted it to be easier to save new songs I heard and liked. And the constant need to copypasta to random notes which I keep forgetting about was pretty annoying.

Isn’t it easier to just type !save and be done with it? Well, now you can:
https://feq.ru/savebot/songlist.php?u=Enbis - here’s an example of how it may look like (don’t mind the songs, they are random).
https://feq.ru/savebot/auth.php - and here’s the install link.

It (edit: no longer requires) extra steps like creating an app and (edit) will now automatically refresh the token.

Can be used from my host, but you can (and, if you have the means, it’s better to) upload it on your own. Source code (link at the auth page) is open and free to use, modify, do whatever you please with it. It’s not exactly a big thing, I’ve spent only couple evenings of free time on it, so obviously there won’t be any copyright hassle over it.

As of right now, I’d give about 70% chance something will go wrong, since I only tested it on myself. But I’ll try to fix whatever I can.

Update: if you used this bot before 10.11.2019 and suddenly discovered it doesn’t work, it’s because I had to move hosting. It still works, just update the !save command to this:
$(urlfetch https://feq.ru/savebot/save.php?u=$(user)&c=$(channel)&q=$(querystring))

1 Like

Here’s some feedback:

  • Upload your source code to a git repo for version control. People typically use GitHub.
  • Use prepared statements when doing any database queries. As it stands right now a YouTube track title/artist makes you vulnerable to SQL injection.
  • Use OAuth2 authorization properly. Users should not be making their own applications, they should be using yours.
  • Support OAuth2 refresh tokens. Users will never need to reauthenticate if you supported this.

That’s useful feedback, thank you!
Yeah, I thought about github, but was too lazy to reg there for years since I stopped coding, maybe this is a good reason to :slight_smile:
Couldn’t figure out how to do it without making an application for the user since it has to be their channel’s queue, not mine.
I tried refresh token at first, but it didn’t work on first try so I thought there’s little reason to, and it didn’t seem much different from re-authentication anyway. But will take a look at it again.

1 Like

Well, since this piece of failcode ended up being actively used in one chat recently, I tried to work on this a bit more, and hopefully fixed quite a few issues with it so far.

  • It no longer requires separate app created by the user. Don’t even ask how that happened. Oh, actually probably it’s best to explain in case someone else ends up going same route: for some reason at first I couldn’t make it to accept my testing account’s authorisation from the same clientid/secret. It always redirected me on the same twitch account authorisation, and chosing other account still redirected me back again and again. Relogging to test acc and creating there app seemed to fix it. So my train of thought was that it’s not supposed to give other people’s access to your queue in first place. Anyway, as it turns out, that’s not the case. It worked fine once I switched to using two browsers for different accounts for testing.

  • No longer asking for channel’s name, but requests channel permissions instead. Both resulted in a good deal simplified install process that is now just one click away.

  • Supports refresh tokens. If it goes as planned, should automatically refresh ~once a week, depending on !save command usage. Also has manual refresh on the auth page.

  • No longer should be vulnerable to quotes. While I couldn’t yet figure out what’s the deal with prepared statements, there’s a good ol’ regular expressions and str_replace as a temporary fix. I’ll get back to those later.

  • There’s no longer need for 2 commands, !save command supports the inner command thing, whatever it’s called. So !save mylist would return the link to the list, !save help would explain stuff, etc. Also has !save unsave to delete last song. No proper edit yet, might fix it later too.

  • The UI is tiny bit more advanced now. Still pretty simple. Manuals and stuff clarified a bit too.

  • Finally regged on the github and uploaded this there. Yay… sort of. https://github.com/Enbis-codes/savebot

There are still a few things I plan to implement there, but right now it should be at least somewhat useable.

1 Like

Sorry if that is a too stupid question. But it always shows me “you already had this song in your list! Type !save mylist to see it.” after it saved one song. Edit: Now i see, that it saved the song from Monstercat. But also only the first one, not the followed ones. Is there a way to save the songs from LastFM scrobbling?)

Are there some general things i’ve might have missed?

I’m using the Last FM scrobbler with Spotify if that’s any relevant.

1 Like

Hmm, I never tested it with Spotify as it doesn’t work in my country, so can’t say for sure if that’s the cause. But generally “you already have this song” error comes if the queue has not moved. Also are you sure it’s even considered a separate tracks and not just one “track” that streams songs from LastFM? Do you see different song names in the queue list when you try to save them?

1 Like

When I type the !song command it shows me different songs. The ones that are played at that time. I need to implement the $(lastfm username) somewhere to grab the songs i guess. But I don’t no where. Sry, I’m a total noob at such things.

Funny thing. If i change the user in that command ($(urlfetch https://jy.feq.ru/bot/save.php?u=$(user)&c=$(channel)&q=$(querystring))) to $(lastfm username) it showes me the songname as my username, so it grabes it. (@BlackWaterbyApparat, song’s saved to your list! Type !save mylist to see it.) I just need to put it somewhere else in the command line.

Again, sry. I hope that’s at least a bit understandable.

1 Like