From 0e741bae8f626025c64eea41d028a0e2708255ea Mon Sep 17 00:00:00 2001 From: AnthonyAxenov Date: Wed, 8 Jun 2022 08:57:53 +0800 Subject: [PATCH] New scripts to find channel in playlists --- .gitignore | 8 +++-- tools/check-pls.sh | 7 ++-- tools/download-all.sh | 6 ++-- tools/find-in-all.sh | 20 +++++++++++ tools/find-in-pls.sh | 78 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 111 insertions(+), 8 deletions(-) create mode 100755 tools/find-in-all.sh create mode 100755 tools/find-in-pls.sh diff --git a/.gitignore b/.gitignore index f87b7c4..96b9c30 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,7 @@ /.idea -/files -a.m3u8 +/.vscode +/downloaded +*.m3u +*.m3u.* +*.m3u8 +*.m3u8.* diff --git a/tools/check-pls.sh b/tools/check-pls.sh index d20c21d..a224fd5 100755 --- a/tools/check-pls.sh +++ b/tools/check-pls.sh @@ -8,10 +8,11 @@ # ./check-pls.sh local/pls.m3u # ./check-pls.sh https://example.com/pls.m3u # -# Both *.m3u and *.m3u8 are supported. +# 1st argument is playlist file name or URL. +# If it is an URL it will be saved in /tmp and +# checked as local file. # -# If argument is link to playlist it will be -# saved in /tmp and then check as local file. +# Both *.m3u and *.m3u8 are supported. # ################################################# diff --git a/tools/download-all.sh b/tools/download-all.sh index e7a3e47..5e12b63 100755 --- a/tools/download-all.sh +++ b/tools/download-all.sh @@ -8,10 +8,10 @@ # ./download-all.sh # # All playlists from playlists.ini will be -# downloaded in ./files directory +# downloaded in ./downloaded directory # ################################################# -mkdir files && \ - cd files && \ +mkdir -p downloaded && \ + cd downloaded && \ grep -P "pls='(.*)'" ../playlists.ini | sed "s/^pls=//g" | sed "s/'//g" | tr -d '\r' | xargs wget diff --git a/tools/find-in-all.sh b/tools/find-in-all.sh new file mode 100755 index 0000000..e7199a1 --- /dev/null +++ b/tools/find-in-all.sh @@ -0,0 +1,20 @@ +#!/bin/bash + +################################################# +# +# IPTV channel finder (all playlists) +# +# Usage: +# ./download-all.sh +# ./find-in-all.sh disney +# +# 1st argument is channel name pattern. +# +################################################# + +SCRIPT_DIR="$( cd -- "$( dirname -- "${BASH_SOURCE[0]:-$0}"; )" &> /dev/null && pwd 2> /dev/null; )"; +[ ! -d ./downloaded ] && echo "Error: ./downloaded directory does not exist. Run $SCRIPT_DIR/tools/download-all.sh" && exit 1 +[ ! "$(ls -A ./downloaded)" ] && echo "Error: ./downloaded directory is empty. Run $SCRIPT_DIR/tools/download-all.sh" && exit 2 +for file in ./downloaded/*; do + "$SCRIPT_DIR"/find-in-pls.sh "$1" "$file" +done diff --git a/tools/find-in-pls.sh b/tools/find-in-pls.sh new file mode 100755 index 0000000..1a0f5f0 --- /dev/null +++ b/tools/find-in-pls.sh @@ -0,0 +1,78 @@ +#!/bin/bash + +################################################# +# +# IPTV channel finder (one playlist) +# +# Usage: +# ./find-in-pls.sh disney local/pls.m3u +# ./find-in-pls.sh disney https://example.com/pls.m3u +# +# 1st argument is channel name pattern. +# +# 2nd argument is playlist file name or URL. +# If it is an URL it will be saved in /tmp and +# checked as local file. +# +# Both *.m3u and *.m3u8 are supported. +# +################################################# + +awk ' + BEGIN { + IGNORECASE=1 + channel = ARGV[1] + playlist = ARGV[2] + found_count = 0 + found_last = 0 + regex_ch = tolower(sprintf("^#EXTINF:.+,\s*(.*%s.*)", channel)) + regex_url = "^https?:\/\/.*$" + + print "--------------------" + print "\033[20m\033[97mPlaylist:\033[0m " playlist + print "\033[20m\033[97mChannel to find:\033[0m " channel + + if (playlist ~ /^http(s)?:\/\/.*/) { + parts_count = split(playlist, parts, "/") + file_name = parts[parts_count] + code = system("wget " playlist " -qO /tmp/" file_name " > /dev/null") + if (code == 0) { + print "Saved in /tmp/" file_name + } else { + print "ERROR: cannot download playlist: " playlist + exit 1 + } + playlist = "/tmp/" file_name + } + + ARGV[1] = playlist + delete ARGV[2] + print "--------------------" + } + { + sub("\r$", "", $0) # crlf -> lf + if (tolower($0) ~ tolower(regex_ch)) { + found_count++ + #print "\n\033[32m" FNR " FOUND:\033[0m\t" $0 + print "\n" $0 + found_last = FNR + } + if (found_last > 0) { + if (tolower($0) ~ tolower(regex_url)) { + #print "\t\t" $0 + print $0 "\n" + found_last = 0 + } + } + } + END { + if (found_count == 0) { + print "\033[91mNothing found\033[0m" + } else { + print "--------------------" + print "\033[20m\033[97mPlaylist:\033[0m " playlist + print "\033[20m\033[97mChannel found:\033[0m " channel + print "\033[20m\033[97mFound:\033[0m\033[32m " found_count "\033[0m" + } + } +' $1 $2