Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
d6b133a8e0
|
|||
|
c9486c54b2
|
|||
|
ac062aa1ba
|
|||
|
bcd45e34bf
|
|||
|
b3ee981bd1
|
|||
|
182b9a92ce
|
|||
|
fbc1870ce7
|
|||
|
edd18e92ed
|
|||
|
dc61d47b66
|
|||
|
10c3b8f5c1
|
@@ -4,8 +4,7 @@
|
||||
|
||||
Консольная программа для проверки 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)
|
||||
> Telegram-канал: [@iptv_aggregator](https://t.me/iptv_aggregator)
|
||||
> Обсуждение: [@iptv_aggregator_chat](https://t.me/iptv_aggregator_chat)
|
||||
|
||||
@@ -10,10 +10,11 @@ import (
|
||||
"axenov/iptv-checker/app/cache"
|
||||
"axenov/iptv-checker/app/config"
|
||||
"axenov/iptv-checker/app/logger"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
const VERSION = "1.0.4"
|
||||
const VERSION = "1.0.6"
|
||||
|
||||
// Arguments описывает аргументы командной строки
|
||||
type Arguments struct {
|
||||
|
||||
14
app/cache/cache.go
vendored
14
app/cache/cache.go
vendored
@@ -10,28 +10,30 @@ import (
|
||||
"axenov/iptv-checker/app/config"
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"log"
|
||||
"strconv"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
func Init(cfg *config.CacheConfig) *redis.Client {
|
||||
redis := redis.NewClient(&redis.Options{
|
||||
Addr: fmt.Sprintf("%s:%s", cfg.Host, strconv.Itoa(int(cfg.Port))),
|
||||
redisUrl := fmt.Sprintf("%s:%s", cfg.Host, strconv.Itoa(int(cfg.Port)))
|
||||
redisClient := redis.NewClient(&redis.Options{
|
||||
Addr: redisUrl,
|
||||
DB: int(cfg.Db),
|
||||
PoolSize: 1000,
|
||||
ReadTimeout: -1,
|
||||
WriteTimeout: -1,
|
||||
})
|
||||
client := redis.Conn()
|
||||
client := redisClient.Conn()
|
||||
ctx := context.Background()
|
||||
err := client.Ping(ctx).Err()
|
||||
if err == nil {
|
||||
log.Println("Connected to cache DB")
|
||||
log.Println("Connected to cache DB:", redisUrl)
|
||||
cfg.IsActive = true
|
||||
} else {
|
||||
log.Println("Error while connecting to cache DB, program may work not as expected:", err)
|
||||
}
|
||||
|
||||
return redis
|
||||
return redisClient
|
||||
}
|
||||
|
||||
@@ -22,7 +22,6 @@ import (
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"os"
|
||||
"runtime"
|
||||
"slices"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -166,20 +165,22 @@ func CheckPlaylists(lists []playlist.Playlist) (int, int) {
|
||||
}
|
||||
|
||||
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")
|
||||
if !app.Config.Cache.IsActive {
|
||||
return
|
||||
}
|
||||
|
||||
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 проверяет каналы и возвращает их же с результатами проверки
|
||||
@@ -223,9 +224,8 @@ func CheckChannels(pls playlist.Playlist) playlist.Playlist {
|
||||
return
|
||||
}
|
||||
|
||||
//TODO user-agent
|
||||
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)
|
||||
tvChannel.CheckedAt = time.Now().Unix()
|
||||
if err != nil {
|
||||
@@ -237,7 +237,7 @@ func CheckChannels(pls playlist.Playlist) playlist.Playlist {
|
||||
tvChannel.Status = resp.StatusCode
|
||||
tvChannel.IsOnline = tvChannel.Status < http.StatusBadRequest
|
||||
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)
|
||||
bodyString := string(bodyBytes)
|
||||
_ = resp.Body.Close()
|
||||
@@ -248,11 +248,12 @@ func CheckChannels(pls playlist.Playlist) playlist.Playlist {
|
||||
|
||||
isContentCorrect := isContentBinary ||
|
||||
strings.Contains(bodyString, "#EXTM3U") ||
|
||||
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
|
||||
chOffline <- tvChannel
|
||||
return
|
||||
@@ -321,27 +322,22 @@ func CheckChannels(pls playlist.Playlist) playlist.Playlist {
|
||||
|
||||
// calcParameters вычисляет оптимальное количество горутин и таймаут запроса
|
||||
func calcParameters(count int) (time.Duration, int) {
|
||||
percentage := float32(runtime.NumCPU()) / 10
|
||||
for percentage >= 1 {
|
||||
percentage *= 0.5
|
||||
}
|
||||
|
||||
routines := int(float32(count) * percentage)
|
||||
if routines > 1500 {
|
||||
routines = 1500
|
||||
routines := count
|
||||
if routines > 3000 {
|
||||
routines = 3000
|
||||
}
|
||||
if routines < 1 {
|
||||
routines = 1
|
||||
}
|
||||
|
||||
var digits int
|
||||
var digits = 1
|
||||
x := count
|
||||
for x >= 10 {
|
||||
digits++
|
||||
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 {
|
||||
timeout = 10
|
||||
}
|
||||
|
||||
@@ -106,17 +106,28 @@ func parseAttributes(line string) map[string]string {
|
||||
return result
|
||||
}
|
||||
|
||||
// parseName парсит название канала из строки тега #EXTINF
|
||||
func parseName(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], " ")
|
||||
}
|
||||
|
||||
// parseTitle парсит название канала из строки тега #EXTINF
|
||||
func parseTitle(line string) string {
|
||||
// сначала пытаемся по-доброму: в строке есть тег, могут быть атрибуты,
|
||||
// есть запятая-разделитель, после неё -- название канала (с запятыми или без)
|
||||
regex := regexp.MustCompile(`['"]?\s*,\s*(.+)`)
|
||||
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-адресу
|
||||
@@ -170,7 +181,7 @@ func (pls *Playlist) Parse() Playlist {
|
||||
if strings.HasPrefix(line, "#EXTINF") {
|
||||
isChannel = true
|
||||
tmpChannel.Attributes = parseAttributes(line)
|
||||
tmpChannel.Title = parseName(line)
|
||||
tmpChannel.Title = parseTitle(line)
|
||||
|
||||
if tmpChannel.Title == "" {
|
||||
if tvgid, ok := tmpChannel.Attributes["tvg-id"]; ok {
|
||||
|
||||
@@ -51,13 +51,9 @@ func Fetch(url string) ([]byte, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
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")
|
||||
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)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -11,9 +11,10 @@ import (
|
||||
"axenov/iptv-checker/app/checker"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/spf13/cobra"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// checkCmd represents the file command
|
||||
|
||||
@@ -16,8 +16,8 @@ import (
|
||||
// rootCmd represents the base command when called without any subcommands
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: "iptvc",
|
||||
Short: "Simple utility to check iptv playlists. Part of iptv.axenov.dev project.",
|
||||
Long: `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 m3u.su project.
|
||||
Copyright (c) 2025, Антон Аксенов, MIT license.`,
|
||||
// Uncomment the following line if your bare application
|
||||
// has an action associated with it:
|
||||
|
||||
Reference in New Issue
Block a user