You are not logged in.
I would very much like to share a BASH script I use daily,
which finds and plays my music very fast.
My Music library is well organized (folder and files well named),
and in my case, cmus has a too long keyboard workflow.
with m I can type
m radioh cr
and it will find and play instantly Radiohead - Creep
So here it is, please critic
Originally posted on gentoo forums, but I just registered for an other matter,
and arch forums are admittedly more lively for this kind of posts.
edit: moved from github to gitlab
Last edited by arnaudv6 (2018-06-08 07:50:17)
Offline
Make an Aur package, I'm too lazy
https://ugjka.net
paru > yay | webcord > discord
pacman -S spotify-launcher
mount /dev/disk/by-...
Offline
Comments by line numbers:
30: Why do you say "no tilde please", it'd work just fine with a tilde, just get rid of the quotes, and/or just use $HOME
33: What is sentaku, I cannot find it anywhere in the repos or AUR
35: Why do you need a windowing toolkit?
36: Why do you need youtube-dl to play local music? Have this test only under the conditional blocks that actually need youtube-dl
38: This line does nothing useful
39: Don't do this: like line 30 comment, just do it right in the first place. Do not try to reinvent the wheel.
58: You concatenate all search parameters into a single word. Your example of "m radioh cr" would make searchterm radiohcr. Is this what you want?
62-65: Why not just have this in the case statement on line 55 to save a variable and a conditional test:
*) echo "${help}"; exit ;;
86-91: Equivalent:
find "${music_dir}" -printf "%P\n" | sort > "${index_file}"
~103: I don't get the logic. You create a list of all files with find, then you grep that list for your match. Just use find - again, don't try to reinvent the wheel.
The following seems to get the same results - just doesn't have the cli parameter checking and I didn't implement the youtube_dl stuff as I don't use it.
#!/bin/bash
search=('find ~/music -type f')
while [[ $# -gt 1 ]]; do
search+=("-ipath \"*$1*\"")
shift
done
search+=("-iname \"*$1*\"")
files=( $(eval ${search[@]}) )
for f in ${files[@]}; do mpv $f; done
[[ ${#files} -eq 0 ]] && echo "do youtube_dl stuff here"
(edit: oops, I forgot to replace my test "echo" with mpv. Fixed)
EDIT 2: This was all being nitpicky. It all looks good as it is - I hope my nitpicking is only seen as the constructive criticism it is intended to be in order to highlight possibilities in shell scripting.
"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" - Richard Stallman
Offline
Make an Aur package, I'm too lazy
That sort of attitude is uncalled for and not welcome. Laziness does not justify rudeness.
Sakura:-
Mobo: MSI MAG X570S TORPEDO MAX // Processor: AMD Ryzen 9 5950X @4.9GHz // GFX: AMD Radeon RX 5700 XT // RAM: 32GB (4x 8GB) Corsair DDR4 (@ 3000MHz) // Storage: 1x 3TB HDD, 6x 1TB SSD, 2x 120GB SSD, 1x 275GB M2 SSD
Making lemonade from lemons since 2015.
Offline
Umm sorry if that sounded rude, I meant that I would love to try the script if there was a package for it.
https://ugjka.net
paru > yay | webcord > discord
pacman -S spotify-launcher
mount /dev/disk/by-...
Offline
Umm sorry if that sounded rude, I meant that I would love to try the script if there was a package for it.
You don't need a package for a simple bash script. If you want to test it, you can even store it in /tmp and just run it.
35: Why do you need a windowing toolkit?
whiptail is similar to dialog.
It seems there is an option to ask for a youtube search & download if there is no local result. I think I'd have used a commandline switch for that, but everyone has their own preferences.
58: You concatenate all search parameters into a single word. Your example of "m radioh cr" would make searchterm radiohcr. Is this what you want?
Well, the parameters are separated with a space, and then later the script loops over the space separated words. An array may be a better choice.
Last edited by progandy (2016-02-25 14:35:39)
| alias CUTF='LANG=en_XX.UTF-8@POSIX ' |
Offline
33: What is sentaku, I cannot find it anywhere in the repos or AUR
In his defense, he did at least provide a URL to sentaku github in the comments at the top.
@arnaudv6
Maybe change your error message to
Unable to find <binary> : <url of project>
Or such, at least for stuff that's not in AUR. Also, if any such message would exceed 80 characters, consider splitting to two lines.
But whether the Constitution really be one thing, or another, this much is certain - that it has either authorized such a government as we have had, or has been powerless to prevent it. In either case, it is unfit to exist.
-Lysander Spooner
Offline
ugjka wrote:Umm sorry if that sounded rude, I meant that I would love to try the script if there was a package for it.
You don't need a package for a simple bash script. If you want to test it, you can even store it in /tmp and just run it.
It has 4 dependencies (one of which is not even in the AUR), that's not simple anymore.
https://ugjka.net
paru > yay | webcord > discord
pacman -S spotify-launcher
mount /dev/disk/by-...
Offline
In his defense, he did at least provide a URL to sentaku github in the comments at the top.
Ah, sorry - I missed that. But as above, it does seem like a lot of not-particularly-common dependencies for a simple task.
"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" - Richard Stallman
Offline
Woohoo ! Impressive feedback.
Thank you all much for your concern.
It has 4 dependencies (one of which is not even in the AUR), that's not simple anymore.
I completely get your point. I can not easily provide a AUR package for I use gentoo,
but problem is the same with gentoo : no package.
I will edit my first post with a link to sentaku-0.3 you can place in ./local/bin (if it is in your $PATH)
30: Why do you say "no tilde please", it'd work just fine with a tilde, just get rid of the quotes, and/or just use $HOME
38: This line does nothing useful
39: Don't do this: like line 30 comment, just do it right in the first place. Do not try to reinvent the wheel.
My bash knowledge is bad here : so there would be a simple way where user can enter ~/Music with spaces folder and it all works ?
36: Why do you need youtube-dl to play local music? Have this test only under the conditional blocks that actually need youtube-dl
62-65: Why not just have this in the case statement on line 55 to save a variable and a conditional test:
*) echo "${help}"; exit ;;
Right : thanks ! Corrected...
86-91: Equivalent:
find "${music_dir}" -printf "%P\n" | sort > "${index_file}"
I don't think so : with my way, find prints out relative paths, which cuts out 40% of index-file size in my case.
~103: I don't get the logic. You create a list of all files with find, then you grep that list for your match..............
Well that to me is the whole point of having an index: blazing fast first search, lessens disk accesses.
(I reboot my machine each day at least, but maybe I only once a month add a music to my collection. Plus I love fast )
Thank you much!
edit: had->add
Last edited by arnaudv6 (2017-05-25 11:51:01)
Offline
I think I'd have used a commandline switch for that, but everyone has their own preferences.
There is a -y switch which bypasses local search.
But when searching for a local file and finding none, the script proposes searching youtube
Well, the parameters are separated with a space, and then later the script loops over the space separated words. An array may be a better choice.
Genuinly candide here : would it be better ?
Offline
Trilby wrote:33: What is sentaku, I cannot find it anywhere in the repos or AUR
In his defense, he did at least provide a URL to sentaku github in the comments at the top.
@arnaudv6
Maybe change your error message toUnable to find <binary> : <url of project>
Or such, at least for stuff that's not in AUR. Also, if any such message would exceed 80 characters, consider splitting to two lines.
Right thanks ! Corrected
Offline
Trilby wrote:86-91: Equivalent:
find "${music_dir}" -printf "%P\n" | sort > "${index_file}"I don't think so : with my way, find prints out relative paths, which cuts out 40% of index-file size in my case.
Did you try my version? The %P in the printf (not %p) prints relative paths.
"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" - Richard Stallman
Offline
Ok now youtube-dl is optional and checked at runtime (thanks @Trilby)
I am considering dropping whiptail for a simpler solution
I just wish to now @Trilby @ugjka @progandy: whiptail is not installed on your systems ? Not so common then ?
When it comes to sentaku, I just don't get why this script is not more used !? I can think of a few alternatives:
https://github.com/facebook/PathPicker # select python facebook
https://github.com/geier/choose # select python
https://github.com/rcmdnk/sentaku # select bash
https://github.com/garybernhardt/selecta # select ruby
https://github.com/thoughtbot/pick # fuzzy select C
https://github.com/hugows/hf/ # fuzzy find Go
https://github.com/mptre/yank # select>xsel C
But sentaku being one-file bash script made it a go for m
Would this help if I dynamically launched whichever of mpv, mplayer or gst-play is installed ?
or have player be a config option ?
edit : "help to" -> "help if I", lauch -> launched
Last edited by arnaudv6 (2017-05-25 11:52:03)
Offline
Did you try my version? The %P in the printf (not %p) prints relative paths.
You're right I did not
And you're right again : your way is shorter and more elegant : corrected, thanks !
Offline
I would suggest adding '-mindepth 1' to to Trilby's find command to avoid a blank line. Alternately, '-type f' will avoid printing directories - which I think you'd want to do - and consequentially 'bypass' the blank line problem. Remember, a particularly great thing about shell scripting is that you can test stuff from the interactive prompt:
$ music_dir=~'/Music MP3'
$ find "${music_dir}" -mindepth 1 -printf "%P\n" | sort
...
$ find "${music_dir}" -type f -printf "%P\n" | sort
...
But whether the Constitution really be one thing, or another, this much is certain - that it has either authorized such a government as we have had, or has been powerless to prevent it. In either case, it is unfit to exist.
-Lysander Spooner
Offline
Thanks for the support, guys.
I removed whiptail dependency.
@alphaniner : I added the -mindepth 1 switch, thanks.
as a matter of fact, I like to have directories listed, so I can play one CD/artist at a time.
Offline
m now gets sentaku from github and puts it in its directory if not found.
Only mpv is required so.
I will soon advertize its md5sum. Thanks guys for all your insights and comments.
Offline
It works, nice
https://ugjka.net
paru > yay | webcord > discord
pacman -S spotify-launcher
mount /dev/disk/by-...
Offline
Moving to "Community Contributions".
Edit: @arnaudv6, please give a better title to the thread. Perhaps a name for your script.
Last edited by x33a (2016-02-26 13:39:10)
Offline
It works, nice
Thanks!
Offline
Moving to "Community Contributions".
Edit: @arnaudv6, please give a better title to the thread. Perhaps a name for your script.
Flattered, x33a.
I kind of like m's name: obviously short and just one letter to type.
I guess potentially harmful programs (like rm) need names with at least 2 distant letters on the keyboard to limit mistakes though.
Changing the title of the thread.
Offline
30: Why do you say "no tilde please", it'd work just fine with a tilde, just get rid of the quotes, and/or just use $HOME
38: This line does nothing useful
39: Don't do this: like line 30 comment, just do it right in the first place. Do not try to reinvent the wheel.
I did some cleaning, and finally applied your advises, Trilby:
I don't know why I delayed so much, anyway thank you much for ginving it in the first place.
Also I would like to sort directories first in my files index, but keep the exact same format:
find "${music_dir}" -mindepth 1 -printf "%P\n" | sort > "${m_index_f}"
I tried such solutions with no success, does anyone have a better way please?
tree -fin --noreport -o music_index.2.txt
for ((l = 0; l <= 24; l++)); do find . -mindepth ${l} -maxdepth ${l} -type f | sort; done > list4
for ((l = 0; l <= 24; l++)); do find . -mindepth ${l} -maxdepth ${l} | sort; done > list4
Last edited by arnaudv6 (2016-06-04 08:26:22)
Offline
I downloaded the script and ran it with the -u switch for it to create a database
I just got an error message
"/home/dka/bin/m: line 99: /home/dka/music MP3/music_index.txt: No such file or directory
find: ‘/home/dka/music MP3’: No such file or directory"
Shouldn't the script just create the directory itself?
like
mkdir -p $HOME/music/MP3
one other thing I typed in a search and got
"No file matches. Get first youtube result? (Y/n)"
but before I typed anything it started downloading form youtube.
edit, ok I found the line and increased the time from 4 to 8 seconds
Forgot to say I like it! it works quickly. I think the title of the thread should be
"m: finds plays and downloads music"
Last edited by chickenPie4tea (2016-06-06 15:26:25)
You can like linux without becoming a fanatic!
Offline
I downloaded the script and ran it with the -u switch for it to create a database
I just got an error message
"/home/dka/bin/m: line 99: /home/dka/music MP3/music_index.txt: No such file or directory
find: ‘/home/dka/music MP3’: No such file or directory"
Well, I reacted too late: you must know by now, music dir was set in a variable for the user to adapt.
I changed m so it complies with XDG user directories now: does this suit you best?
When it comes to youtube downloads behaviour, here are my motives:
I often leave the console without looking at it:
m then finds and plays something with no more input from I.
Delay seems fine to me but I am ready to hear about your experience.
Thanks for trying m!
Last edited by arnaudv6 (2016-06-18 06:27:32)
Offline