shahin8r / iptv

A simple CLI IPTV player for M3U playlists with fuzzy finding in the terminal.
GNU General Public License v3.0
233 stars 19 forks source link

save_channels() not parsing lines in correct order #7

Closed dmacmill closed 1 year ago

dmacmill commented 1 year ago

Hello,

The m3u8 file I used, https://raw.githubusercontent.com/Free-TV/IPTV/master/playlist.m3u8, has the name of the channel come before the url, like so:

#EXTINF:-1 tvg-name="TN Todo Noticias" tvg-logo="https://upload.wikimedia.org/wikipedia/commons/thumb/4/4f/TN_todo_noticias_logo.svg/200px-TN_todo_noticias_logo.svg.png" tvg-id="TodoNoticias.ar" group-title="Argentina",TN Todo Noticias
https://live-01-01-tn.vodgc.net/TN24/index.m3u8
#EXTINF:-1 tvg-name="Encuentro Ⓨ Ⓖ" tvg-logo="https://i.imgur.com/IyP2UIx.png" tvg-id="Encuentro.ar" group-title="Argentina",Encuentro Ⓨ Ⓖ
https://www.youtube.com/user/encuentro/live
#EXTINF:-1 tvg-name="Pakapaka Ⓨ Ⓖ" tvg-logo="https://i.imgur.com/Q4zaCuM.png" tvg-id="Pakapaka.ar" group-title="Argentina",Pakapaka Ⓨ Ⓖ
https://www.youtube.com/user/CanalPakapaka/live

But the save_channels function tries to read the url first, resulting in channels not pairing with their corresponding urls. When trying to reach ABC news, I get Bloomberg for example.

Below fixes this issue for me:

save_channels() {
  m3u_url=$(cat "$m3u_url_file")

  printf "\nLoading channels... "
  curl -s "$m3u_url" | grep EXTINF: -A 2 > $tmp_playlist
  printf "Done!\n"

  printf "Parsing channels... "
  channels=()
  url=""

  while IFS= read -r line; do
  if [[ "$line" =~ tvg-name=\"([^\"]+)\" || "$line" =~ tvg-id=\"([^\"]+)\" ]]; then
    name="${BASH_REMATCH[1]}"
    url=""
  elif [[ "$line" == http* ]]; then
    url="$line"
    channels+=("$name [CH:${#channels[@]}] url:$url")
  fi
  done < "$tmp_playlist"

  printf "Done!\n"

  printf "%s\n" "${channels[@]}" > $channels_file
}
shahin8r commented 1 year ago

Good catch and thanks for providing the fix! @dmacmill 🙏🏽

shahin8r commented 1 year ago

Please let me know if it works as expected now.

dmacmill commented 1 year ago

Please let me know if it works as expected now.

It does, thank you.