Skip to content

Commit

Permalink
自动保存channel.txt
Browse files Browse the repository at this point in the history
  • Loading branch information
FrankHan1233 committed Jan 31, 2025
1 parent 7cee201 commit bda86df
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 47 deletions.
34 changes: 11 additions & 23 deletions internal/app/iptv/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ type Channel struct {
ChannelID string `json:"channelID"` // 频道ID
ChannelName string `json:"channelName"` // 频道名称
UserChannelID string `json:"userChannelID"` // 频道号
ChannelURLs []url.URL `json:"channelURLs"` // 频道URL列表
ChannelURL string `json:"channelURL"` // 频道URL
TimeShift string `json:"timeShift"` // 时移类型
TimeShiftLength time.Duration `json:"timeShiftLength"` // 支持的时移长度
TimeShiftURL *url.URL `json:"timeShiftURL"` // 时移地址(回放地址)
TimeShiftURL string `json:"timeShiftURL"` // 时移地址(回放地址)

GroupName string `json:"groupName"` // 程序识别的频道分类
LogoName string `json:"logoName"` // 频道台标名称
Expand All @@ -42,7 +42,7 @@ func ToM3UFormat(channels []Channel, udpxyURL, catchupSource string, multicastFi
sb.WriteString("#EXTM3U\n")
for _, channel := range channels {
// 根据指定条件,获取频道URL地址
channelURLStr, err := getChannelURLStr(channel.ChannelURLs, udpxyURL, multicastFirst)
channelURLStr, err := getChannelURLStr(channel, udpxyURL, multicastFirst)
if err != nil {
return "", err
}
Expand All @@ -63,9 +63,9 @@ func ToM3UFormat(channels []Channel, udpxyURL, catchupSource string, multicastFi
}
}
// 设置频道回看参数
if channel.TimeShift == "1" && channel.TimeShiftLength > 0 && channel.TimeShiftURL != nil {
if channel.TimeShift == "1" && channel.TimeShiftLength > 0 && multicastFirst {
m3uLineSb.WriteString(fmt.Sprintf(" catchup=\"%s\" catchup-source=\"%s\" catchup-days=\"%d\"",
"default", channel.TimeShiftURL.String()+catchupSource, int64(channel.TimeShiftLength.Hours()/24)))
"default", channel.TimeShiftURL+catchupSource, int64(channel.TimeShiftLength.Hours()/24)))
}
// 设置频道分组和名称
m3uLineSb.WriteString(fmt.Sprintf(" group-title=\"%s\",%s\n%s\n",
Expand Down Expand Up @@ -108,7 +108,7 @@ func ToTxtFormat(channels []Channel, udpxyURL string, multicastFirst bool) (stri
// 输出频道信息
for _, channel := range groupChannels {
// 根据指定条件,获取频道URL地址
channelURLStr, err := getChannelURLStr(channel.ChannelURLs, udpxyURL, multicastFirst)
channelURLStr, err := getChannelURLStr(channel, udpxyURL, multicastFirst)
if err != nil {
return "", err
}
Expand All @@ -122,26 +122,14 @@ func ToTxtFormat(channels []Channel, udpxyURL string, multicastFirst bool) (stri
}

// getChannelURLStr 根据指定条件,获取频道URL地址
func getChannelURLStr(channelURLs []url.URL, udpxyURL string, multicastFirst bool) (string, error) {
if len(channelURLs) == 0 {
func getChannelURLStr(channel Channel, udpxyURL string, multicastFirst bool) (string, error) {
if len(channel.ChannelURL) == 0 {
return "", errors.New("no channel urls found")
}

var channelURL url.URL
if len(channelURLs) == 1 {
channelURL = channelURLs[0]
if multicastFirst {
return udpxyURL + channel.ChannelURL, nil
} else {
for _, channelURL = range channelURLs {
if (multicastFirst && channelURL.Scheme == SCHEME_IGMP) ||
(!multicastFirst && channelURL.Scheme != SCHEME_IGMP) {
break
}
}
}

if udpxyURL != "" && channelURL.Scheme == SCHEME_IGMP {
return url.JoinPath(udpxyURL, fmt.Sprintf("/rtp/%s", channelURL.Host))
} else {
return channelURL.String(), nil
return channel.TimeShiftURL, nil
}
}
27 changes: 3 additions & 24 deletions internal/app/iptv/hwctc/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,21 +100,7 @@ func (c *Client) GetAllChannelList(ctx context.Context) ([]iptv.Channel, error)

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

channelURLs = append(channelURLs, *channelURL)
}

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

// TimeShiftLength类型转换
timeShiftLength, err := strconv.ParseInt(string(matches[6]), 10, 64)
Expand All @@ -124,14 +110,7 @@ func (c *Client) GetAllChannelList(ctx context.Context) ([]iptv.Channel, error)
}

// 解析时移地址
timeShiftURL, err := url.Parse(string(matches[7]))
if err != nil {
c.logger.Warn("The timeShiftURL of this channel is illegal. Use the default value: nil.", zap.String("channelName", channelName), zap.String("timeShiftURL", string(matches[7])))
}
if timeShiftURL != nil {
// 重置时移地址的查询参数
timeShiftURL.RawQuery = ""
}
timeShiftURL := string(matches[7])

// 自动识别频道的分类
groupName := iptv.GetChannelGroupName(c.chGroupRulesList, channelName)
Expand All @@ -143,7 +122,7 @@ func (c *Client) GetAllChannelList(ctx context.Context) ([]iptv.Channel, error)
ChannelID: string(matches[1]),
ChannelName: channelName,
UserChannelID: string(matches[3]),
ChannelURLs: channelURLs,
ChannelURL: channelURL,
TimeShift: string(matches[5]),
TimeShiftLength: time.Duration(timeShiftLength) * time.Minute,
TimeShiftURL: timeShiftURL,
Expand Down

0 comments on commit bda86df

Please sign in to comment.