Skip to content

Commit

Permalink
feat: add the ability to specify a min and max id
Browse files Browse the repository at this point in the history
  • Loading branch information
nakrovati committed Mar 18, 2024
1 parent fd77b68 commit baa35c9
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 13 deletions.
27 changes: 24 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@ package main
import (
"fapesnap/pkg/providers/fapello"
"fapesnap/pkg/providers/fapodrop"
"fapesnap/pkg/utils"
"log"

"github.com/spf13/cobra"
)

type Provider interface {
DownloadPhotos(userName string) error
DownloadPhotos(userName string, min int, max int) error
}

var (
userName string
min int
max int
rootCmd = &cobra.Command{
Use: "root",
Short: "Download photos from fapello/fapodrop",
Expand All @@ -25,8 +28,14 @@ var (
if userName == "" {
log.Fatal("You must specify a username")
}

err := utils.ValidateMinMax(min, max)
if err != nil {
log.Fatal(err)
}

var fapodropProvider Provider = &fapodrop.FapodropProvider{}
fapodropProvider.DownloadPhotos(userName)
fapodropProvider.DownloadPhotos(userName, min, max)
},
}
fapelloCmd = &cobra.Command{
Expand All @@ -36,17 +45,29 @@ var (
if userName == "" {
log.Fatal("You must specify a username")
}

err := utils.ValidateMinMax(min, max)
if err != nil {
log.Fatal(err)
}

var fapelloProvider Provider = &fapello.FapelloProvider{}
fapelloProvider.DownloadPhotos(userName)
fapelloProvider.DownloadPhotos(userName, min, max)
},
}
)

func init() {
rootCmd.AddCommand(fapodropCmd)
rootCmd.AddCommand(fapelloCmd)

fapodropCmd.PersistentFlags().StringVarP(&userName, "username", "u", "", "Profile name in fapodrop")
fapodropCmd.PersistentFlags().IntVarP(&min, "min", "", 1, "Minimum photo ID")
fapodropCmd.PersistentFlags().IntVarP(&max, "max", "", 100000, "Maximum photo ID")

fapelloCmd.PersistentFlags().StringVarP(&userName, "username", "u", "", "Profile name in fapello")
fapelloCmd.PersistentFlags().IntVarP(&min, "min", "", 1, "Minimum photo ID")
fapelloCmd.PersistentFlags().IntVarP(&max, "max", "", 100000, "Maximum photo ID")
}

func main() {
Expand Down
10 changes: 6 additions & 4 deletions pkg/providers/fapello/fapello.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,18 @@ var (

type FapelloProvider struct{}

func (p *FapelloProvider) DownloadPhotos(userName string) error {
func (p *FapelloProvider) DownloadPhotos(userName string, min int, max int) error {
downloadDir, err := utils.GetDownloadDirectory(providerName, userName)
if err != nil {
fmt.Println("Error while getting download directory:", err)
return err
}

recentPhotoID, err := getRecentPhotoID(userName)
if err != nil {
return err
recentPhotoID := max
if max == 100000 {
if recentPhotoID, err = getRecentPhotoID(userName); err != nil {
return err
}
}

urlWithoutID, err := buildURL(userName, recentPhotoID)
Expand Down
13 changes: 7 additions & 6 deletions pkg/providers/fapodrop/fapodrop.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,26 @@ var (

type FapodropProvider struct{}

func (p *FapodropProvider) DownloadPhotos(userName string) error {
func (p *FapodropProvider) DownloadPhotos(userName string, min int, max int) error {
downloadDir, err := utils.GetDownloadDirectory(providerName, userName)
if err != nil {
fmt.Println("Error while getting download directory:", err)
return err
}

recentPhotoID, err := getRecentPhotoID(userName)
if err != nil {
return err
recentPhotoID := max
if max == 100000 {
if recentPhotoID, err = getRecentPhotoID(userName); err != nil {
return err
}
}

urlWithoutID, err := buildURL(userName)
if err != nil {
return err
}

for i := recentPhotoID; i >= 1; i-- {
for i := recentPhotoID; i >= min; i-- {
paddedID := fmt.Sprintf("%04d", i)
photoName := fmt.Sprintf("%s_%s.jpeg", userName, paddedID)

Expand All @@ -53,7 +55,6 @@ func (p *FapodropProvider) DownloadPhotos(userName string) error {
println("Downloaded:", photoName)
}
return nil

}

func buildURL(name string) (string, error) {
Expand Down
19 changes: 19 additions & 0 deletions pkg/utils/validateMinMax.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package utils

import "fmt"

func ValidateMinMax(min int, max int) error {
if min > max {
return fmt.Errorf("min cannot be greater than max")
}

if min < 1 || max < 1 {
return fmt.Errorf("min and max cannot be less than 1")
}

if min > 100000 || max > 100000 {
return fmt.Errorf("max cannot be greater than 100000")
}

return nil
}

0 comments on commit baa35c9

Please sign in to comment.