Update iptv-check

code explained
This commit is contained in:
peterpt 2018-03-09 00:17:51 +00:00 committed by GitHub
parent 8ee6f31ca8
commit c12e9b6de5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -17,8 +17,10 @@ then
mkdir "$path/temp" >/dev/null 2>&1 mkdir "$path/temp" >/dev/null 2>&1
fi fi
# Clean any files from previous run in temp folder
rm -rf "$path/temp/*" >/dev/null 2>&1 rm -rf "$path/temp/*" >/dev/null 2>&1
# Warning is a simple message that will display every 40 checks on streams to explain user how to quit
warn() { warn() {
if [[ $i -gt $wrn ]] if [[ $i -gt $wrn ]]
then then
@ -59,6 +61,8 @@ echo -e "$yellow" "-------------------------------------"
echo -e "$blue" "http://github.com/peterpt" echo -e "$blue" "http://github.com/peterpt"
echo -e "$yellow" "-------------------------------------" echo -e "$yellow" "-------------------------------------"
echo "" echo ""
# checkig for wget if it is installed
which wget > /dev/null 2>&1 which wget > /dev/null 2>&1
if [ "$?" -eq "1" ]; then if [ "$?" -eq "1" ]; then
echo -e "$red" "Wget Missing" echo -e "$red" "Wget Missing"
@ -68,6 +72,8 @@ exit 1
fi fi
} }
# The difference between writefile and writefile2 is that (writefile2) is to process the output from m3u files based in xml codes
# while function (writefile) is to process in output file the conventional m3u files without xml codes in it
function writefile2() { function writefile2() {
if [[ -f "$wfile" ]] if [[ -f "$wfile" ]]
then then
@ -86,11 +92,15 @@ echo "$chkf" >> "$wfile"
echo "" >> "$wfile" echo "" >> "$wfile"
fi fi
} }
function writefile() { function writefile() {
# checks if tool already created previously an m3u file
if [[ -f "$wfile" ]] if [[ -f "$wfile" ]]
then then
#searchs for that specific url in original file and get line number
gturlline=$(grep -n "\<$chkf\>"<$path/temp/1 | tr ":" "\n" | sed -n 1p) gturlline=$(grep -n "\<$chkf\>"<$path/temp/1 | tr ":" "\n" | sed -n 1p)
# This variavble will get the line number before the previous url (this is to get channel name)
defline=$((gturlline-1)) defline=$((gturlline-1))
stdata=$(sed -n "$defline p"< $path/temp/1) stdata=$(sed -n "$defline p"< $path/temp/1)
echo "$stdata" >> "$wfile" echo "$stdata" >> "$wfile"
@ -108,7 +118,10 @@ echo "" >> "$wfile"
fi fi
} }
# Function for m3u files with xml content
function xmlproc() { function xmlproc() {
# Find http links only and delete all the other xml codes in the file , this works with many tests i did , but it may need more filtering for m3u files with more xml funtions in it
grep -F "http" <$path/temp/1 | sed 's/<link>//g' | sed 's/^.*http/http/' | sed 's/&amp.*|//' | sed -e 's/\(.ts\).*\(=\)/\1\2/' | sed 's/=/ /g' | sed "s~</link>~ ~g" > $path/temp/2 grep -F "http" <$path/temp/1 | sed 's/<link>//g' | sed 's/^.*http/http/' | sed 's/&amp.*|//' | sed -e 's/\(.ts\).*\(=\)/\1\2/' | sed 's/=/ /g' | sed "s~</link>~ ~g" > $path/temp/2
srvnmb=$(wc -l "$path/temp/2" | awk '{print$1}') srvnmb=$(wc -l "$path/temp/2" | awk '{print$1}')
rm -rf "$path/temp/stream" >/dev/null 2>&1 rm -rf "$path/temp/stream" >/dev/null 2>&1
@ -171,52 +184,77 @@ echo -e "$orange" "$wfile"
exit 1 exit 1
fi fi
} }
# Function that will download for specific time the test stream
function teststream() { function teststream() {
wrn="0" wrn="0"
# Checks if tool already created a previous m3u file
if [[ -f "$wfile" ]] if [[ -f "$wfile" ]]
then then
exts="1" exts="1"
else else
exts="0" exts="0"
fi fi
# checks if m3u file have xml content
ckf=$(grep "<item>" <$path/temp/1) ckf=$(grep "<item>" <$path/temp/1)
if [[ ! -z "$ckf" ]] if [[ ! -z "$ckf" ]]
then then
xmlproc xmlproc
fi fi
#checks for the http links in m3u file
glnk=$(grep -F "http" < "$path/temp/1" | sed '/EXTINF/d' | sed '/EXTM3U/d' | awk '!a[$0]++' | sed '/^$/d') glnk=$(grep -F "http" < "$path/temp/1" | sed '/EXTINF/d' | sed '/EXTM3U/d' | awk '!a[$0]++' | sed '/^$/d')
#Write all the http links only to a new file so they can be checked ahead
echo "$glnk" | tr " " "\n" > "$path/temp/2" echo "$glnk" | tr " " "\n" > "$path/temp/2"
# Counts how many links must be checked
srvnmb=$(wc -l "$path/temp/2" | awk '{print$1}') srvnmb=$(wc -l "$path/temp/2" | awk '{print$1}')
# removes any previous temp pid files and stream captures from previous run
rm -rf "$path/temp/stream" >/dev/null 2>&1 rm -rf "$path/temp/stream" >/dev/null 2>&1
rm -rf "$path/temp/pid.tmp" >/dev/null 2>&1 rm -rf "$path/temp/pid.tmp" >/dev/null 2>&1
echo "" echo ""
echo -e "$red""Press CTRL+C To Stop or Abort IPTV list check" echo -e "$red""Press CTRL+C To Stop or Abort IPTV list check"
echo "" echo ""
# Starts the stream checks
for i in $(seq "$srvnmb") for i in $(seq "$srvnmb")
do do
chkf=$(sed -n "${i}p" < "$path/temp/2") chkf=$(sed -n "${i}p" < "$path/temp/2")
# To avoid errors in previous filter , it checks if the link starts with http , rtmp or HTTP
chkurl=$(echo "$chkf" | head -c 4) chkurl=$(echo "$chkf" | head -c 4)
case "$chkurl" in case "$chkurl" in
http|rtmp|HTTP) http|rtmp|HTTP)
# start the stream download with wget , creates a file with the pid from wget
wget -q "$chkf" -O "$path/temp/stream" & echo $! > "$path/temp/pid.tmp" wget -q "$chkf" -O "$path/temp/stream" & echo $! > "$path/temp/pid.tmp"
# reads current wget pid
pid=$(sed -n 1p < "$path/temp/pid.tmp") pid=$(sed -n 1p < "$path/temp/pid.tmp")
# 4 seconds is the time that wget will download the stream befores gets killed
sleep 4 sleep 4
# checks if wget pid is still active
rpid=$(ps -p "$pid" -o pid= | awk '{print$1}') rpid=$(ps -p "$pid" -o pid= | awk '{print$1}')
if [[ "$rpid" == "$pid" ]] if [[ "$rpid" == "$pid" ]]
then then
# kills wget pid
kill $pid kill $pid
fi fi
# checks if downlaoded stream file it is in temp directory
if [[ ! -f "$path/temp/stream" ]] if [[ ! -f "$path/temp/stream" ]]
then then
echo -e "$yellow" "Error reading captured file" echo -e "$yellow" "Error reading captured file"
else else
# checks the size of the stream file
stsz=$(wc -c "$path/temp/stream" | awk '{print$1}') stsz=$(wc -c "$path/temp/stream" | awk '{print$1}')
# In case stream file is less than 100 bytes then it is not valid
if [[ "$stsz" -le "100" ]] if [[ "$stsz" -le "100" ]]
then then
echo -e "$green" "Link:$yellow $i$green of :$yellow$srvnmb$green is$red OFF" echo -e "$green" "Link:$yellow $i$green of :$yellow$srvnmb$green is$red OFF"
else else
echo -e "$green" "Link:$yellow $i$green of :$yellow$srvnmb$green is$green ON" echo -e "$green" "Link:$yellow $i$green of :$yellow$srvnmb$green is$green ON"
#file have more than 100 bytes , then it is a valid stream , goto write file fuction
writefile writefile
fi fi
fi fi
@ -249,8 +287,9 @@ exit 1
fi fi
} }
# Case user m3u file is remote (http) then run this function
function remotef() { function remotef() {
# wil download the remote m3u file to temp folder and will check its size
wget "{$file}" -O "$path/temp/1" >/dev/null 2>&1 wget "{$file}" -O "$path/temp/1" >/dev/null 2>&1
flsz=$(wc -c "$path/temp/1" | awk '{print$1}') flsz=$(wc -c "$path/temp/1" | awk '{print$1}')
if [[ "$flsz" -le "10" ]] if [[ "$flsz" -le "10" ]]
@ -262,6 +301,8 @@ exit 0
fi fi
teststream teststream
} }
# Local m3u file is loaded here
function localf(){ function localf(){
if [[ ! -f "$file" ]] if [[ ! -f "$file" ]]
then then
@ -297,15 +338,18 @@ echo -e "$yellow" "-------------------------------------"
echo "" echo ""
exit 1 exit 1
fi fi
# If a null file name is not found then executes the script again deflecting wget errors to dev/null
if [[ ! -f $path/null ]] if [[ ! -f $path/null ]]
then then
echo "0" > $path/null echo "0" > $path/null
exec $sname $1 2>/dev/null exec $sname $1 2>/dev/null
exit 1 exit 1
fi fi
# here it means that script already was loaded and restarted , so delete the null file for the next start
rm -rf $path/null >/dev/null 2>&1 rm -rf $path/null >/dev/null 2>&1
logo logo
file="$1" file="$1"
#check if user input is a remote or local file by searching for http word in the user input variable
echo "$file" | grep "http" >/dev/null 2>&1 echo "$file" | grep "http" >/dev/null 2>&1
if [ "$?" -eq "0" ]; then if [ "$?" -eq "0" ]; then
remotef remotef