Skip to content

Commit

Permalink
chore: change repo structure
Browse files Browse the repository at this point in the history
would probably still change it again lmao
  • Loading branch information
noornee committed May 25, 2024
1 parent e6dcff2 commit 3620c97
Show file tree
Hide file tree
Showing 10 changed files with 169 additions and 308 deletions.
34 changes: 15 additions & 19 deletions cmd/reddit-dl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"fmt"
"os"
"sync"
"time"

"github.com/noornee/reddit-dl/internal/handler"
"github.com/noornee/reddit-dl/internal/service"
"github.com/noornee/reddit-dl/internal/utility"
"github.com/noornee/reddit-dl/internal/helper"
"github.com/noornee/reddit-dl/internal/reddit"
"github.com/urfave/cli/v2"
)

Expand Down Expand Up @@ -54,32 +54,28 @@ func main() {

if err := app.Run(os.Args); err != nil {
fmt.Println()
utility.ErrorLog.Println(err)
helper.ErrorLog.Println(err)
}
}

func controller(raw_url string, useDash bool) {
url, title, err := handler.ParseUrl(raw_url)
if err != nil {
utility.ErrorLog.Fatal(err)
}
title := fmt.Sprintf("%d", time.Now().Unix())

// body -> the url response body in form of []bytes
body, err := handler.GetBody(url)
body, err := helper.GetJSONBody(raw_url)
if err != nil {
utility.ErrorLog.Fatal(err)
helper.ErrorLog.Fatal(err)
}

reddit_data, err := utility.ParseJSONBody(body, useDash)
reddit_data, err := reddit.ExtractRedditData(body, useDash)
if err != nil {
utility.ErrorLog.Fatal(err)
helper.ErrorLog.Fatal(err)
}

var wg sync.WaitGroup

if reddit_data.IsDash == true {
utility.InfoLog.Println("Downloading DASHPlaylist\n")
service.DashPlaylist(reddit_data.MediaUrl, title)
if reddit_data.IsDash {
helper.InfoLog.Println("Downloading DASHPlaylist")
helper.DownloadDashPlaylist(reddit_data.MediaUrl, title)
return
}

Expand All @@ -88,13 +84,13 @@ func controller(raw_url string, useDash bool) {
wg.Add(1)
go func(url string) {
defer wg.Done()
service.Setup(url, "", title)
helper.Download(url, "", title)
}(url)
}
wg.Wait()

} else {
media, audio := utility.GetMediaUrl(reddit_data.MediaUrl)
service.Setup(media, audio, title)
media, audio := helper.GetMediaUrl(reddit_data.MediaUrl)
helper.Download(media, audio, title)
}
}
100 changes: 0 additions & 100 deletions internal/handler/handler.go

This file was deleted.

91 changes: 0 additions & 91 deletions internal/handler/handler_test.go

This file was deleted.

36 changes: 20 additions & 16 deletions internal/service/downloader.go → internal/helper/downloader.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package service
package helper

import (
"fmt"
Expand All @@ -9,12 +9,9 @@ import (

"github.com/cavaliergopher/grab/v3"
"github.com/gookit/color"
"github.com/noornee/reddit-dl/internal/utility"
)

// create a directory temp
func createDir() string {

const temp_dir = ".reddit_temp"

// if temp_dir exists, delete
Expand All @@ -24,29 +21,25 @@ func createDir() string {

err := os.Mkdir(temp_dir, os.ModePerm)
if err != nil {
utility.ErrorLog.Println(err)
ErrorLog.Println(err)
}

return temp_dir
}

func downloaded_size(resp *grab.Response) string {

size_bytes := resp.BytesComplete()
size_kb := size_bytes / (1 << 10)
// mb := size_bytes / (1 << 20)

return color.Green.Sprintf("%dKB", size_kb)

}

func total_download_size(resp *grab.Response) string {

size_bytes := resp.Size()
size_kb := size_bytes / (1 << 10)

return color.Blue.Sprintf("%dKB", size_kb)

}

// download progress
Expand All @@ -65,7 +58,6 @@ Loop:
break Loop
}
}

}

func downloader(urls []string) {
Expand Down Expand Up @@ -93,7 +85,7 @@ func downloader(urls []string) {

// check for errors
if err := resp.Err(); err != nil {
utility.ErrorLog.Fatalf("Download failed: %v\n", err)
ErrorLog.Fatalf("Download failed: %v\n", err)
}
}(url)
}
Expand All @@ -102,7 +94,6 @@ func downloader(urls []string) {

// download files[video/gif/image](files with no sound)
func downloader_nos(url, title string) {

// create client
client := grab.NewClient()

Expand All @@ -118,21 +109,34 @@ func downloader_nos(url, title string) {

// check for errors
if err := resp.Err(); err != nil {
utility.ErrorLog.Fatalf("Download failed: %v\n", err)
ErrorLog.Fatalf("Download failed: %v\n", err)
}

if strings.Contains(resp.Filename, ".mp4") {
file_name := fmt.Sprintf("%s.mp4", title)
err := os.Rename(resp.Filename, file_name)
if err != nil {
utility.ErrorLog.Println(err)
ErrorLog.Println(err)
}

utility.InfoLog.Printf("Download saved to %v \n", file_name)
InfoLog.Printf("Download saved to %v \n", file_name)

} else {
InfoLog.Printf("Download saved to %v \n", resp.Filename)
}
}

func Download(media_url, audio_url, title string) {
if audio_url != "" {
status_code, mime := GetHead(audio_url)

if status_code == 200 && !strings.Contains(mime, "image") {
downloader([]string{media_url, audio_url})
video_merger(title)
return
}

utility.InfoLog.Printf("Download saved to %v \n", resp.Filename)
}

downloader_nos(media_url, title)
}
Loading

0 comments on commit 3620c97

Please sign in to comment.