-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit e8f8170
Showing
1 changed file
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"flag" | ||
"fmt" | ||
"io/ioutil" | ||
"log" | ||
"net/http" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
var Provider string | ||
|
||
type TVChannel struct { | ||
Name string `json:"name"` | ||
Provider string `json:"provider"` | ||
Url string `json:"url"` | ||
Source string `json:"source"` | ||
} | ||
|
||
func getM3U(url string) ([]byte, error) { | ||
resp, err := http.Get(url) | ||
if err != nil { | ||
return nil, err | ||
} | ||
data, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if err := resp.Body.Close(); err != nil { | ||
return nil, err | ||
} | ||
return data, nil | ||
} | ||
|
||
func convertToStruct(m3u []byte) ([]TVChannel, error){ | ||
var TvChannels []TVChannel | ||
|
||
re := regexp.MustCompile("#EXTINF.+,([^\\n]+)\\r\\n.*\\n(http://.+)\\r") | ||
for _, items := range re.FindAllSubmatch(m3u, -1) { | ||
source := string(items[2]) | ||
url := strings.Split(source, "/") | ||
channel := TVChannel{ | ||
string(items[1]), | ||
Provider, | ||
"/" + url[len(url)-2], | ||
source, | ||
} | ||
TvChannels = append(TvChannels, channel) | ||
} | ||
if len(TvChannels) == 0 { | ||
return nil, errors.New("not found channels in target") | ||
} | ||
return TvChannels, nil | ||
} | ||
|
||
func init() { | ||
flag.StringVar(&Provider, "p", "Edem", "Provider name") | ||
} | ||
|
||
func main() { | ||
flag.Parse() | ||
|
||
if len(flag.Args()) < 1 { | ||
flag.Usage() | ||
log.Fatalln("Url should be specifically") | ||
} | ||
m3u, err := getM3U(flag.Args()[len(flag.Args())-1]) | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
channels, err := convertToStruct(m3u) | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
data, err := json.MarshalIndent(channels, "", " ") | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
fmt.Println(string(data)) | ||
} |