hnarayanan / shpotify

A command-line interface to Spotify.
https://harishnarayanan.org/projects/
2.02k stars 153 forks source link

Added list playlist option #131

Closed davidem closed 4 years ago

davidem commented 4 years ago

Also see my latest comment in issue #37 :)

Hi Guys, coming a bit late to the shpotify party, but nevertheless thankful for the script. I’ve added to my fork the possibility to list the contents of a playlist based on the uri like this: spotify list uri .

is of course an uri of a playlist. is the output format and can be csv, tsv, text or html.

I deliberately used list to list the contents instead of using the already play or status, as neither of them sounded correct. I’ve made a global function of getAccessToken and to get the list I’ve used an API call to Spotify. The output (json) is parsed with a tool call jq. I’ve found no other way to parse it properly.

Let me know what you think :)

willhaslett commented 4 years ago

My two cents is that external dependencies should be a rule out for a little Bash script like this. There are a couple of good alternatives mentioned here, using Python (external but omnipresent), or grep with a serviceable regex.

hnarayanan commented 4 years ago

I was about to say this. I would be very hesitant to merge involving something like jq.

davidem commented 4 years ago

Too bad, jqis simply the easiest tool to parse json on linux/macos.

@willhaslett: I've checked the page you mentioned and before turning to 'jq' yesterday I've made a fair share of attempts to parse JSON in bash, but it isn't as straightforward as you say. For example, here's a list of possibilities I've found on your suggested page from top to bottom:

In other words: there's no simple way to parse nested-json output like the ones Spotify poops out with pre-installed tools in bash.

Yes, python is possible, but which version will you use? macOS 10.14 and earlier had python2.7 installed by default, only 10.15 comes with v3. And (thanks to a lot of bugs in Catalina) 10.14 is still widely used. Besides, the used modules (json or mjson) are not installed by default.

And yes, awk/sed/whatever can be used but that takes time to figure out and results in ugly incomprehensible code, imho.

All options, except for the shoot in your foot-methods with awk/sed/grep, require yet another tool to be installed. One exception: ticktick.sh could be incorporated into shpotify, but that's a hassle as well ;)

So to get reliable results and neat code, 'something' has to be installed anyway, in my humble option, so why not use jq? Although I understand the hesitation to introduce dependencies and maybe I'm totally wrong about this. :)

Cheers,

David

davidem commented 4 years ago

I could write wrote a check to see if jq exists. If not, the possibilities from help are hidden and the list option is disabled.

davidem commented 4 years ago

I've added a list mine option to list my playlists. It poops out uri, title and public status.

To accomplish this I've added a method to authorize to spotify, based on a gist by Hugh Rawlinson. The first time it opens a browser window, asking the user to authorise the use of the app. Subsequent requests lean on a 'refresh token'. With this authorisation it is possible to get personal Spotify information, like in this situation playlist information.

The browser windows doesn't get closed properly, which I need to look further into. But the rest works nice. It is now possible to list your playlists and pass that uri back to spotify to start that playlist. It's no longer needed to go back to the desktop app :)

It also uses jq, and is hidden if it is not installed.

(apparently it is not possible to create a separate pull request, unless I create separate branches (which I didn't), is it? )

davidem commented 4 years ago

The refresh-token is located in /var/tmp/shotify-refresh-token, but come to think of it would be better to place it in a personal config dir, wouldn’t it?