Skip to content

Commit

Permalink
add afreeca dl vod support
Browse files Browse the repository at this point in the history
  • Loading branch information
horsaen committed Mar 15, 2024
1 parent 7e75a6c commit 11c6d76
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 6 deletions.
13 changes: 10 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,21 @@ func main() {

var username string

userArg := flag.String("username", "", "Username of the streamer")
mode := flag.String("mode", "afreeca", "Mode")
userArg := flag.String("username", "", "Streamer username")
playlist := flag.Bool("playlist", false, "Download playlist")
vod := flag.Bool("vod", false, "Download vod")
version := flag.Bool("version", false, "Print version")

flag.Parse()

if *version {
fmt.Println("https://github.com/horsaen/afreeca-downloader")
fmt.Println("v2.0.1")
fmt.Println("v2.0.2")
os.Exit(0)
}

if *userArg != "" || *playlist {
if *userArg != "" || *playlist || *vod {
username = *userArg
} else {
fmt.Println("Enter username:")
Expand All @@ -42,8 +43,14 @@ func main() {

switch *mode {
case "afreeca":
tools.Exists("downloads/Afreeca")
if *playlist {
afreeca.Playlist()
} else if *vod {
var TitleNo string
fmt.Println("Enter title number:")
fmt.Scan(&TitleNo)
afreeca.Vod(TitleNo)
} else {
afreeca.Start(username)
}
Expand Down
12 changes: 10 additions & 2 deletions plugins/afreeca/afreeca.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ func Playlist() {
}

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

if !CheckExists(bjId) {
fmt.Printf("User %s not found.\n", bjId)
os.Exit(1)
Expand All @@ -30,3 +28,13 @@ func Start(bjId string) {
Download(bjId, nickname, url)
}
}

func Vod(TitleNo string) {
tools.Exists("downloads/Afreeca/Vods")

sourceFiles := GetVodFiles(TitleNo)

files := ParseVods(sourceFiles)

DownloadVods(TitleNo, files)
}
41 changes: 40 additions & 1 deletion plugins/afreeca/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ type Master struct {
} `json:"CHANNEL"`
}

type VodStruct struct {
Data struct {
BjId string `json:"bj_id"`
Files []struct {
File string `json:"file"`
}
} `json:"data"`
}

func UserData(bjId string) (string, int) {
url := "https://bjapi.afreecatv.com/api/" + bjId + "/station"

Expand Down Expand Up @@ -173,7 +182,6 @@ func StreamList(baseUrl string, masterUrl string) string {
}

return playlists[0]

}

func GetStream(bjId string, broad_no int, password string) string {
Expand All @@ -182,5 +190,36 @@ func GetStream(bjId string, broad_no int, password string) string {
master := MasterPlaylist(bjId, broad_no, "")

return base + "/" + StreamList(base, master)
}

func GetVodFiles(titleNo string) []string {
url := "https://api.m.afreecatv.com/station/video/a/view"

payload := strings.NewReader("nTitleNo=" + titleNo + "&nApiLevel=10&nPlaylistIdx=0")

client := &http.Client{}

req, _ := http.NewRequest("POST", url, payload)

req.Header.Add("Content-Type", "application/x-www-form-urlencoded")

resp, err := client.Do(req)

if err != nil {
log.Fatal(err)
}

bodyText, _ := io.ReadAll(resp.Body)

var vod VodStruct

json.Unmarshal(bodyText, &vod)

var files []string

for _, file := range vod.Data.Files {
files = append(files, file.File)
}

return files
}
40 changes: 40 additions & 0 deletions plugins/afreeca/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,43 @@ func DownloadPlaylists(playlists []string) {

}
}

func DownloadVods(TitleNo string, files []string) {
tools.Exists("downloads/Afreeca/Vods/" + TitleNo)

vodBase := "https://vod-archive-global-cdn-z02.afreecatv.com"
length := len(files)

for i, file := range files {
var downloaded = 0
segments := []string{}

parseUrl, _ := url.Parse(file)
resp, _ := http.Get(file)

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/Vods/" + TitleNo + "/" + strconv.Itoa(i+1) + ".ts"
out, err := os.Create(filename)

if err != nil {
log.Fatal(err)
}

for _, segment := range segments {
resp, _ := http.Get(segment)
downloaded += 1
output := fmt.Sprintf("\rDownloaded %d segments out of %d || File %d of %d", downloaded, len(segments), i+1, length)
fmt.Print(output)
io.Copy(out, resp.Body)
}
}

}
32 changes: 32 additions & 0 deletions plugins/afreeca/vod.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package afreeca

import (
"bufio"
"net/http"
"strings"
)

func ParseVods(files []string) []string {
vodBase := "https://vod-archive-global-cdn-z02.afreecatv.com"

fileArr := []string{}

for _, file := range files {
qualities := []string{}

resp, _ := http.Get(file)

scanner := bufio.NewScanner(resp.Body)

for scanner.Scan() {
line := scanner.Text()
if strings.HasSuffix(line, ".m3u8") {
qualities = append(qualities, line)
}
}

fileArr = append(fileArr, vodBase+qualities[len(qualities)-1])
}

return fileArr
}

0 comments on commit 11c6d76

Please sign in to comment.