Skip to content

Commit

Permalink
fix env variables for login
Browse files Browse the repository at this point in the history
  • Loading branch information
TiltedToast committed Oct 30, 2022
1 parent 9148fe6 commit d0c82ff
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,16 @@ type Post struct {

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

exe, err := os.Executable()
if err != nil {
fmt.Println("Error getting executable path")
return
}

exePath := filepath.Dir(exe)

godotenv.Load(fmt.Sprintf("%s/.env", exePath))

if contains(args, "-h") || contains(args, "--help") || len(args) == 0 {
printHelpMessage()
Expand All @@ -62,8 +71,8 @@ func main() {
posts := fetchPostsFromPage(options.tags, totalPages, options)

newpath := filepath.Join(".", options.outputDir)
err := os.MkdirAll(newpath, os.ModePerm)
if err != nil {
mkdirErr := os.MkdirAll(newpath, os.ModePerm)
if mkdirErr != nil {
fmt.Println("Error creating directory, exiting")
return
}
Expand Down Expand Up @@ -208,7 +217,10 @@ func fetchPostsFromPage(tags []string, totalPageAmount int, options inputOptions
currentPage, tagString)

// Credentials to get access to extra features for Danbooru Gold users
postsUrl += "&login=" + os.Getenv("LOGIN_NAME") + "&api_key=" + os.Getenv("API_KEY")

if os.Getenv("LOGIN_NAME") != "" && os.Getenv("API_KEY") != "" {
postsUrl += "&login=" + os.Getenv("LOGIN_NAME") + "&api_key=" + os.Getenv("API_KEY")
}

response, err := http.Get(postsUrl)
if err != nil {
Expand All @@ -227,7 +239,6 @@ func fetchPostsFromPage(tags []string, totalPageAmount int, options inputOptions
if err != nil {
fmt.Println("Error reading response")
}

// Parse JSON Response into list of posts
var result []Post
if err := json.Unmarshal(responseData, &result); err != nil {
Expand Down Expand Up @@ -263,7 +274,9 @@ func getTotalPages(tags []string) int {
url := fmt.Sprintf("https://danbooru.donmai.us/posts?tags=%s&limit=200", tagString)

// Credentials to get access to extra features for Danbooru Gold users
url += "&login=" + os.Getenv("LOGIN_NAME") + "&api_key=" + os.Getenv("API_KEY")
if os.Getenv("LOGIN_NAME") != "" && os.Getenv("API_KEY") != "" {
url += "&login=" + os.Getenv("LOGIN_NAME") + "&api_key=" + os.Getenv("API_KEY")
}

resp, err := http.Get(url)
if err != nil {
Expand Down

0 comments on commit d0c82ff

Please sign in to comment.