10 Commits

8 changed files with 62 additions and 56 deletions

View File

@@ -4,8 +4,7 @@
Консольная программа для проверки IPTV-плейлистов в формате m3u или m3u8. Консольная программа для проверки IPTV-плейлистов в формате m3u или m3u8.
> **Веб-сайт:** [iptv.axenov.dev](https://iptv.axenov.dev) > **Веб-сайт:** [m3u.su](https://m3u.su)
> **Зеркало:** [m3u.su](https://m3u.su)
> Исходный код: [git.axenov.dev/IPTV/iptvc](https://git.axenov.dev/IPTV/iptvc) > Исходный код: [git.axenov.dev/IPTV/iptvc](https://git.axenov.dev/IPTV/iptvc)
> Telegram-канал: [@iptv_aggregator](https://t.me/iptv_aggregator) > Telegram-канал: [@iptv_aggregator](https://t.me/iptv_aggregator)
> Обсуждение: [@iptv_aggregator_chat](https://t.me/iptv_aggregator_chat) > Обсуждение: [@iptv_aggregator_chat](https://t.me/iptv_aggregator_chat)

View File

@@ -10,10 +10,11 @@ import (
"axenov/iptv-checker/app/cache" "axenov/iptv-checker/app/cache"
"axenov/iptv-checker/app/config" "axenov/iptv-checker/app/config"
"axenov/iptv-checker/app/logger" "axenov/iptv-checker/app/logger"
"github.com/redis/go-redis/v9" "github.com/redis/go-redis/v9"
) )
const VERSION = "1.0.4" const VERSION = "1.0.6"
// Arguments описывает аргументы командной строки // Arguments описывает аргументы командной строки
type Arguments struct { type Arguments struct {

14
app/cache/cache.go vendored
View File

@@ -10,28 +10,30 @@ import (
"axenov/iptv-checker/app/config" "axenov/iptv-checker/app/config"
"context" "context"
"fmt" "fmt"
"github.com/redis/go-redis/v9"
"log" "log"
"strconv" "strconv"
"github.com/redis/go-redis/v9"
) )
func Init(cfg *config.CacheConfig) *redis.Client { func Init(cfg *config.CacheConfig) *redis.Client {
redis := redis.NewClient(&redis.Options{ redisUrl := fmt.Sprintf("%s:%s", cfg.Host, strconv.Itoa(int(cfg.Port)))
Addr: fmt.Sprintf("%s:%s", cfg.Host, strconv.Itoa(int(cfg.Port))), redisClient := redis.NewClient(&redis.Options{
Addr: redisUrl,
DB: int(cfg.Db), DB: int(cfg.Db),
PoolSize: 1000, PoolSize: 1000,
ReadTimeout: -1, ReadTimeout: -1,
WriteTimeout: -1, WriteTimeout: -1,
}) })
client := redis.Conn() client := redisClient.Conn()
ctx := context.Background() ctx := context.Background()
err := client.Ping(ctx).Err() err := client.Ping(ctx).Err()
if err == nil { if err == nil {
log.Println("Connected to cache DB") log.Println("Connected to cache DB:", redisUrl)
cfg.IsActive = true cfg.IsActive = true
} else { } else {
log.Println("Error while connecting to cache DB, program may work not as expected:", err) log.Println("Error while connecting to cache DB, program may work not as expected:", err)
} }
return redis return redisClient
} }

View File

@@ -22,7 +22,6 @@ import (
"math/rand" "math/rand"
"net/http" "net/http"
"os" "os"
"runtime"
"slices" "slices"
"strings" "strings"
"sync" "sync"
@@ -166,7 +165,10 @@ func CheckPlaylists(lists []playlist.Playlist) (int, int) {
} }
func cachePlaylist(pls playlist.Playlist) { func cachePlaylist(pls playlist.Playlist) {
if app.Config.Cache.IsActive { if !app.Config.Cache.IsActive {
return
}
jsonBytes, err := json.Marshal(pls) jsonBytes, err := json.Marshal(pls)
if err != nil { if err != nil {
log.Printf("Error while saving playlist to cache: %s", err) log.Printf("Error while saving playlist to cache: %s", err)
@@ -179,7 +181,6 @@ func cachePlaylist(pls playlist.Playlist) {
} }
log.Println("Cached sucessfully") log.Println("Cached sucessfully")
}
} }
// CheckChannels проверяет каналы и возвращает их же с результатами проверки // CheckChannels проверяет каналы и возвращает их же с результатами проверки
@@ -223,9 +224,8 @@ func CheckChannels(pls playlist.Playlist) playlist.Playlist {
return return
} }
//TODO user-agent
req.Header.Set("User-Agent", "Mozilla/5.0 WINK/1.31.1 (AndroidTV/9) HlsWinkPlayer") req.Header.Set("User-Agent", "Mozilla/5.0 WINK/1.31.1 (AndroidTV/9) HlsWinkPlayer")
req.Header.Add("Range", "bytes=0-1023") // 1 Kb, but sometimes servers ignore it req.Header.Set("Range", "bytes=0-511") // 512 B, but sometimes servers ignore it
resp, err := httpClient.Do(req) resp, err := httpClient.Do(req)
tvChannel.CheckedAt = time.Now().Unix() tvChannel.CheckedAt = time.Now().Unix()
if err != nil { if err != nil {
@@ -237,7 +237,7 @@ func CheckChannels(pls playlist.Playlist) playlist.Playlist {
tvChannel.Status = resp.StatusCode tvChannel.Status = resp.StatusCode
tvChannel.IsOnline = tvChannel.Status < http.StatusBadRequest tvChannel.IsOnline = tvChannel.Status < http.StatusBadRequest
tvChannel.ContentType = resp.Header.Get("Content-Type") tvChannel.ContentType = resp.Header.Get("Content-Type")
chunk := io.LimitReader(resp.Body, 1024) // just for sure chunk := io.LimitReader(resp.Body, 512) // just for sure
bodyBytes, _ := io.ReadAll(chunk) bodyBytes, _ := io.ReadAll(chunk)
bodyString := string(bodyBytes) bodyString := string(bodyBytes)
_ = resp.Body.Close() _ = resp.Body.Close()
@@ -248,11 +248,12 @@ func CheckChannels(pls playlist.Playlist) playlist.Playlist {
isContentCorrect := isContentBinary || isContentCorrect := isContentBinary ||
strings.Contains(bodyString, "#EXTM3U") || strings.Contains(bodyString, "#EXTM3U") ||
strings.Contains(bodyString, "#EXT-X-") ||
strings.Contains(bodyString, "<MPD ") || strings.Contains(bodyString, "<MPD ") ||
strings.Contains(bodyString, "<SegmentTemplate ") || strings.Contains(bodyString, "<SegmentTemplate ") ||
strings.Contains(bodyString, "<AdaptationSet ") strings.Contains(bodyString, "<AdaptationSet ")
if tvChannel.Status >= http.StatusBadRequest || !isContentCorrect { if tvChannel.Status >= http.StatusBadRequest && !isContentCorrect {
tvChannel.Error = bodyString tvChannel.Error = bodyString
chOffline <- tvChannel chOffline <- tvChannel
return return
@@ -321,27 +322,22 @@ func CheckChannels(pls playlist.Playlist) playlist.Playlist {
// calcParameters вычисляет оптимальное количество горутин и таймаут запроса // calcParameters вычисляет оптимальное количество горутин и таймаут запроса
func calcParameters(count int) (time.Duration, int) { func calcParameters(count int) (time.Duration, int) {
percentage := float32(runtime.NumCPU()) / 10 routines := count
for percentage >= 1 { if routines > 3000 {
percentage *= 0.5 routines = 3000
}
routines := int(float32(count) * percentage)
if routines > 1500 {
routines = 1500
} }
if routines < 1 { if routines < 1 {
routines = 1 routines = 1
} }
var digits int var digits = 1
x := count x := count
for x >= 10 { for x >= 10 {
digits++ digits++
x /= 10 x /= 10
} }
timeout := int(math.Ceil(math.Pow(10, float64(digits)) / float64(count) * 15)) timeout := 10 - int(math.Ceil(float64(digits)*1.5))
if timeout > 10 { if timeout > 10 {
timeout = 10 timeout = 10
} }

View File

@@ -106,17 +106,28 @@ func parseAttributes(line string) map[string]string {
return result return result
} }
// parseName парсит название канала из строки тега #EXTINF // parseTitle парсит название канала из строки тега #EXTINF
func parseName(line string) string { func parseTitle(line string) string {
//TODO https://git.axenov.dev/IPTV/iptvc/issues/7 // сначала пытаемся по-доброму: в строке есть тег, могут быть атрибуты,
parts := strings.Split(line, ",") // есть запятая-разделитель, после неё -- название канала (с запятыми или без)
if len(parts) == 2 {
return strings.Trim(parts[1], " ")
}
regex := regexp.MustCompile(`['"]?\s*,\s*(.+)`) regex := regexp.MustCompile(`['"]?\s*,\s*(.+)`)
regexMatches := regex.FindAllStringSubmatch(line, -1) regexMatches := regex.FindAllStringSubmatch(line, -1)
return regexMatches[0][1] if len(regexMatches) > 0 && len(regexMatches[0]) >= 2 {
return strings.TrimSpace(regexMatches[0][1])
}
// теперь пытаемся хоть как-то: в строке есть тег, могут быть атрибуты,
// НЕТ запятой-разделителя и название канала (с запятыми или без)
lastQuotePos := strings.LastIndexAny(line, `,"'`)
if lastQuotePos != -1 && lastQuotePos < len(line)-1 {
afterLastQuote := line[lastQuotePos+1:]
name := strings.TrimSpace(afterLastQuote)
if name != "" {
return name
}
}
return line // ну штош
} }
// Download загружает плейлист по URL-адресу // Download загружает плейлист по URL-адресу
@@ -170,7 +181,7 @@ func (pls *Playlist) Parse() Playlist {
if strings.HasPrefix(line, "#EXTINF") { if strings.HasPrefix(line, "#EXTINF") {
isChannel = true isChannel = true
tmpChannel.Attributes = parseAttributes(line) tmpChannel.Attributes = parseAttributes(line)
tmpChannel.Title = parseName(line) tmpChannel.Title = parseTitle(line)
if tmpChannel.Title == "" { if tmpChannel.Title == "" {
if tvgid, ok := tmpChannel.Attributes["tvg-id"]; ok { if tvgid, ok := tmpChannel.Attributes["tvg-id"]; ok {

View File

@@ -51,13 +51,9 @@ func Fetch(url string) ([]byte, error) {
return nil, err return nil, err
} }
req.Header.Set( req.Header.Set("User-Agent", "Mozilla/5.0 WINK/1.31.1 (AndroidTV/9) HlsWinkPlayer")
"User-Agent",
"Mozilla/5.0 WINK/1.31.1 (AndroidTV/9) HlsWinkPlayer",
)
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true} http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
httpClient := http.Client{Timeout: 5 * time.Second} httpClient := http.Client{Timeout: 10 * time.Second}
resp, err := httpClient.Do(req) resp, err := httpClient.Do(req)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@@ -11,9 +11,10 @@ import (
"axenov/iptv-checker/app/checker" "axenov/iptv-checker/app/checker"
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/spf13/cobra"
"log" "log"
"time" "time"
"github.com/spf13/cobra"
) )
// checkCmd represents the file command // checkCmd represents the file command

View File

@@ -16,8 +16,8 @@ import (
// rootCmd represents the base command when called without any subcommands // rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{ var rootCmd = &cobra.Command{
Use: "iptvc", Use: "iptvc",
Short: "Simple utility to check iptv playlists. Part of iptv.axenov.dev project.", Short: "Simple utility to check iptv playlists. Part of m3u.su project.",
Long: `Simple utility to check iptv playlists. Part of iptv.axenov.dev project. Long: `Simple utility to check iptv playlists. Part of m3u.su project.
Copyright (c) 2025, Антон Аксенов, MIT license.`, Copyright (c) 2025, Антон Аксенов, MIT license.`,
// Uncomment the following line if your bare application // Uncomment the following line if your bare application
// has an action associated with it: // has an action associated with it: