Skip to content

Commit

Permalink
refactor: add a method for the provider to generate photo IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
nakrovati committed May 9, 2024
1 parent fbdf894 commit f250ea8
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 23 deletions.
28 changes: 12 additions & 16 deletions internal/downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,34 +24,30 @@ type PhotosProvider interface {
GetCollectionName() string
GetProviderName() string
GetFileName(src string) string
GetPhotoURL(photoID int) (string, error)
GetPhotoURL(photoID string) (string, error)
GetPhotos() ([]string, error)
GetRecentPhotoID() (int, error)
GetMinMaxPhotoID() (int, int)
}

type Downloader struct {
PhotosProvider
PhotosProvider PhotosProvider
}

func (d *Downloader) DownloadPhotos() error {
min, max := d.GetMinMaxPhotoID()

if max == 100000 {
var err error

max, err = d.PhotosProvider.GetRecentPhotoID()
if err != nil {
return fmt.Errorf("failed to get recent photo ID: %w", err)
}
downloadDir, err := utils.GetDownloadDirectory(d.PhotosProvider.GetProviderName(), d.PhotosProvider.GetCollectionName())
if err != nil {
return fmt.Errorf("failed to get download directory: %w", err)
}

downloadDir, err := utils.GetDownloadDirectory(d.PhotosProvider.GetProviderName(), d.GetCollectionName())
photos, err := d.PhotosProvider.GetPhotos()
if err != nil {
return fmt.Errorf("failed to get download directory: %w", err)
return fmt.Errorf("failed to get photos: %w", err)
}

for i := max; i >= min; i-- {
photoURL, err := d.PhotosProvider.GetPhotoURL(i)
for i := len(photos) - 1; i >= 0; i-- {
photoID := photos[i]

photoURL, err := d.PhotosProvider.GetPhotoURL(photoID)
if err != nil {
return fmt.Errorf("failed to get photo URL: %w", err)
}
Expand Down
34 changes: 31 additions & 3 deletions pkg/providers/fapello/fapello.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,41 @@ func (p *Provider) GetCollectionName() string {
return p.Username
}

func (p *Provider) GetPhotoURL(photoID int) (string, error) {
urlWithoutID, err := buildURL(p.BaseURL, p.Username, photoID)
func (p *Provider) GetPhotos() ([]string, error) {
if p.MinPhotoID > p.MaxPhotoID {
return []string{}, fmt.Errorf("min photo ID (%d) is greater than max photo ID (%d)", p.MinPhotoID, p.MaxPhotoID)
}

recentPhotoID, err := p.GetRecentPhotoID()
if err != nil {
return []string{}, err
}

if p.MaxPhotoID > recentPhotoID {
p.MaxPhotoID = recentPhotoID
}

photos := make([]string, p.MaxPhotoID-p.MinPhotoID+1)

for i := p.MinPhotoID; i <= p.MaxPhotoID; i++ {
photos[i-p.MinPhotoID] = strconv.Itoa(i)
}

return photos, nil
}

func (p *Provider) GetPhotoURL(photoID string) (string, error) {
intPhotoID, err := strconv.Atoi(photoID)
if err != nil {
return "", err
}

urlWithoutID, err := buildURL(p.BaseURL, p.Username, intPhotoID)
if err != nil {
return "", err
}

paddedID := fmt.Sprintf("%04d", photoID)
paddedID := fmt.Sprintf("%04d", intPhotoID)
photoName := fmt.Sprintf("%s_%v.jpg", p.Username, paddedID)

url, err := url.JoinPath(urlWithoutID, photoName)
Expand Down
32 changes: 28 additions & 4 deletions pkg/providers/fapodrop/fapodrop.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,41 @@ func (p *Provider) GetCollectionName() string {
return p.Username
}

func (p *Provider) GetMinMaxPhotoID() (int, int) {
return p.MinPhotoID, p.MaxPhotoID
func (p *Provider) GetPhotos() ([]string, error) {
if p.MinPhotoID > p.MaxPhotoID {
return []string{}, fmt.Errorf("min photo ID (%d) is greater than max photo ID (%d)", p.MinPhotoID, p.MaxPhotoID)
}

recentPhotoID, err := p.GetRecentPhotoID()
if err != nil {
return []string{}, err
}

if p.MaxPhotoID > recentPhotoID {
p.MaxPhotoID = recentPhotoID
}

photos := make([]string, p.MaxPhotoID-p.MinPhotoID+1)

for i := p.MinPhotoID; i <= p.MaxPhotoID; i++ {
photos[i-p.MinPhotoID] = strconv.Itoa(i)
}

return photos, nil
}

func (p *Provider) GetPhotoURL(photoID int) (string, error) {
func (p *Provider) GetPhotoURL(photoID string) (string, error) {
intPhotoID, err := strconv.Atoi(photoID)
if err != nil {
return "", err
}

urlWithoutID, err := buildURL(p.BaseURL, p.Username)
if err != nil {
return "", err
}

paddedID := fmt.Sprintf("%04d", photoID)
paddedID := fmt.Sprintf("%04d", intPhotoID)
photoName := fmt.Sprintf("%s_%s.jpeg", p.Username, paddedID)

url, err := url.JoinPath(urlWithoutID, photoName)
Expand Down

0 comments on commit f250ea8

Please sign in to comment.