Skip to content

Commit 48dec33

Browse files
committed
add check range function
1 parent 253d351 commit 48dec33

File tree

6 files changed

+118
-144
lines changed

6 files changed

+118
-144
lines changed

checker/check.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,16 @@ import (
1111
"github.com/sirupsen/logrus"
1212
)
1313

14+
// HttpClient provides an http client with a timeout for users
1415
func HttpClient(timeout time.Duration) *http.Client {
1516
return &http.Client{
1617
Timeout: timeout * time.Second,
1718
}
1819
}
1920

21+
// FetchSource performs a get request for the given url and returns the body content
22+
// @params url: target url
23+
// @params timeout: timeout for request
2024
func FetchSource(url string, timeout time.Duration) (error, string) {
2125
client := HttpClient(timeout)
2226
resp, err := client.Get(url)
@@ -43,7 +47,10 @@ type urlChecker struct {
4347
client *http.Client
4448
}
4549

50+
// Do is method of struct urlChecker.
51+
// It performs GET request for the url and saves the result to log file.
4652
func (c *urlChecker) Do() {
53+
fmt.Println(c.url)
4754
defer func() {
4855
if err := recover(); err != nil {
4956
logrus.WithFields(logrus.Fields{
@@ -61,6 +68,7 @@ func (c *urlChecker) Do() {
6168
logrus.WithFields(logrus.Fields{
6269
"url": c.url,
6370
}).Infoln("Passed!")
71+
strChan <- c.url
6472
} else {
6573
logrus.WithFields(logrus.Fields{
6674
"url": c.url,
@@ -69,10 +77,18 @@ func (c *urlChecker) Do() {
6977
}
7078
}
7179

80+
// CheckRangeSycn checks all the urls in the range given by url
81+
// @params url: a url containing a range, through which we could get a sequence of urls within this range.
82+
// The range must be a number range.
7283
func CheckRangeSycn(url string, timeout time.Duration) {
73-
84+
// generate all the urls according to the url range
85+
urls := ParseRange(url)
86+
// make use of CheckAllSync to complete this check
87+
CheckAllSync(urls, timeout)
7488
}
7589

90+
// CheckAllSync checks all the urls simultaneously
91+
// @params urls: all the url that need to check
7692
func CheckAllSync(urls []string, timeout time.Duration) {
7793
runtime.GOMAXPROCS(runtime.NumCPU())
7894
client := HttpClient(timeout)

checker/init.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
func init() {
1111
initLog()
12+
go writeToFile()
1213
}
1314

1415
func initLog() {
@@ -30,3 +31,30 @@ func initLog() {
3031
})
3132
logrus.SetOutput(logfile)
3233
}
34+
35+
var strChan chan string
36+
37+
func writeToFile() {
38+
strChan = make(chan string)
39+
// create m3u file
40+
m3upath, err := os.Getwd()
41+
if err != nil {
42+
logrus.Fatalln(err)
43+
}
44+
m3uname := "iptv.m3u"
45+
path := filepath.Join(m3upath, m3uname)
46+
m3ufile, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0666)
47+
if err != nil {
48+
logrus.Fatalln(err)
49+
}
50+
for str := range strChan {
51+
_, err = m3ufile.WriteString(str + "\n")
52+
if err != nil {
53+
logrus.Fatalln(err)
54+
}
55+
}
56+
err = m3ufile.Close()
57+
if err != nil {
58+
logrus.Fatalln(err)
59+
}
60+
}

checker/parser.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ func ParseRange(url string) []string {
5454
}
5555
res := reg.FindAllStringSubmatch(url, -1)
5656
if res == nil {
57-
return nil
57+
fmt.Println("No matches found: ", url)
58+
logrus.Fatalln("No matches found: ", url)
5859
}
5960
prefix := res[0][reg.SubexpIndex("prefix")]
6061
suffix := res[0][reg.SubexpIndex("suffix")]

0 commit comments

Comments
 (0)