13 Commits

Author SHA1 Message Date
d6b133a8e0 Версия 1.0.6
All checks were successful
release / release (push) Successful in 6m33s
2025-11-19 00:15:38 +08:00
c9486c54b2 Улучшен парсинг названий каналов (#7) 2025-11-18 23:49:28 +08:00
ac062aa1ba Версия 1.0.5
All checks were successful
release / release (push) Successful in 11m41s
2025-10-02 22:31:08 +08:00
bcd45e34bf Изменены ссылки на сайт 2025-10-02 01:22:58 +08:00
b3ee981bd1 Мелочи по зависимостям 2025-10-02 01:22:21 +08:00
182b9a92ce Увеличен таймаут получения плейлиста до 10 сек 2025-10-02 01:18:41 +08:00
fbc1870ce7 Снижен размер чанка до 512Б при проверке канала 2025-10-02 01:16:40 +08:00
edd18e92ed Корректировка расчёта рутин/таймаута 2025-10-02 01:15:08 +08:00
dc61d47b66 Вывод строки подключения redis 2025-10-02 01:12:46 +08:00
10c3b8f5c1 Фикс проверки рабочих каналов при корректном контенте, но ошибочном HTTP-коде 2025-10-01 12:21:27 +08:00
041b32e1df Версия 1.0.4
All checks were successful
release / release (push) Successful in 1m23s
2025-05-16 23:10:02 +08:00
e98d923ce5 Оптимизация проверки каналов (#5)
- теперь проверяется только первый 1 Кб контента
- скорректирована проверка mpd-контента
2025-05-16 23:09:27 +08:00
01ddf25ed5 Поддержка тегирования по атрибуту tvg-name канала 2025-05-13 16:48:34 +08:00
10 changed files with 84 additions and 67 deletions

1
.gitignore vendored
View File

@@ -9,5 +9,6 @@ output/
*.m3u8 *.m3u8
*.json *.json
*.ini *.ini
iptvc
!/**/*.gitkeep !/**/*.gitkeep

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.3" 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 проверяет каналы и возвращает их же с результатами проверки
@@ -217,16 +218,16 @@ func CheckChannels(pls playlist.Playlist) playlist.Playlist {
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true} http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
req, err := http.NewRequest("GET", tvChannel.URL, nil) req, err := http.NewRequest("GET", tvChannel.URL, nil)
tvChannel.CheckedAt = time.Now().Unix()
if err != nil { if err != nil {
data := errorData{tvChannel: tvChannel, err: err} data := errorData{tvChannel: tvChannel, err: err}
chError <- data chError <- data
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.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()
if err != nil { if err != nil {
data := errorData{tvChannel: tvChannel, err: err} data := errorData{tvChannel: tvChannel, err: err}
chError <- data chError <- data
@@ -236,7 +237,8 @@ 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")
bodyBytes, _ := io.ReadAll(resp.Body) chunk := io.LimitReader(resp.Body, 512) // just for sure
bodyBytes, _ := io.ReadAll(chunk)
bodyString := string(bodyBytes) bodyString := string(bodyBytes)
_ = resp.Body.Close() _ = resp.Body.Close()
contentType := http.DetectContentType(bodyBytes) contentType := http.DetectContentType(bodyBytes)
@@ -246,20 +248,17 @@ func CheckChannels(pls playlist.Playlist) playlist.Playlist {
isContentCorrect := isContentBinary || isContentCorrect := isContentBinary ||
strings.Contains(bodyString, "#EXTM3U") || strings.Contains(bodyString, "#EXTM3U") ||
strings.Contains(bodyString, "<SegmentTemplate") strings.Contains(bodyString, "#EXT-X-") ||
strings.Contains(bodyString, "<MPD ") ||
strings.Contains(bodyString, "<SegmentTemplate ") ||
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
} }
if isContentBinary {
tvChannel.Content = "binary"
} else {
tvChannel.Content = bodyString
}
chOnline <- tvChannel chOnline <- tvChannel
return return
}(tvChannel) }(tvChannel)
@@ -323,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

@@ -32,7 +32,6 @@ type Channel struct {
Status int `json:"status"` // Код статуса HTTP Status int `json:"status"` // Код статуса HTTP
IsOnline bool `json:"isOnline"` // Признак доступности канала (при Status < 400) IsOnline bool `json:"isOnline"` // Признак доступности канала (при Status < 400)
Error string `json:"error"` // Текст ошибки (при Status >= 400) Error string `json:"error"` // Текст ошибки (при Status >= 400)
Content string `json:"content"` // Тело ответа (формат m3u, либо маскированные бинарные данные, либо пусто)
ContentType string `json:"contentType"` // MIME-тип тела ответа ContentType string `json:"contentType"` // MIME-тип тела ответа
Tags []string `json:"tags"` // Список тегов канала Tags []string `json:"tags"` // Список тегов канала
CheckedAt int64 `json:"checkedAt"` // Время проверки в формате UNIX timestamp CheckedAt int64 `json:"checkedAt"` // Время проверки в формате UNIX timestamp
@@ -107,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-адресу
@@ -171,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

@@ -19,6 +19,7 @@ import (
// TagBlock описывает объект с набором тегов, который подходит для каналов по регулярному выражению // TagBlock описывает объект с набором тегов, который подходит для каналов по регулярному выражению
type TagBlock struct { type TagBlock struct {
TvgId string `json:"tvg-id"` TvgId string `json:"tvg-id"`
TvgName string `json:"tvg-name"`
Title string `json:"title"` Title string `json:"title"`
Tags []string `json:"tags"` Tags []string `json:"tags"`
} }
@@ -42,6 +43,18 @@ func (block *TagBlock) GetTags(ch playlist.Channel) []string {
if checkString == "" { if checkString == "" {
return result return result
} }
} else if block.TvgName != "" {
regex, err = regexp.Compile(block.TvgName)
if err != nil {
return result
}
if _, ok := ch.Attributes["tvg-name"]; !ok {
return result
}
checkString = ch.Attributes["tvg-name"]
if checkString == "" {
return result
}
} else if block.Title != "" { } else if block.Title != "" {
regex, err = regexp.Compile(block.Title) regex, err = regexp.Compile(block.Title)
if err != nil { if err != nil {

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: