Files
iptvc/cmd/check.go

101 lines
3.0 KiB
Go

/*
* Copyright (c) 2025, Антон Аксенов
* This file is part of iptvc project
* MIT License: https://git.axenov.dev/IPTV/iptvc/src/branch/master/LICENSE
*/
package cmd
import (
"axenov/iptv-checker/app"
"axenov/iptv-checker/app/checker"
"axenov/iptv-checker/app/playlist"
"encoding/json"
"fmt"
"log"
"time"
"github.com/spf13/cobra"
)
// checkCmd represents the file command
var checkCmd = &cobra.Command{
Use: "check",
Short: "Check playlists",
Run: func(cmd *cobra.Command, args []string) {
app.Init()
files, _ := cmd.Flags().GetStringSlice("file")
urls, _ := cmd.Flags().GetStringSlice("url")
codes, _ := cmd.Flags().GetStringSlice("code")
waitSeconds := app.Args.RepeatEverySec
if waitSeconds <= 0 {
waitSeconds = 5
}
currentIteration := 1
for {
if app.Args.RepeatCount != 1 {
log.Printf(
"@ New iteration current=%d count=%d\n",
currentIteration,
app.Args.RepeatCount,
)
}
var lists []playlist.Playlist
if len(files) == 0 && len(urls) == 0 && len(codes) == 0 {
lists = checker.PrepareListsToCheck(files, urls, codes)
} else {
if currentIteration == 1 {
lists = checker.PrepareListsToCheck(files, urls, codes)
}
}
if len(lists) > 0 {
startTime := time.Now()
onlineCount, offlineCount := checker.CheckPlaylists(lists)
log.Printf(
"Done! count=%d online=%d offline=%d elapsedTime=%.2fs\n",
len(lists),
onlineCount,
offlineCount,
time.Since(startTime).Seconds(),
)
if app.Args.NeedJson {
marshal, _ := json.Marshal(lists)
fmt.Println(string(marshal))
}
} else {
log.Println("There are no playlists to check")
}
if app.Args.RepeatCount != 0 {
if uint(currentIteration) == app.Args.RepeatCount {
break
}
currentIteration++
}
log.Printf("Waiting for new iteration... seconds=%d\n", app.Args.RepeatEverySec)
time.Sleep(time.Duration(app.Args.RepeatEverySec) * time.Second)
}
},
}
func init() {
checkCmd.Flags().StringVarP(&app.Args.TagsPath, "tags", "t", "./channels.json", "path to a local tagfile")
checkCmd.Flags().StringVarP(&app.Args.IniPath, "ini", "i", "./playlists.ini", "path to a local ini-file")
checkCmd.Flags().UintVarP(&app.Args.RandomCount, "random", "r", 0, "take this count of random playlists to check from ini-file")
checkCmd.Flags().UintVarP(&app.Args.RepeatCount, "repeat", "", 1, "repeat same check X times")
checkCmd.Flags().UintVarP(&app.Args.RepeatEverySec, "every", "", 5, "wait N seconds after every check")
checkCmd.Flags().BoolVarP(&app.Args.NeedJson, "json", "j", false, "print results in JSON format in the end")
checkCmd.Flags().BoolVarP(&app.Args.NeedQuiet, "quiet", "q", false, "suppress logs (does not affect on -j)")
checkCmd.Flags().StringSliceP("file", "f", []string{}, "path to a local playlist file (m3u/m3u8)")
checkCmd.Flags().StringSliceP("url", "u", []string{}, "URL to a remote playlist (http/https)")
checkCmd.Flags().StringSliceP("code", "c", []string{}, "code of playlist from ini-file")
rootCmd.AddCommand(checkCmd)
}