Refactor to read and write playlist to file

This commit is contained in:
Shahin Agha-Ghassem 2023-03-19 12:12:44 +01:00
parent 4cd68f1787
commit 67533b2ffb

47
iptv
View File

@ -6,17 +6,26 @@ fi
m3u=$1 m3u=$1
tmp_playlist="/tmp/iptvtemp" config_path="$HOME/.config/iptv/"
tmp_channels="/tmp/iptvchannels" channels_file="$config_path/channels"
tmp_playlist="/tmp/iptvplaylist"
printf "\nLoading channels... " mkdir -p "$config_path"
curl -s $m3u | grep EXTINF: -A 1 > $tmp_playlist
printf "Done!\n"
printf "Parsing channels... " if [ -z "$m3u" ] && [ ! -s "$channels_file" ]; then
channels=() echo "No M3U playlist has been configured. Run with: iptv http://domain/playlist.m3u"
readarray -t lines < "$tmp_playlist" exit 1
for (( i=0; i<${#lines[@]}; i+=2 )); do fi
if [ ! -z "$m3u" ]; then
printf "\nLoading channels... "
curl -s $m3u | grep EXTINF: -A 1 > $tmp_playlist
printf "Done!\n"
printf "Parsing channels... "
channels=()
readarray -t lines < "$tmp_playlist"
for (( i=0; i<${#lines[@]}; i+=2 )); do
line="${lines[i]}" line="${lines[i]}"
line2="${lines[i+1]}" line2="${lines[i+1]}"
@ -24,13 +33,23 @@ for (( i=0; i<${#lines[@]}; i+=2 )); do
name="${BASH_REMATCH[1]}" name="${BASH_REMATCH[1]}"
channels+=("$name [CH:$i] url:$line2") channels+=("$name [CH:$i] url:$line2")
fi fi
done done
printf "Done!\n" printf "Done!\n"
printf "%s\n" "${channels[@]}" > $channels_file
echo "Playlist saved. Now run iptv again without a M3U URL."
exit
fi
selected=$(cat "$channels_file" | sed 's/ [^ ]*$//' | fzf)
if [ ! -n "$selected" ]; then
exit 1
fi
printf "%s\n" "${channels[@]}" > $tmp_channels
selected=$(cat "$tmp_channels" | sed 's/ [^ ]*$//' | fzf)
selected_channel=$(echo "$selected" | sed 's/.*\(\[CH:[0-9]\+\]\).*/\1/') selected_channel=$(echo "$selected" | sed 's/.*\(\[CH:[0-9]\+\]\).*/\1/')
selected_channel_line=$(cat "$tmp_channels" | grep -F "$selected_channel") selected_channel_line=$(cat "$channels_file" | grep -F "$selected_channel")
selected_channel_url=$(echo "$selected_channel_line" | grep -oP 'url:\K.*' | tr -d '\r') selected_channel_url=$(echo "$selected_channel_line" | grep -oP 'url:\K.*' | tr -d '\r')
selected_channel_name=$(echo "$selected_channel_line" | sed 's/\(.*\) url:.*/\1/') selected_channel_name=$(echo "$selected_channel_line" | sed 's/\(.*\) url:.*/\1/')