Files
iptvc/cmd/check.go
Anthony Axenov 6ea3683350
Some checks failed
Release / release (push) Has been cancelled
Аргументы --repeat и --every
2025-11-22 18:32:27 +08:00

92 lines
2.8 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"
"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")
lists := checker.PrepareListsToCheck(files, urls, codes)
waitSeconds := app.Args.RepeatEverySec
if waitSeconds <= 0 {
waitSeconds = 5
}
currentIteration := 1
iterationCount := app.Args.RepeatCount
if iterationCount <= 0 {
iterationCount = 1
}
for {
if app.Args.RepeatCount > 1 {
log.Printf(
"@ New iteration current=%d count=%d\n",
currentIteration,
app.Args.RepeatCount,
)
}
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))
}
if app.Args.RepeatCount <= 1 || 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)
}