Skip to content

Commit

Permalink
add concurrency
Browse files Browse the repository at this point in the history
  • Loading branch information
horsaen committed Mar 20, 2024
1 parent 31064d2 commit 2dca979
Show file tree
Hide file tree
Showing 22 changed files with 718 additions and 64 deletions.
12 changes: 11 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,14 @@ module horsaen/afreeca-downloader

go 1.22.0

require github.com/andybalholm/brotli v1.1.0
require (
github.com/andybalholm/brotli v1.1.0
github.com/fatih/color v1.16.0
github.com/rodaine/table v1.1.1
)

require (
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
golang.org/x/sys v0.14.0 // indirect
)
28 changes: 28 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,30 @@
github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M=
github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rodaine/table v1.1.1 h1:zBliy3b4Oj6JRmncse2Z85WmoQvDrXOYuy0JXCt8Qz8=
github.com/rodaine/table v1.1.1/go.mod h1:iqTRptjn+EVcrVBYtNMlJ2wrJZa3MpULUmcXFpfcziA=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
20 changes: 12 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"horsaen/afreeca-downloader/plugins/afreeca"
"horsaen/afreeca-downloader/plugins/bigo"
"horsaen/afreeca-downloader/plugins/concurrent"
"horsaen/afreeca-downloader/plugins/flex"
"horsaen/afreeca-downloader/plugins/kick"
"horsaen/afreeca-downloader/plugins/panda"
Expand All @@ -20,27 +21,30 @@ func main() {

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")
playlist := flag.Bool("playlist", false, "Download bot playlists")
concurrently := flag.Bool("concurrent", false, "Download streams concurrently")
vod := flag.Bool("vod", false, "Download Afreeca vod")
version := flag.Bool("version", false, "Print version")

flag.Parse()

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

if *userArg != "" || *playlist || *vod {
tools.Exists("downloads")

if *concurrently {
concurrent.Start()
}

if *userArg != "" || *playlist || *vod || *concurrently {
username = *userArg
} else {
fmt.Println("Enter username:")
fmt.Scan(&username)
}

tools.Exists("downloads")

switch *mode {
case "afreeca":
tools.Exists("downloads/Afreeca")
Expand Down
30 changes: 25 additions & 5 deletions plugins/afreeca/afreeca.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,26 @@ import (
"fmt"
"horsaen/afreeca-downloader/tools"
"os"
"strconv"
)

func Playlist() {
inputPlaylists := GetPlaylists()
func Concurrent(user *[]string) {
tools.Exists("downloads/Afreeca")

playlists := ParsePlaylists(inputPlaylists)
if !CheckExists((*user)[0]) {
(*user)[2] = "Not found"
(*user)[3] = "Not found"
(*user)[4] = "Not found"
return
}

DownloadPlaylists(playlists)
if ConcurrentCheck((*user)[0]) {
nickname, broad_no := UserData((*user)[0])

url := GetStream((*user)[0], broad_no, "")

ConcurrentDownload(user, nickname, strconv.Itoa(broad_no), url)
}
}

func Start(bjId string) {
Expand All @@ -25,10 +37,18 @@ func Start(bjId string) {

url := GetStream(bjId, broad_no, "")

Download(bjId, nickname, url)
Download(bjId, nickname, strconv.Itoa(broad_no), url)
}
}

func Playlist() {
inputPlaylists := GetPlaylists()

playlists := ParsePlaylists(inputPlaylists)

DownloadPlaylists(playlists)
}

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

Expand Down
96 changes: 94 additions & 2 deletions plugins/afreeca/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,99 @@ import (
"time"
)

func Download(bjId string, nickname string, playlist string) bool {
func ConcurrentDownload(user *[]string, nickname string, broad_no string, playlist string) {
bjId := (*user)[0]
tools.Exists("downloads/Afreeca/" + bjId)

client := &http.Client{}

req, _ := http.NewRequest("GET", playlist, nil)

parsedUrl, _ := url.Parse(playlist)

pathSegments := strings.Split(parsedUrl.Path, "/")

newPath := strings.Join(pathSegments[:len(pathSegments)-1], "/")

filename := nickname + "-" + bjId + "-" + broad_no + "-" + time.Now().Format("200601021504") + "-afreeca.ts"

out, _ := os.Create("downloads/Afreeca/" + bjId + "/" + filename)

playlistUrls := make(map[string]bool)

var bytes int64 = 0
var start_time = time.Now()

for {
resp, err := client.Do(req)

if err != nil {
(*user)[2] = "error"
(*user)[3] = "error"
(*user)[4] = err.Error()
return
}

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

bodyText := string(bodyBytes)

if !strings.Contains(bodyText, ".TS") {
(*user)[2] = "Offline"
(*user)[3] = "Offline"
(*user)[4] = "Offline"
Concurrent(user)
}

scanner := bufio.NewScanner(strings.NewReader(bodyText))

for scanner.Scan() {
line := scanner.Text()
if strings.HasSuffix(line, ".TS") {

playlistUrl := fmt.Sprintf("%s://%s%s/%s", parsedUrl.Scheme, parsedUrl.Host, newPath, line)

if !playlistUrls[playlistUrl] {

resp, err := http.Get(playlistUrl)

if err != nil {
(*user)[2] = "error"
(*user)[3] = "error"
(*user)[4] = err.Error()
return
}

bytes += resp.ContentLength
elapsed_time := time.Since(start_time)

(*user)[2] = tools.FormatBytes(bytes)
(*user)[3] = tools.FormatTime(elapsed_time)
(*user)[4] = filename

_, err = io.Copy(out, resp.Body)

if err != nil {
(*user)[2] = "error"
(*user)[3] = "error"
(*user)[4] = err.Error()
return
}

playlistUrls[playlistUrl] = true
}
}
}

if err := scanner.Err(); err != nil {
fmt.Println(err)
}

time.Sleep(3 * time.Second)
}
}

func Download(bjId string, nickname string, broad_no string, playlist string) bool {
tools.Exists("downloads/Afreeca/" + bjId)

client := &http.Client{}
Expand All @@ -32,7 +124,7 @@ func Download(bjId string, nickname string, playlist string) bool {

newPath := strings.Join(pathSegments[:len(pathSegments)-1], "/")

filename := nickname + "-" + bjId + "-" + time.Now().Format("200601021504") + "-afreeca.ts"
filename := nickname + "-" + bjId + "-" + broad_no + "-" + time.Now().Format("200601021504") + "-afreeca.ts"

out, _ := os.Create("downloads/Afreeca/" + bjId + "/" + filename)

Expand Down
9 changes: 9 additions & 0 deletions plugins/afreeca/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ func CheckExists(bjId string) bool {
}
}

func ConcurrentCheck(bjId string) bool {
for {
if CheckOnline(bjId) {
return true
}
time.Sleep(3 * time.Minute)
}
}

func DvrCheck(bjId string) bool {
for {
if CheckOnline(bjId) {
Expand Down
16 changes: 16 additions & 0 deletions plugins/bigo/bigo.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,22 @@ import (
"os"
)

func Concurrent(user *[]string) {
tools.Exists("downloads/Bigo")

if !CheckExists((*user)[0]) {
(*user)[2] = "Not found"
(*user)[3] = "Not found"
(*user)[4] = "Not found"
return
}

if ConcurrentCheck((*user)[0]) {
_, nickname, url := GetStreamData((*user)[0])
ConcurrentDownload(user, nickname, url)
}
}

func Start(userId string) {
tools.Exists("downloads/Bigo")

Expand Down
Loading

0 comments on commit 2dca979

Please sign in to comment.