Advertisement
Dorik1972

iLook_parser

Jun 22nd, 2021 (edited)
14,432
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.91 KB | None | 0 0
  1. # SPDX-License-Identifier: GPL-2.0
  2. # Copyright (C) 2020-present Dorik1972 aka Pepsik =)
  3.  
  4. bytesToHumanReadable() {
  5.     local i=${1:-0} d="" s=0 S=("B" "KiB" "MiB" "GiB" "TiB" "PiB" "EiB" "YiB" "ZiB")
  6.     local YELLOW='\033[0;33m'
  7.     local NC='\033[0m' # No Color
  8.  
  9.     printf -v i '%d' "$i" 2>/dev/null
  10.     while ((i > 1024 && s < ${#S[@]}-1)); do
  11.         printf -v d ".%02d" $((i % 1024 * 100 / 1024))
  12.         i=$((i / 1024))
  13.         s=$((s + 1))
  14.     done
  15.     echo " ${YELLOW}$i$d${NC} ${S[$s]}"
  16. }
  17.  
  18. function process_pl {
  19.    local URL="$1"
  20.    local PLAYLIST="$2"
  21.    local YELLOW='\033[0;33m'
  22.    local LRED='\033[0;31m'
  23.    local GREEN='\033[0;32m'
  24.    local LGRAY='\033[0;37m'
  25.    local NC='\033[0m' # No Color
  26.    [ "$(whois $(curl -s ifconfig.me) | awk 'tolower($1) ~ /^country:/ { print $2 }')" == "RU" ] && { echo -e >&2 "${LRED}РУССКИЙ - ИДИ НАХУЙ!${NC}"; exit 1; }
  27.    echo -e "${GREEN}Get and parse the playlist ...${NC}"
  28.    response=$(curl --compressed --location --connect-timeout 10 --max-time 30 --fail --write-out '%{response_code} %{speed_download} %{size_download}' -H 'Cache-Control: no-cache' -R -s "$URL")
  29.    [ $? -eq 1 ] && { echo -e >&2 "${LRED}Unexpected error in curl utility.\nCheck curl version it should be >= 7.64 and support --write-out and --compressed options${NC}"; exit 1; }
  30.    read -r response_code speed_download size_download < <(tail -n1 <<<"$response")  
  31.    if [[ $response_code == 200 ]]; then
  32.        echo -e "Download size : $(bytesToHumanReadable ${size_download})\nDownload speed: $(bytesToHumanReadable ${speed_download})ps"
  33.        Count=$(<<<"$response" wc -l | xargs)
  34.        ((--Count))
  35.        echo "$response" | head -n $Count |
  36.            awk '!a[$0]++' | awk 'substr($1,1,8)!=p{print;p=substr($1,1,8)}' |
  37.                awk '{gsub("x-tvg-url","url-tvg");gsub("localhost",h);gsub("00000000000000",k);gsub("epg.xml.gz","epg2.xml.gz");gsub("/img/","/img2/");print}' h="${HOST}" k="${KEY}" > ${PLAYLIST}.m3u
  38.    else
  39.        echo -e "${LRED}HTTP response code: ${YELLOW}${response_code}${NC}"
  40.        echo -e "${LRED}Failed to download playlist template. Perhaps the resource is not available or there was an error in the template url-link${NC}"
  41.        exit 1
  42.    fi
  43.  
  44.    echo -e "${GREEN}Processing the categories:${NC}"
  45.    for CAT in "${!categories[@]}"; do
  46.        [ ${categories[$CAT]} == 0 ] && sed -i'.bak' "/$CAT/,/^/d" ${PLAYLIST}.m3u || echo -e "\t${LGRAY}$CAT${NC}"  # .bak is used for MacOS & BSD compatibility
  47.    done
  48.    find $ADDON -name "*.bak" -type f -delete
  49.    echo -e "Done\n${GREEN}Sort the playlist alphabetically${NC}"
  50.    tail -n +2 ${PLAYLIST}.m3u | sed "N;s/\n/,\t/g" | sort -fV -k 2 -t , | sed "s/,\t/\n/g" | sed -e $'1i\\\n'"$(head -n 1 ${PLAYLIST}.m3u)" > ${PLAYLIST}_sorted.m3u
  51.    echo -e "Done\n${GREEN}Playlists created:${NC}\n\t${PLAYLIST}.m3u\n\t${PLAYLIST}_sorted.m3u"
  52.    echo -e "${YELLOW}Processed ${Count} lines in ${SECONDS} seconds.${NC}"
  53.  
  54. }
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement