Skip to content

Commit

Permalink
restructure project
Browse files Browse the repository at this point in the history
  • Loading branch information
TiltedToast committed Aug 16, 2023
1 parent c7704b6 commit fe14ee5
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 67 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@
go.work
.env
.idea
.vscode
.vscode
output
21 changes: 14 additions & 7 deletions cmd/danbooru-go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,27 @@ import (
"os"
"path/filepath"
"runtime"
"slices"
"sync"

"github.com/joho/godotenv"
pb "github.com/schollz/progressbar/v3"
"github.com/valyala/fasthttp"

. "github.com/tiltedtoast/danbooru-go/internal"
. "github.com/tiltedtoast/danbooru-go/internal/logger"
. "github.com/tiltedtoast/danbooru-go/internal/options"
. "github.com/tiltedtoast/danbooru-go/internal/models"
)

var (
opts = GetOptions()
logger = GetLogger()
)

func main() {
args := os.Args[1:]

logger := NewLogger()

exe, err := os.Executable()
if err != nil {
logger.Fatal("Error getting executable path")
Expand All @@ -33,18 +40,18 @@ func main() {
}
}

if Contains(args, "-h") || Contains(args, "--help") || len(args) == 0 {
if slices.Contains(args, "-h") || slices.Contains(args, "--help") || len(args) == 0 {
PrintHelpMessage()
return
}

logger.Trace(fmt.Sprintf("Arguments: %v", OPTIONS))
logger.Trace(fmt.Sprintf("Arguments: %v", opts))

if len(OPTIONS.Tags) == 0 {
if len(opts.Tags) == 0 {
logger.Fatal("No tags provided")
}

totalPages := GetTotalPages(OPTIONS.Tags)
totalPages := GetTotalPages(opts.Tags)

logger.Trace(fmt.Sprintf("Total pages: %d", totalPages))

Expand All @@ -61,7 +68,7 @@ func main() {

logger.Trace(fmt.Sprintf("Total posts: %d", len(posts)))

newpath := filepath.Join(".", OPTIONS.OutputDir)
newpath := filepath.Join(".", opts.OutputDir)
if err := os.MkdirAll(newpath, os.ModePerm); err != nil {
logger.Fatal("Error creating directory, exiting")
}
Expand Down
30 changes: 17 additions & 13 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,38 +1,42 @@
module github.com/tiltedtoast/danbooru-go

go 1.19
go 1.21

require (
github.com/PuerkitoBio/goquery v1.8.1
go.uber.org/ratelimit v0.2.0
go.uber.org/ratelimit v0.3.0
)

require github.com/joho/godotenv v1.5.1
require (
github.com/joho/godotenv v1.5.1
github.com/x-cray/logrus-prefixed-formatter v0.5.2
)

require (
github.com/benbjohnson/clock v1.3.5 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect
github.com/x-cray/logrus-prefixed-formatter v0.5.2 // indirect
golang.org/x/crypto v0.7.0 // indirect
github.com/onsi/ginkgo v1.16.5 // indirect
github.com/onsi/gomega v1.27.10 // indirect
golang.org/x/crypto v0.12.0 // indirect
)

require (
github.com/andybalholm/brotli v1.0.5 // indirect
github.com/klauspost/compress v1.16.5 // indirect
github.com/klauspost/compress v1.16.7 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.46.0
github.com/valyala/fasthttp v1.48.0
)

require (
github.com/andres-erbsen/clock v0.0.0-20160526145045-9e14626cd129 // indirect
github.com/andybalholm/cascadia v1.3.2 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/schollz/progressbar/v3 v3.13.1
github.com/sirupsen/logrus v1.9.3
golang.org/x/net v0.9.0 // indirect
golang.org/x/sys v0.7.0 // indirect
golang.org/x/term v0.7.0 // indirect
golang.org/x/net v0.14.0 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/term v0.11.0 // indirect
)
106 changes: 88 additions & 18 deletions go.sum

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions internal/logger.go → internal/logger/logger.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package internal
package logger

import (
"os"
"slices"

log "github.com/sirupsen/logrus"
prefixed "github.com/x-cray/logrus-prefixed-formatter"
)

func NewLogger() *log.Logger {
func GetLogger() *log.Logger {
return &Logger
}

Expand All @@ -23,9 +24,9 @@ var Logger = log.Logger{
}

func determine_level(args []string) log.Level {
if Contains(args, "--debug") {
if slices.Contains(args, "--debug") {
return log.DebugLevel
} else if Contains(args, "--verbose") || Contains(args, "-v") {
} else if slices.Contains(args, "--verbose") || slices.Contains(args, "-v") {
return log.TraceLevel
} else {
return log.ErrorLevel
Expand Down
15 changes: 10 additions & 5 deletions internal/post.go → internal/models/post.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package internal
package types

import (
"bufio"
Expand All @@ -8,6 +8,8 @@ import (
"strconv"
"strings"

. "github.com/tiltedtoast/danbooru-go/internal/logger"
. "github.com/tiltedtoast/danbooru-go/internal/options"
"github.com/valyala/fasthttp"
)

Expand All @@ -20,7 +22,10 @@ type Post struct {
LargeFileURL string `json:"large_file_url"`
}

var logger = NewLogger()
var (
logger = GetLogger()
opts = GetOptions()
)

// Download a post and saves it to a subfolder based on its rating
func (post *Post) Download(client *fasthttp.Client) {
Expand Down Expand Up @@ -55,16 +60,16 @@ func (post *Post) Download(client *fasthttp.Client) {
}

// Create subfolder if it doesn't exist
if _, err := os.Stat(fmt.Sprint("./" + OPTIONS.OutputDir + subfolder)); os.IsNotExist(err) {
newpath := filepath.Join(OPTIONS.OutputDir, subfolder)
if _, err := os.Stat(fmt.Sprint("./" + opts.OutputDir + subfolder)); os.IsNotExist(err) {
newpath := filepath.Join(opts.OutputDir, subfolder)
if err := os.MkdirAll(newpath, os.ModePerm); err != nil {
logger.Warn(fmt.Sprintf("Error creating subfolder: %v", err))
return
}
}

filename := strconv.Itoa(post.Score) + "_" + strconv.Itoa(post.ID) + "." + post.FileExt
filename = filepath.Join(fmt.Sprint(OPTIONS.OutputDir+subfolder), filename)
filename = filepath.Join(fmt.Sprint(opts.OutputDir+subfolder), filename)

if _, err := os.Stat(filename); err == nil {
logger.Trace(fmt.Sprintf("File already exists: %s", filename))
Expand Down
2 changes: 1 addition & 1 deletion internal/user.go → internal/models/user.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package internal
package types

type User struct {
LastLoggedInAt string `json:"last_logged_in_at"`
Expand Down
10 changes: 7 additions & 3 deletions internal/args.go → internal/options/args.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package internal
package options

import (
"os"
Expand All @@ -14,10 +14,10 @@ type Args struct {
General bool
}

var OPTIONS = NewArgs()
var args = parseArgs()

// Parses the command line arguments and returns an Args struct
func NewArgs() Args {
func parseArgs() Args {
args := os.Args[1:]

options := Args{
Expand Down Expand Up @@ -58,3 +58,7 @@ func NewArgs() Args {

return options
}

func GetOptions() *Args {
return &args
}
25 changes: 10 additions & 15 deletions internal/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,18 @@ import (
"github.com/valyala/fasthttp"
"go.uber.org/ratelimit"

. "github.com/tiltedtoast/danbooru-go/internal/logger"
. "github.com/tiltedtoast/danbooru-go/internal/options"
. "github.com/tiltedtoast/danbooru-go/internal/models"
)

var (
BASE_URL = "https://danbooru.donmai.us"
POSTS_PER_PAGE = 200
LOGIN_NAME = os.Getenv("DANBOORU_LOGIN")
API_KEY = os.Getenv("DANBOORU_API_KEY")
logger = GetLogger()
opts = GetOptions()
)

// Loops over all pages and returns a list of all posts
Expand Down Expand Up @@ -63,7 +68,7 @@ func FetchPostsFromPage(totalPageAmount int, client *fasthttp.Client) []Post {
defer wg.Done()
rl.Take()
tagString := ""
for _, tag := range OPTIONS.Tags {
for _, tag := range opts.Tags {
tagString += url.QueryEscape(tag) + "+"
}

Expand Down Expand Up @@ -95,10 +100,10 @@ func FetchPostsFromPage(totalPageAmount int, client *fasthttp.Client) []Post {

// User can exclude ratings via CLI flags
for _, post := range result {
if post.Rating == "s" && !OPTIONS.Sensitive ||
post.Rating == "q" && !OPTIONS.Questionable ||
post.Rating == "e" && !OPTIONS.Explicit ||
post.Rating == "g" && !OPTIONS.General {
if post.Rating == "s" && !opts.Sensitive ||
post.Rating == "q" && !opts.Questionable ||
post.Rating == "e" && !opts.Explicit ||
post.Rating == "g" && !opts.General {
continue
}
posts = append(posts, post)
Expand Down Expand Up @@ -186,16 +191,6 @@ func IsGoldMember() bool {
return userJson.LevelString != "Member"
}

// Returns true if the given string is inside the slice
func Contains[T comparable](slice []T, element T) bool {
for _, a := range slice {
if a == element {
return true
}
}
return false
}

func PrintHelpMessage() {
fmt.Println("Usage:")
fmt.Println(" danbooru-go [options]")
Expand Down

0 comments on commit fe14ee5

Please sign in to comment.