Skip to content

Commit

Permalink
add check range function
Browse files Browse the repository at this point in the history
  • Loading branch information
swahpy committed May 14, 2022
1 parent 253d351 commit 48dec33
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 144 deletions.
18 changes: 17 additions & 1 deletion checker/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@ import (
"github.com/sirupsen/logrus"
)

// HttpClient provides an http client with a timeout for users
func HttpClient(timeout time.Duration) *http.Client {
return &http.Client{
Timeout: timeout * time.Second,
}
}

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

// Do is method of struct urlChecker.
// It performs GET request for the url and saves the result to log file.
func (c *urlChecker) Do() {
fmt.Println(c.url)
defer func() {
if err := recover(); err != nil {
logrus.WithFields(logrus.Fields{
Expand All @@ -61,6 +68,7 @@ func (c *urlChecker) Do() {
logrus.WithFields(logrus.Fields{
"url": c.url,
}).Infoln("Passed!")
strChan <- c.url
} else {
logrus.WithFields(logrus.Fields{
"url": c.url,
Expand All @@ -69,10 +77,18 @@ func (c *urlChecker) Do() {
}
}

// CheckRangeSycn checks all the urls in the range given by url
// @params url: a url containing a range, through which we could get a sequence of urls within this range.
// The range must be a number range.
func CheckRangeSycn(url string, timeout time.Duration) {

// generate all the urls according to the url range
urls := ParseRange(url)
// make use of CheckAllSync to complete this check
CheckAllSync(urls, timeout)
}

// CheckAllSync checks all the urls simultaneously
// @params urls: all the url that need to check
func CheckAllSync(urls []string, timeout time.Duration) {
runtime.GOMAXPROCS(runtime.NumCPU())
client := HttpClient(timeout)
Expand Down
28 changes: 28 additions & 0 deletions checker/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

func init() {
initLog()
go writeToFile()
}

func initLog() {
Expand All @@ -30,3 +31,30 @@ func initLog() {
})
logrus.SetOutput(logfile)
}

var strChan chan string

func writeToFile() {
strChan = make(chan string)
// create m3u file
m3upath, err := os.Getwd()
if err != nil {
logrus.Fatalln(err)
}
m3uname := "iptv.m3u"
path := filepath.Join(m3upath, m3uname)
m3ufile, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0666)
if err != nil {
logrus.Fatalln(err)
}
for str := range strChan {
_, err = m3ufile.WriteString(str + "\n")
if err != nil {
logrus.Fatalln(err)
}
}
err = m3ufile.Close()
if err != nil {
logrus.Fatalln(err)
}
}
3 changes: 2 additions & 1 deletion checker/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ func ParseRange(url string) []string {
}
res := reg.FindAllStringSubmatch(url, -1)
if res == nil {
return nil
fmt.Println("No matches found: ", url)
logrus.Fatalln("No matches found: ", url)
}
prefix := res[0][reg.SubexpIndex("prefix")]
suffix := res[0][reg.SubexpIndex("suffix")]
Expand Down
Loading

0 comments on commit 48dec33

Please sign in to comment.