forked from BelkaDev/Mustream
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplay
More file actions
82 lines (69 loc) · 2.58 KB
/
Copy pathplay
File metadata and controls
82 lines (69 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/bin/bash
# https://github.com/BelkaDev/mustream
# Requires spotifyd
check::network() {
[[ $? != 0 ]] && echo "Network error: couldn't reach server." && exit 1
}
check::running() {
[[ -z $(pidof -s spotifyd) ]] && echo "Spotifyd is not running." && exit 1
}
_http_request() {
user_agent="User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36"
header=$(echo "User-Agent: $user_agent" \
echo "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8" \
echo "Accept-Language: en-US,en;q=0.5" \
echo "Upgrade-Insecure-Requests: 1" \
echo "Connection: keep-alive")
curl -s "$1" -H "$header" --compressed
}
_fetch_metadata() {
parameter="$1"
echo "$result" | grep -Eo "meta property=\"og:$parameter\".*?" | grep -oh -P '(?<=content=\").*' | awk -F "\" />" '{print $1}'
}
_format_output() {
artist=" $(echo "$1" | awk -F "·" '{print $1}')·"
[[ $type == "playlist" ]] && artist="" # hides the playlist author name
item="$2"
echo "$artist $item."
echo "Details:$(echo "$1" | awk -F "·" '{$1="";print $0 }')" | sed -e 's/\s\+/ · /2g'
}
check::running
type=track # default
engine=https://www.google.com
###### Parsing arguments ######
if [[ ${1^^} =~ ^(-A|--ALBUM|ALBUM)$ ]]; then
type=album && shift
elif [[ ${1^^} =~ ^(-P|--PLAYLIST|PLAYLIST|SOME)$ ]]; then
type=playlist && shift
fi
if [[ $* == *"open.spotify.com/playlist/"* ]]; then
type=playlist
key=$(echo $* | sed 's:.*/::' | cut -c 1-22)
else
###### Preparing request ######
qurl=$engine"/search?hl=en&q=$(echo $* | sed 's/ /+/g')"+"site:open.spotify.com/"$type
###### Fetching key ######
dom=`_http_request $qurl`
key=$(echo "$dom" | \
grep -o '<a href=['"'"'"][^"'"'"']*['"'"'"]' | \
sed -e 's/^<a href=["'"'"']//' -e 's/["'"'"']$//' | \
grep -i 'open.spotify.com/'$type/ | \
grep -i 'https' -m 1 | \
sed 's:.*/::' | \
cut -c 1-22)
check::network
fi
###### Generating link ######
link=https://open.spotify.com/$type/$key
result=`_http_request $link`
check::network
[[ $(echo "$result" | grep -m 1 -o "error") = "error" ||
$(echo "$result" | grep -m 1 -o "Moved Permanently") = "Moved Permanently" ]] &&
echo "Nothing found." && exit 1
###### Sending D-Bus Event ######
dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotifyd /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.OpenUri string:spotify:$type:$key &>/dev/null
###### Output ######
echo "Playing: $(_format_output "$(_fetch_metadata "description")" "$(_fetch_metadata "title")")"
[[ $type = "playlist" ]] && echo "Url: $link"
###### Exiting ######
exit 0