Skip to content

Commit

Permalink
add playlist dl support
Browse files Browse the repository at this point in the history
  • Loading branch information
horsaen committed Mar 10, 2024
1 parent b513f4b commit 7e75a6c
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 3 deletions.
11 changes: 8 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:")
Expand All @@ -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":
Expand Down
8 changes: 8 additions & 0 deletions plugins/afreeca/afreeca.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ import (
"os"
)

func Playlist() {
inputPlaylists := GetPlaylists()

playlists := ParsePlaylists(inputPlaylists)

DownloadPlaylists(playlists)
}

func Start(bjId string) {
tools.Exists("downloads/Afreeca")

Expand Down
39 changes: 39 additions & 0 deletions plugins/afreeca/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"net/http"
"net/url"
"os"
"path"
"strconv"
"strings"
"time"
)
Expand Down Expand Up @@ -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)
}

}
}
52 changes: 52 additions & 0 deletions plugins/afreeca/playlist.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 7e75a6c

Please sign in to comment.