Skip to content

Commit

Permalink
fix: 当返回的ChannelURL同时包含多个地址(组播和单播)时,无法正确解析的问题。
Browse files Browse the repository at this point in the history
  • Loading branch information
super321 committed Dec 22, 2024
1 parent 4519a17 commit 4139f9a
Showing 1 changed file with 26 additions and 7 deletions.
33 changes: 26 additions & 7 deletions internal/app/iptv/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"strconv"
"strings"
"time"

"go.uber.org/zap"
)

const otherGroupName = "其他"
Expand Down Expand Up @@ -48,13 +50,13 @@ func (c *Client) GetChannelList(ctx context.Context, token *Token) ([]Channel, e

// 组装请求数据
data := map[string]string{
"conntype": "dhcp",
"conntype": c.config.Conntype,
"UserToken": token.UserToken,
"tempKey": tempKey,
"stbid": token.Stbid,
"SupportHD": "1",
"UserID": c.config.UserID,
"Lang": "1",
"Lang": c.config.Lang,
}
body := url.Values{}
for k, v := range data {
Expand Down Expand Up @@ -102,7 +104,7 @@ func (c *Client) GetChannelList(ctx context.Context, token *Token) ([]Channel, e
}

// 过滤掉特殊频道的正则表达式
chExcludeRegex := regexp.MustCompile("^(.+?(画中画|单音轨))|(\\d+)$")
chExcludeRegex := regexp.MustCompile("^.+?(画中画|单音轨)$")

channels := make([]Channel, 0, len(matchesList))
for _, matches := range matchesList {
Expand All @@ -113,24 +115,41 @@ func (c *Client) GetChannelList(ctx context.Context, token *Token) ([]Channel, e
channelName := string(matches[2])
// 过滤掉特殊频道
if chExcludeRegex.MatchString(channelName) {
c.logger.Warn("This is not a normal channel, skip it.", zap.String("channelName", channelName))
continue
}

// channelURL类型转换
channelURL, err := url.Parse(string(matches[4]))
if err != nil {
// channelURL可能同时返回组播和单播多个地址(通过|分割),这里优先取组播地址
var channelURL *url.URL
channelURLStrList := strings.Split(string(matches[4]), "|")
for _, channelURLStr := range channelURLStrList {
channelURL, err = url.Parse(channelURLStr)
if err != nil {
continue
}

if channelURL != nil && channelURL.Scheme == "igmp" {
break
}
}

if channelURL == nil {
c.logger.Warn("The channelURL of this channel is illegal, skip it.", zap.String("channelName", channelName), zap.String("channelURL", string(matches[4])))
continue
}

// TimeShiftLength类型转换
timeShiftLength, err := strconv.ParseInt(string(matches[6]), 10, 64)
if err != nil {
c.logger.Warn("The timeShiftLength of this channel is illegal, skip it.", zap.String("channelName", channelName), zap.String("timeShiftLength", string(matches[6])))
continue
}

// 解析时移地址
timeShiftURL, err := url.Parse(string(matches[7]))
if err != nil {
c.logger.Warn("The timeShiftURL of this channel is illegal, skip it.", zap.String("channelName", channelName), zap.String("timeShiftURL", string(matches[7])))
continue
}
// 重置时移地址的查询参数
Expand Down Expand Up @@ -173,7 +192,7 @@ func ToM3UFormat(channels []Channel, udpxyURL, catchupSource string) (string, er
for _, channel := range channels {
var err error
var channelURL string
if udpxyURL != "" {
if udpxyURL != "" && channel.ChannelURL.Scheme == "igmp" {
channelURL, err = url.JoinPath(udpxyURL, fmt.Sprintf("/rtp/%s", channel.ChannelURL.Host))
if err != nil {
return "", err
Expand Down Expand Up @@ -229,7 +248,7 @@ func ToTxtFormat(channels []Channel, udpxyURL string) (string, error) {
for _, channel := range groupChannels {
var err error
var channelURL string
if udpxyURL != "" {
if udpxyURL != "" && channel.ChannelURL.Scheme == "igmp" {
channelURL, err = url.JoinPath(udpxyURL, fmt.Sprintf("/rtp/%s", channel.ChannelURL.Host))
if err != nil {
return "", err
Expand Down

0 comments on commit 4139f9a

Please sign in to comment.