Skip to content

Commit

Permalink
add flag
Browse files Browse the repository at this point in the history
  • Loading branch information
KyonLi committed Jan 31, 2019
1 parent 5442ba0 commit b00ccd0
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 26 deletions.
26 changes: 12 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
# 移动魔百盒直播源转m3u8播放列表
# 移动魔百盒直播源生成m3u8播放列表

## 使用方法

### 1. 获取频道列表接口地址和基础URL

参照 [@zhantong](https://github.com/zhantong) 的文章 [苏州移动IPTV抓包](https://www.polarxiong.com/archives/苏州移动IPTV抓包.html)

### 2. 修改 `main.go` 中的 `CHANNEL_API_URL``BASE_URL`

### 3. 编译运行

`go build`

`./ottcn2m3u8`

会在当前目录下生成 channel.m3u8
参照 [@zhantong](https://github.com/zhantong) 的文章 [苏州移动IPTV抓包](https://www.polarxiong.com/archives/苏州移动IPTV抓包.html) 获取频道列表接口地址和基础URL

Usage of ottcn2m3u8:
-api string
API URL to fetch channel list. (default "http://looktvepg.jsa.bcs.ottcn.com:8080/ysten-lvoms-epg/epg/getChannelIndexs.shtml?deviceGroupId=1697")
-base string
Base URL for stream. (default "http://183.207.248.71:80/cntv/live1")
-h This help.
-o string
Output file path. (default "channel.m3u8")
-v Verbose mode.
43 changes: 31 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"encoding/json"
"flag"
"fmt"
"io"
"log"
Expand All @@ -13,8 +14,13 @@ import (
"strconv"
)

var CHANNEL_API_URL = "http://looktvepg.jsa.bcs.ottcn.com:8080/ysten-lvoms-epg/epg/getChannelIndexs.shtml?deviceGroupId=1697"
var BASE_URL = "http://183.207.248.71:80/cntv/live1"
var (
help = flag.Bool("h", false, "This help.")
verbose = flag.Bool("v", false, "Verbose mode.")
baseURL = flag.String("base", "http://183.207.248.71:80/cntv/live1", "Base URL for stream.")
apiURL = flag.String("api", "http://looktvepg.jsa.bcs.ottcn.com:8080/ysten-lvoms-epg/epg/getChannelIndexs.shtml?deviceGroupId=1697", "API URL to fetch channel list.")
outputFile = flag.String("o", "channel.m3u8", "Output file path.")
)

type Channel struct {
UUID string `json:"uuid"`
Expand All @@ -23,12 +29,14 @@ type Channel struct {
}

func (c *Channel) toString() string {
u, _ := url.Parse(BASE_URL + "/" + c.ChannelName + "/" + c.UUID)
u, _ := url.Parse(*baseURL + "/" + c.ChannelName + "/" + c.UUID)
return fmt.Sprintf("#EXTINF:-1,%s\n%s\n", c.ChannelName, u.String())
}

func getJSONContent() map[string]Channel {
resp, err := http.Get(CHANNEL_API_URL)
fmt.Println("Fetching channel list...")

resp, err := http.Get(*apiURL)
if err != nil {
log.Fatalln(err)
}
Expand All @@ -46,6 +54,8 @@ func getJSONContent() map[string]Channel {
}

func generateChannelList(json *map[string]Channel) []Channel {
fmt.Println("Parsing...")

keySort := make(map[int]string, len(*json))
for key := range *json {
number := stringArrayToString(regexp.MustCompile("[0-9]").FindAllStringSubmatch(key, -1))
Expand Down Expand Up @@ -82,13 +92,17 @@ func stringArrayToString(array [][]string) string {
}

func generateM3U8(channels *[]Channel) {
fmt.Println("Generating m3u8...")

content := fmt.Sprintf("#EXTM3U\n\n")
for _, c := range *channels {
content += c.toString()
fmt.Println(c.ChannelName)
if *verbose {
fmt.Println(c.ChannelName)
}
}

f, err := os.Create("channel.m3u8")
f, err := os.Create(*outputFile)
if err != nil {
log.Fatalln(err)
}
Expand All @@ -98,14 +112,19 @@ func generateM3U8(channels *[]Channel) {
if err != nil {
log.Fatalln(err)
}

fmt.Printf("Done, all saved to %s\n", *outputFile)
}

func main() {
fmt.Println("Fetching channel list...")
flag.Parse()

if *help {
flag.Usage()
return
}

j := getJSONContent()
fmt.Println("Parsing...")
list := generateChannelList(&j)
fmt.Println("Generating m3u8...")
generateM3U8(&list)
fmt.Println("Done, all saved to channel.m3u8")
l := generateChannelList(&j)
generateM3U8(&l)
}

0 comments on commit b00ccd0

Please sign in to comment.