Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
57eb194efa
|
|||
|
4cbdd41b7c
|
|||
|
79891d178f
|
|||
|
89601096ba
|
|||
|
68329697ac
|
|||
|
c2ff027223
|
|||
|
13723a2dc5
|
|||
|
2412b570be
|
|||
|
303ccdd02b
|
|||
|
b689f3e799
|
|||
|
c1a7f7e289
|
|||
|
994df87846
|
15
.env.example
15
.env.example
@@ -1,10 +1,9 @@
|
||||
APP_DEBUG=false
|
||||
|
||||
HTTP_HOST=0.0.0.0
|
||||
HTTP_PORT=8031
|
||||
|
||||
REDIS_HOST=
|
||||
REDIS_PORT=
|
||||
REDIS_USERNAME=
|
||||
REDIS_PASSWORD=
|
||||
REDIS_DB=
|
||||
CACHE_ENABLED=false
|
||||
CACHE_HOST=localhost
|
||||
CACHE_PORT=6379
|
||||
CACHE_USERNAME=
|
||||
CACHE_PASSWORD=
|
||||
CACHE_DB=1
|
||||
CACHE_TTL=1800
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
# IPTV Checker (iptvc)
|
||||
|
||||
[](https://git.axenov.dev/IPTV/iptvc/releases/latest)
|
||||
|
||||
Консольная программа для проверки IPTV-плейлистов в формате m3u или m3u8.
|
||||
|
||||
> [!IMPORTANT]
|
||||
|
||||
15
app/app.go
15
app/app.go
@@ -7,11 +7,13 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"axenov/iptv-checker/app/cache"
|
||||
"axenov/iptv-checker/app/config"
|
||||
"axenov/iptv-checker/app/logger"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
const VERSION = "0.2.0"
|
||||
const VERSION = "1.0.2"
|
||||
|
||||
// Arguments описывает аргументы командной строки
|
||||
type Arguments struct {
|
||||
@@ -25,14 +27,15 @@ type Arguments struct {
|
||||
|
||||
var (
|
||||
Args Arguments
|
||||
Redis *redis.Client
|
||||
Cache *redis.Client
|
||||
Config *config.Config
|
||||
//TagBlocks []tagfile.TagBlock
|
||||
)
|
||||
|
||||
// Init инициализирует глобальные переменные
|
||||
// Init инициализирует конфигурацию и подключение к keydb
|
||||
func Init() {
|
||||
Config = config.Init()
|
||||
//logger.Init(Args.NeedQuiet)
|
||||
//Redis = cache.Init(Config.Redis)
|
||||
logger.Init(Args.NeedQuiet)
|
||||
if Config.Cache.IsEnabled {
|
||||
Cache = cache.Init(&Config.Cache)
|
||||
}
|
||||
}
|
||||
|
||||
19
app/cache/cache.go
vendored
19
app/cache/cache.go
vendored
@@ -15,22 +15,23 @@ import (
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func Init(cfg config.RedisConfig) *redis.Client {
|
||||
rdb := redis.NewClient(&redis.Options{
|
||||
func Init(cfg *config.CacheConfig) *redis.Client {
|
||||
redis := redis.NewClient(&redis.Options{
|
||||
Addr: fmt.Sprintf("%s:%s", cfg.Host, strconv.Itoa(int(cfg.Port))),
|
||||
DB: int(cfg.Db),
|
||||
PoolSize: 1000,
|
||||
ReadTimeout: -1,
|
||||
WriteTimeout: -1,
|
||||
})
|
||||
client := rdb.Conn()
|
||||
|
||||
var ctx context.Context
|
||||
if client.Ping(ctx).Err() != nil {
|
||||
log.Println("Error while connecting to Redis", cfg.Host, cfg.Port, cfg.Db)
|
||||
client := redis.Conn()
|
||||
ctx := context.Background()
|
||||
err := client.Ping(ctx).Err()
|
||||
if err == nil {
|
||||
log.Println("Connected to cache DB")
|
||||
cfg.IsActive = true
|
||||
} else {
|
||||
log.Println("Connected to Redis", cfg.Host, cfg.Port, cfg.Db)
|
||||
log.Println("Error while connecting to cache DB, program may work not as expected:", err)
|
||||
}
|
||||
|
||||
return rdb
|
||||
return redis
|
||||
}
|
||||
|
||||
@@ -12,10 +12,13 @@ import (
|
||||
"axenov/iptv-checker/app/playlist"
|
||||
"axenov/iptv-checker/app/tagfile"
|
||||
"axenov/iptv-checker/app/utils"
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"log"
|
||||
"maps"
|
||||
"math"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"os"
|
||||
@@ -26,7 +29,10 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
var tagBlocks []tagfile.TagBlock
|
||||
var (
|
||||
tagBlocks []tagfile.TagBlock
|
||||
ctx = context.Background()
|
||||
)
|
||||
|
||||
// PrepareListsToCheck готовит список плейлистов для проверки
|
||||
func PrepareListsToCheck(files []string, urls []string, codes []string) []playlist.Playlist {
|
||||
@@ -67,8 +73,20 @@ func PrepareListsToCheck(files []string, urls []string, codes []string) []playli
|
||||
}
|
||||
lists = append(lists, list)
|
||||
}
|
||||
} else {
|
||||
if app.Config.Cache.IsActive {
|
||||
cachedLists := getCachedPlaylists()
|
||||
for key := range ini.Lists {
|
||||
if _, ok := cachedLists[key]; ok {
|
||||
continue
|
||||
}
|
||||
lists = append(lists, ini.Lists[key])
|
||||
}
|
||||
log.Printf("Found %d cached playlists\n", len(cachedLists))
|
||||
} else {
|
||||
lists = slices.Collect(maps.Values(ini.Lists))
|
||||
}
|
||||
|
||||
if int(app.Args.RandomCount) > 0 && int(app.Args.RandomCount) <= len(lists) {
|
||||
rand.Shuffle(len(lists), func(i int, j int) { lists[i], lists[j] = lists[j], lists[i] })
|
||||
lists = lists[:app.Args.RandomCount]
|
||||
@@ -79,17 +97,31 @@ func PrepareListsToCheck(files []string, urls []string, codes []string) []playli
|
||||
return lists
|
||||
}
|
||||
|
||||
// getCachedPlaylists возвращает из кеша проверенные ранее плейлисты
|
||||
func getCachedPlaylists() map[string]playlist.Playlist {
|
||||
result := make(map[string]playlist.Playlist)
|
||||
keys := app.Cache.Keys(ctx, "*")
|
||||
for _, key := range keys.Val() {
|
||||
value := app.Cache.Get(ctx, key).Val()
|
||||
var pls playlist.Playlist
|
||||
_ = json.Unmarshal([]byte(value), &pls)
|
||||
result[pls.Code] = pls
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// CheckPlaylists проверяет плейлисты и возвращает их же с результатами проверки
|
||||
func CheckPlaylists(lists []playlist.Playlist) (int, int) {
|
||||
step, onlineCount, offlineCount := 0, 0, 0
|
||||
count := len(lists)
|
||||
tagBlocks = tagfile.Init(app.Args.TagsPath)
|
||||
|
||||
if count == 0 {
|
||||
log.Println("There are no playlists to check")
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
log.Printf("%d playlists will be checked\n", len(lists))
|
||||
step, onlineCount, offlineCount := 0, 0, 0
|
||||
tagBlocks = tagfile.Init(app.Args.TagsPath)
|
||||
|
||||
for idx := range lists {
|
||||
pls := lists[idx]
|
||||
step++
|
||||
@@ -115,6 +147,7 @@ func CheckPlaylists(lists []playlist.Playlist) (int, int) {
|
||||
if err != nil {
|
||||
log.Printf("Cannot read playlist [%s]: %s\n", pls.Url, err)
|
||||
offlineCount++
|
||||
cachePlaylist(pls)
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -125,13 +158,30 @@ func CheckPlaylists(lists []playlist.Playlist) (int, int) {
|
||||
|
||||
log.Printf("Parsed, checking channels (%d)...\n", len(pls.Channels))
|
||||
pls = CheckChannels(pls)
|
||||
|
||||
lists[idx] = pls
|
||||
cachePlaylist(pls)
|
||||
}
|
||||
|
||||
return onlineCount, offlineCount
|
||||
}
|
||||
|
||||
func cachePlaylist(pls playlist.Playlist) {
|
||||
if app.Config.Cache.IsActive {
|
||||
jsonBytes, err := json.Marshal(pls)
|
||||
if err != nil {
|
||||
log.Printf("Error while saving playlist to cache: %s", err)
|
||||
}
|
||||
|
||||
ttl := time.Duration(app.Config.Cache.Ttl) * time.Second
|
||||
written := app.Cache.Set(ctx, pls.Code, string(jsonBytes), ttl)
|
||||
if written.Err() != nil {
|
||||
log.Printf("Error while saving playlist to cache: %s", err)
|
||||
}
|
||||
|
||||
log.Println("Cached sucessfully")
|
||||
}
|
||||
}
|
||||
|
||||
// CheckChannels проверяет каналы и возвращает их же с результатами проверки
|
||||
func CheckChannels(pls playlist.Playlist) playlist.Playlist {
|
||||
type errorData struct {
|
||||
@@ -188,7 +238,7 @@ func CheckChannels(pls playlist.Playlist) playlist.Playlist {
|
||||
tvChannel.ContentType = resp.Header.Get("Content-Type")
|
||||
bodyBytes, _ := io.ReadAll(resp.Body)
|
||||
bodyString := string(bodyBytes)
|
||||
resp.Body.Close()
|
||||
_ = resp.Body.Close()
|
||||
contentType := http.DetectContentType(bodyBytes)
|
||||
|
||||
isContentBinary := strings.Contains(contentType, "octet-stream") ||
|
||||
@@ -219,7 +269,6 @@ func CheckChannels(pls playlist.Playlist) playlist.Playlist {
|
||||
select {
|
||||
case tvChannel := <-chOnline:
|
||||
tvChannel.IsOnline = true
|
||||
pls.OnlineCount++
|
||||
pls.Channels[tvChannel.Id] = tvChannel
|
||||
if app.Args.Verbose {
|
||||
log.Printf("[%.3d/%.3d] ONLINE '%s'\n", idx, count, tvChannel.Title)
|
||||
@@ -228,7 +277,6 @@ func CheckChannels(pls playlist.Playlist) playlist.Playlist {
|
||||
log.Printf("> MimeType: %s\n", tvChannel.ContentType)
|
||||
}
|
||||
case tvChannel := <-chOffline:
|
||||
pls.OfflineCount++
|
||||
pls.Channels[tvChannel.Id] = tvChannel
|
||||
if app.Args.Verbose {
|
||||
log.Printf("[%.3d/%.3d] OFFLINE '%s'\n", idx, count, tvChannel.Title)
|
||||
@@ -237,7 +285,6 @@ func CheckChannels(pls playlist.Playlist) playlist.Playlist {
|
||||
log.Printf("> Status: %d\n", tvChannel.Status)
|
||||
}
|
||||
case data := <-chError:
|
||||
pls.OfflineCount++
|
||||
pls.Channels[data.tvChannel.Id] = data.tvChannel
|
||||
if app.Args.Verbose {
|
||||
log.Printf("[%.3d/%.3d] ERROR '%s'\n", idx, count, data.tvChannel.Title)
|
||||
@@ -254,6 +301,14 @@ func CheckChannels(pls playlist.Playlist) playlist.Playlist {
|
||||
close(chError)
|
||||
pls.CheckedAt = time.Now().Unix()
|
||||
|
||||
for _, tvChannel := range pls.Channels {
|
||||
if tvChannel.IsOnline {
|
||||
pls.OnlineCount++
|
||||
} else {
|
||||
pls.OfflineCount++
|
||||
}
|
||||
}
|
||||
|
||||
log.Printf(
|
||||
"Checked successfully! online=%d onlinePercent=%.2f%% offline=%d offlinePercent=%.2f%% elapsedTime=%.2fs",
|
||||
pls.OnlineCount,
|
||||
@@ -268,26 +323,27 @@ func CheckChannels(pls playlist.Playlist) playlist.Playlist {
|
||||
|
||||
// calcParameters вычисляет оптимальное количество горутин и таймаут запроса
|
||||
func calcParameters(count int) (time.Duration, int) {
|
||||
var routines int
|
||||
var percentage float32
|
||||
|
||||
if count <= 100 {
|
||||
routines = count
|
||||
} else {
|
||||
percentage = float32(runtime.NumCPU()) * 0.075
|
||||
percentage := float32(runtime.NumCPU()) / 10
|
||||
for percentage >= 1 {
|
||||
percentage *= 0.5
|
||||
}
|
||||
routines = int(float32(count) * percentage)
|
||||
}
|
||||
if routines > 500 {
|
||||
routines = 500
|
||||
|
||||
routines := int(float32(count) * percentage)
|
||||
if routines > 1500 {
|
||||
routines = 1500
|
||||
}
|
||||
if routines < 1 {
|
||||
routines = 1
|
||||
}
|
||||
|
||||
timeout := 10 / float32(count) * 150
|
||||
var digits int
|
||||
x := count
|
||||
for x >= 10 {
|
||||
digits++
|
||||
x /= 10
|
||||
}
|
||||
|
||||
timeout := int(math.Ceil(math.Pow(10, float64(digits)) / float64(count) * 15))
|
||||
if timeout > 10 {
|
||||
timeout = 10
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"github.com/joho/godotenv"
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
@@ -14,39 +15,34 @@ import (
|
||||
// Config описывает конфигурацию
|
||||
type Config struct {
|
||||
DebugMode bool
|
||||
Redis RedisConfig
|
||||
Http HttpConfig
|
||||
Cache CacheConfig
|
||||
}
|
||||
|
||||
// RedisConfig описывает конфигурацию подключения к Redis
|
||||
type RedisConfig struct {
|
||||
// CacheConfig описывает конфигурацию подключения к keydb
|
||||
type CacheConfig struct {
|
||||
IsEnabled bool
|
||||
Host string
|
||||
Port uint
|
||||
Username string
|
||||
Password string
|
||||
Db uint
|
||||
}
|
||||
|
||||
// HttpConfig описывает конфигурацию веб-сервера
|
||||
type HttpConfig struct {
|
||||
Host string
|
||||
Port uint
|
||||
Ttl uint
|
||||
IsActive bool
|
||||
}
|
||||
|
||||
// Init инициализирует объект конфигурации из переменных окружения
|
||||
func Init() *Config {
|
||||
_ = godotenv.Load(".env")
|
||||
return &Config{
|
||||
DebugMode: readEnvBoolean("APP_DEBUG", false),
|
||||
Redis: RedisConfig{
|
||||
Host: readEnv("REDIS_HOST", ""),
|
||||
Port: readEnvInteger("REDIS_PORT", 6379),
|
||||
Username: readEnv("REDIS_USERNAME", ""),
|
||||
Password: readEnv("REDIS_PASSWORD", ""),
|
||||
Db: readEnvInteger("REDIS_DB", 0),
|
||||
},
|
||||
Http: HttpConfig{
|
||||
Host: readEnv("HTTP_HOST", "0.0.0.0"),
|
||||
Port: readEnvInteger("HTTP_PORT", 1380),
|
||||
//DebugMode: readEnvBoolean("APP_DEBUG", false),
|
||||
Cache: CacheConfig{
|
||||
IsEnabled: readEnvBoolean("CACHE_ENABLED", false),
|
||||
Host: readEnv("CACHE_HOST", "localhost"),
|
||||
Port: readEnvInteger("CACHE_PORT", 6379),
|
||||
Username: readEnv("CACHE_USERNAME", ""),
|
||||
Password: readEnv("CACHE_PASSWORD", ""),
|
||||
Db: readEnvInteger("CACHE_DB", 0),
|
||||
Ttl: readEnvInteger("CACHE_TTL", 1800),
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -57,7 +53,6 @@ func readEnv(key string, defaultValue string) string {
|
||||
if exists {
|
||||
return value
|
||||
}
|
||||
|
||||
return defaultValue
|
||||
}
|
||||
|
||||
|
||||
@@ -44,7 +44,7 @@ func Init(path string) (IniFile, error) {
|
||||
|
||||
log.Println("Loading playlists from ini-file:", pathNormalized)
|
||||
for _, section := range iniFile.Sections() {
|
||||
if section.Name() == ini.DefaultSection { //TODO выкосить костыль
|
||||
if section.Name() == ini.DefaultSection {
|
||||
continue
|
||||
}
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
"os"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Group - структура для хранения информации о группе каналов
|
||||
@@ -123,6 +124,8 @@ func parseName(line string) string {
|
||||
func (pls *Playlist) Download() error {
|
||||
content, err := utils.Fetch(pls.Url)
|
||||
if err != nil {
|
||||
pls.Content = err.Error()
|
||||
pls.CheckedAt = time.Now().Unix()
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -134,6 +137,8 @@ func (pls *Playlist) Download() error {
|
||||
func (pls *Playlist) ReadFromFs() error {
|
||||
content, err := os.ReadFile(pls.Url)
|
||||
if err != nil {
|
||||
pls.Content = err.Error()
|
||||
pls.CheckedAt = time.Now().Unix()
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -144,6 +149,7 @@ func (pls *Playlist) ReadFromFs() error {
|
||||
// Parse разбирает плейлист
|
||||
func (pls *Playlist) Parse() Playlist {
|
||||
isChannel := false
|
||||
pls.Attributes = make(map[string]string)
|
||||
pls.Channels = make(map[string]Channel)
|
||||
pls.Groups = make(map[string]Group)
|
||||
|
||||
|
||||
@@ -9,7 +9,6 @@ package cmd
|
||||
import (
|
||||
"axenov/iptv-checker/app"
|
||||
"axenov/iptv-checker/app/checker"
|
||||
"axenov/iptv-checker/app/logger"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/spf13/cobra"
|
||||
@@ -22,7 +21,7 @@ var checkCmd = &cobra.Command{
|
||||
Use: "check",
|
||||
Short: "Check playlists",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
logger.Init(app.Args.NeedQuiet)
|
||||
app.Init()
|
||||
|
||||
files, _ := cmd.Flags().GetStringSlice("file")
|
||||
urls, _ := cmd.Flags().GetStringSlice("url")
|
||||
|
||||
@@ -27,7 +27,6 @@ Copyright (c) 2025, Антон Аксенов, MIT license.`,
|
||||
// Execute adds all child commands to the root command and sets flags appropriately.
|
||||
// This is called by main.main(). It only needs to happen once to the rootCmd.
|
||||
func Execute() {
|
||||
app.Init()
|
||||
err := rootCmd.Execute()
|
||||
if err != nil {
|
||||
os.Exit(1)
|
||||
|
||||
3
go.mod
3
go.mod
@@ -3,7 +3,9 @@ module axenov/iptv-checker
|
||||
go 1.23.6
|
||||
|
||||
require (
|
||||
github.com/joho/godotenv v1.5.1
|
||||
github.com/redis/go-redis/v9 v9.7.3
|
||||
github.com/spf13/cobra v1.9.1
|
||||
gopkg.in/ini.v1 v1.67.0
|
||||
)
|
||||
|
||||
@@ -12,7 +14,6 @@ require (
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
||||
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
||||
github.com/spf13/cobra v1.9.1 // indirect
|
||||
github.com/spf13/pflag v1.0.6 // indirect
|
||||
github.com/stretchr/testify v1.7.0 // indirect
|
||||
)
|
||||
|
||||
4
go.sum
4
go.sum
@@ -12,6 +12,8 @@ github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/r
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
|
||||
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
|
||||
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
|
||||
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
|
||||
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/redis/go-redis/v9 v9.7.3 h1:YpPyAayJV+XErNsatSElgRZZVCwXX9QzkKYNvO7x0wM=
|
||||
@@ -27,6 +29,6 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
|
||||
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
||||
Reference in New Issue
Block a user