diff --git a/main.go b/main.go index 1fa1429..b831131 100644 --- a/main.go +++ b/main.go @@ -20,17 +20,18 @@ func main() { userArg := flag.String("username", "", "Username of the streamer") mode := flag.String("mode", "afreeca", "Mode") + playlist := flag.Bool("playlist", false, "Download playlist") version := flag.Bool("version", false, "Print version") flag.Parse() if *version { fmt.Println("https://github.com/horsaen/afreeca-downloader") - fmt.Println("v2.0.0") + fmt.Println("v2.0.1") os.Exit(0) } - if *userArg != "" { + if *userArg != "" || *playlist { username = *userArg } else { fmt.Println("Enter username:") @@ -41,7 +42,11 @@ func main() { switch *mode { case "afreeca": - afreeca.Start(username) + if *playlist { + afreeca.Playlist() + } else { + afreeca.Start(username) + } case "bigo": bigo.Start(username) case "flex": diff --git a/plugins/afreeca/afreeca.go b/plugins/afreeca/afreeca.go index 654fc9e..ef40709 100644 --- a/plugins/afreeca/afreeca.go +++ b/plugins/afreeca/afreeca.go @@ -6,6 +6,14 @@ import ( "os" ) +func Playlist() { + inputPlaylists := GetPlaylists() + + playlists := ParsePlaylists(inputPlaylists) + + DownloadPlaylists(playlists) +} + func Start(bjId string) { tools.Exists("downloads/Afreeca") diff --git a/plugins/afreeca/download.go b/plugins/afreeca/download.go index 0801726..d1135a7 100644 --- a/plugins/afreeca/download.go +++ b/plugins/afreeca/download.go @@ -9,6 +9,8 @@ import ( "net/http" "net/url" "os" + "path" + "strconv" "strings" "time" ) @@ -97,3 +99,40 @@ func Download(bjId string, nickname string, playlist string) bool { time.Sleep(3 * time.Second) } } + +func DownloadPlaylists(playlists []string) { + now := time.Now().Format("200601021504") + tools.Exists("downloads/Afreeca/" + now) + + vodBase := "https://vod-archive-kr-cdn-z01.afreecatv.com" + length := len(playlists) + + for i, playlist := range playlists { + var downloaded = 0 + segments := []string{} + + parseUrl, _ := url.Parse(playlist) + resp, _ := http.Get(playlist) + + scanner := bufio.NewScanner(resp.Body) + + for scanner.Scan() { + line := scanner.Text() + if strings.HasSuffix(line, ".ts") { + segments = append(segments, vodBase+path.Dir(parseUrl.Path)+"/"+line) + } + } + + filename := "downloads/Afreeca/" + now + "/" + strconv.Itoa(i+1) + ".ts" + out, _ := os.Create(filename) + + for _, segment := range segments { + resp, _ := http.Get(segment) + downloaded += 1 + output := fmt.Sprintf("\rDownloaded %d segments out of %d || Playlist %d of %d", downloaded, len(segments), i+1, length) + fmt.Print(output) + io.Copy(out, resp.Body) + } + + } +} diff --git a/plugins/afreeca/playlist.go b/plugins/afreeca/playlist.go new file mode 100644 index 0000000..d849ca6 --- /dev/null +++ b/plugins/afreeca/playlist.go @@ -0,0 +1,52 @@ +package afreeca + +import ( + "bufio" + "fmt" + "net/http" + "os" + "strings" +) + +func GetPlaylists() []string { + inputPlaylist := []string{} + + fmt.Println("Paste playlist URLs:") + + scn := bufio.NewScanner(os.Stdin) + for { + scn.Scan() + userInput := scn.Text() + + if userInput == "" { + return inputPlaylist + } else { + inputPlaylist = append(inputPlaylist, userInput) + } + } +} + +func ParsePlaylists(streams []string) []string { + vodBase := "https://vod-archive-kr-cdn-z01.afreecatv.com" + + segments := []string{} + + for _, playlist := range streams { + tempSegs := []string{} + + resp, _ := http.Get(playlist) + + scanner := bufio.NewScanner(resp.Body) + + for scanner.Scan() { + line := scanner.Text() + if strings.HasSuffix(line, ".m3u8") { + tempSegs = append(tempSegs, line) + } + } + + segments = append(segments, vodBase+tempSegs[len(tempSegs)-1]) + } + + return segments +}