forked from rtfpessoa/bashflix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbashflix.sh
executable file
·191 lines (149 loc) · 4.54 KB
/
bashflix.sh
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/usr/bin/env bash
#
# Bashflix Script (https://github.com/astavares/bashflix)
#
request_query_raw=$1
language=${2}
player=${3:-mpv}
website=${4:-pirate_bay}
clean=${5}
default_language="en"
if [ -z "${request_query_raw}" ]; then
echo "Search query is mandatory!"
echo ""
echo "usage: $0 <search> [<language>] [<player>] [<clean>]"
echo ""
echo "notes:"
echo " <search> can be a <movie name> or <serie name>"
echo " <movie name> and <serie name> should avoid containing spaces, use dots (.) instead"
echo " <seasson> and <episode> should be zero padded, always have two digits"
echo " <language> defaults to en"
echo " <player> either mpv or vlc"
echo " <clean> remove torrent files and subtitles in the end"
exit 1
else
if [ "$1" == "-h" ]; then
echo "$(cat $HOME/.bashflix_history)"
exit 1
else
#echo "$(cat $HOME/.bashflix_history)$1" > $HOME/.bashflix_history
echo -e "$request_query_raw" >> $HOME/.bashflix_history
fi
fi
# HACK
# Global var to capture function return values
# Needed to do logs and return a value in the same function
FUN_RET=""
clean-str() {
local raw_str="$1"
raw_str="${raw_str#\ }"
raw_str="${raw_str%\ }"
raw_str="${raw_str// /.}"
FUN_RET="${raw_str}"
}
clean-str "${request_query_raw}"
request_query="${FUN_RET}"
echo "Welcome to bashflix"
retrieve-magnet() {
local search_term=$1
echo "Downloading the best magnet for ${search_term}"
local magnet_string=""
if [ "${website}" == "pirate_bay" ]
then
magnet_string=$(pirate-get -s SeedersDsc -0 -C 'echo "%s"' "${search_term}" | tail -n 1)
fi
if [ "${website}" == "rarbg" ] || [ "${magnet_string}" = "No results" ]
then
magnet_string=$(rarbgapi --search-string "${search_term}" | tail -n 1 | sed -n 's/^.*magnet:?/magnet:?/p')
fi
FUN_RET="${magnet_string}"
}
retrieve-name-from-magnet() {
local magnet_string=$1
echo "Parsing torrent name from magnet content"
local torrent_name_param=$(awk -F "&" '{print $2}' <<< "$magnet_string")
local torrent_name_dirty=$(awk -F "=" '{print $2}' <<< "$torrent_name_param")
local torrent_name_raw=$(echo "$torrent_name_dirty" | sed -e 's/%\([0-9A-F][0-9A-F]\)/\\\\\x\1/g')
local torrent_name_escaped=$(echo -e "$torrent_name_raw")
# Not sure why I need to do this twice ?!?!?
local torrent_name=$(echo -e "$torrent_name_escaped")
FUN_RET="${torrent_name}"
}
retrieve-magnet-hash() {
local magnet_string=$1
echo "Parsing torrent hash from magnet content"
local torrent_hash=$(echo "$magnet_string" | awk -F ":" '{print $4}' | awk -F "&" '{print $1}')
FUN_RET="${torrent_hash}"
}
get-subtitle-file() {
local language="$1"
local torrent_name="$2"
local tmp_dir="$3"
subliminal download -l "$language" -d "${tmp_dir}" "${torrent_name}"
}
get-subtitle() {
local torrent_name="$1"
local tmp_dir="$2"
local subtitle_filename=""
local languages=()
if [ "${language}" != "${default_language}" ]; then
languages+=("${language}")
fi
languages+=("${default_language}")
for language in ${languages[@]}; do
get-subtitle-file "${language}" "${torrent_name}" "${tmp_dir}"
subtitle_filename=$(find ${tmp_dir} -maxdepth 1 -name "*${language}*.srt" | head -1)
if [ -n "$subtitle_filename" ]; then
echo "Found subtitle for language ${language}"
break;
fi
done
FUN_RET="${subtitle_filename}"
}
# Get magnet
retrieve-magnet "${request_query}"
magnet_string="${FUN_RET}"
if [ "${magnet_string}" == "No results" ]; then
echo "Cannot find content for the query ${request_query}"
exit 1
fi
# Get torrent name from magnet
retrieve-name-from-magnet "${magnet_string}"
torrent_name="${FUN_RET}"
# Get torrent hash from magnet
retrieve-magnet-hash "${magnet_string}"
torrent_hash="${FUN_RET}"
tmp_dir="/tmp/torrent-stream/${torrent_hash}"
mkdir -p "${tmp_dir}"
echo "Using ${tmp_dir} as temporary directory"
if [ -n "${clean}" ]; then
function cleanup {
echo "Removing temporary directory ${tmp_dir}"
rm -r ${tmp_dir}
echo "See you soon!"
}
trap cleanup EXIT
fi
# Get subtitles
if [ -n "${language}" ]; then
get-subtitle "${torrent_name}" "${tmp_dir}"
subtitle="${FUN_RET}"
fi
case $player in
vlc)
player_flag="-v"
subtitle_args=(-- --sub-file="${subtitle}")
;;
*)
player_flag="-k"
subtitle_args=(-t "${subtitle}")
;;
esac
# Run with peerflix
if [ -n "${subtitle}" ]; then
echo "Playing ${torrent_name} with subtitles ${subtitle}"
peerflix "${magnet_string}" "${player_flag}" ${subtitle_args[@]}
else
echo "Playing ${torrent_name}"
peerflix "${magnet_string}" "${player_flag}"
fi