Who could streamline this script better than what it is?, regarding mostly those tmp's

1

#!/bin/sh -e
#
# Usage:
#   ytpl song title
#   ytpl < titles
#

go() {
    echo "Playing $1"

    # Force TTY input for controls even if titles are read from input
    echo https://youtu.be/ > tmp1.txt
    youtube-dl --get-id "ytsearch:$1" > tmp2.txt
    youtube-dl --get-title "ytsearch:$1" > tmp4.txt
    paste -d" " tmp1.txt tmp2.txt > tmp3.txt
    paste -d" " tmp3.txt tmp4.txt > tmp5.txt
    sed 's/ //' tmp5.txt > tmp6.txt
    cat tmp6.txt
    mpv --vid=no "$(youtube-dl -g "ytsearch:$1" | tail -1)" < /dev/tty
    rm tmp*.txt
}

# All arguments as a single space separated string
if [ -n "$*" ]; then
    go "$*"
    exit
fi

# Read one title per line
while read title; do
    go "$title"
done

user3287029

Posted 2016-02-25T23:33:46.017

Reputation: 13

You are supposed to accept the answer (by clicking on the V-shaped symbol next to the answer text) if it solved your problem. – MariusMatutiae – 2016-02-26T07:15:27.577

Oops, I did not see this message yesterday sorry :( – user3287029 – 2016-02-27T16:54:05.397

Answers

1

echo https://youtu.be/ > tmp1.txt
youtube-dl --get-id "ytsearch:$1" >> tmp1.txt
youtube-dl --get-title "ytsearch:$1" >> tmp1.txt
cat tmp1.txt |tr '\n' ' '|sed 's/ //' > tmp2.txt
cat tmp2.txt
mpv --vid=no "$(youtube-dl -g "ytsearch:$1" | tail -1)" < /dev/tty
rm tmp*.txt

Code sample 2 Follow these many instructions: http://www.unixmen.com/access-twitter-via-command-line-terminal/

a=$(youtube-dl --get-id "ytsearch:$1")
b=$(youtube-dl --get-title "ytsearch:$1")
c="https://youtu.be/$a $b"
echo $c
t update "$c"
mpv --vid=no "$(youtube-dl -g "ytsearch:$1" | tail -1)" < /dev/tty

cybernard

Posted 2016-02-25T23:33:46.017

Reputation: 11 200

I need those 3 lines straight into 1 so I could use it to paste into a list and/or share it to Facebook and Twitter, opening the browser from the terminal itself :) THX – user3287029 – 2016-02-26T00:47:01.173

@user3287029 There 3 lines into 1 – cybernard – 2016-02-26T01:23:36.717

That! Sir... did it :) THX – user3287029 – 2016-02-26T01:43:38.907

@user3287029 FYI: --get-id and --get-title can be combined in 1 line, but they will be in the reverse order. youtube-dl --get-id --get-title "ytsearch:$1". – cybernard – 2016-02-26T01:52:22.447

I know that's why I had to separate them, sorry one more request, the first blankspace needs to be deleted and join the "youtu.be/" with the --get-id result. :( – user3287029 – 2016-02-26T02:05:43.743

@user3287029 updated answer – cybernard – 2016-02-26T02:29:21.313

How can I use cURL to send the youtube link to twitter with the Twitter Api (I'm guessing) all in one line preferably or calling another bash. I know it is too much to ask. THX – user3287029 – 2016-02-26T16:46:16.960

See if code sample #2 works – cybernard – 2016-02-26T23:16:58.160

You are incredible :) is almost working. But they said Twitter does not allow it this way anymore, perhaps I can use Twurl for that. – user3287029 – 2016-02-26T23:36:49.140

@user3287029 updated again. Follow the instructions from unixmen.com, and my then my script change. – cybernard – 2016-02-26T23:45:24.860

No I don't need another idea. I optimized your script, and gave you 2 modifications, so I would rather have my answer marked as the correct answer. You want something else you will have to post a new question, for someone else to answer. – cybernard – 2016-02-27T14:58:56.350