Skip to content

Commit

Permalink
fix: error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
unspok3n committed Sep 17, 2024
1 parent 0f41796 commit 47bc224
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 32 deletions.
62 changes: 31 additions & 31 deletions cmd/itunes-cover-scraper/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,44 +28,44 @@ func main() {
search, err := itunes.Search(query)
if err != nil {
fmt.Println("Search:", err)
}

if search.ResultCount > 0 {
rawUrl := search.Results[0].ArtworkUrl100
originalUrl := itunes.OriginalUrl(rawUrl)
} else {
if search.ResultCount > 0 {
rawUrl := search.Results[0].ArtworkUrl100
originalUrl := itunes.OriginalUrl(rawUrl)

var coverUrl string
if originalUrl != "" {
coverUrl = originalUrl
} else {
coverUrl = strings.Replace(rawUrl, "100x100bb.jpg", "3000x3000bb.jpg", 1)
}
var coverUrl string
if originalUrl != "" {
coverUrl = originalUrl
} else {
coverUrl = strings.Replace(rawUrl, "100x100bb.jpg", "3000x3000bb.jpg", 1)
}

fmt.Println("Downloading...")
err := DownloadFile(coverUrl, coverFilename)
if err != nil {
fmt.Println("Error downloading cover:", err)
}
fmt.Println("Downloading...")
err := DownloadFile(coverUrl, coverFilename)
if err != nil {
fmt.Println("Error downloading cover:", err)
}

fmt.Println("Removing metadata...")
data, err := os.ReadFile(coverFilename)
if err != nil {
fmt.Println("Error reading cover:", err)
}
fmt.Println("Removing metadata...")
data, err := os.ReadFile(coverFilename)
if err != nil {
fmt.Println("Error reading cover:", err)
}

filtered, err := StripExif(data)
if err != nil {
if !errors.Is(err, ErrExifMarkerNotFound) {
fmt.Println("Error removing EXIF metadata:", err)
filtered, err := StripExif(data)
if err != nil {
if !errors.Is(err, ErrExifMarkerNotFound) {
fmt.Println("Error removing EXIF metadata:", err)
}
} else {
if err := os.WriteFile(coverFilename, filtered, 0644); err != nil {
fmt.Println("Error saving cover file:", err)
}
}

} else {
if err := os.WriteFile(coverFilename, filtered, 0644); err != nil {
fmt.Println("Error saving cover file:", err)
}
fmt.Println("No results found")
}

} else {
fmt.Println("No results found")
}

fmt.Println("Press enter to exit")
Expand Down
8 changes: 7 additions & 1 deletion pkg/itunes/itunes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package itunes

import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -51,6 +52,10 @@ func PrepareQuery(query string) string {
return query
}

var (
ErrInvalidJsonp = errors.New("invalid jsonp")
)

func Search(query string) (*SearchResponse, error) {
endpoint := fmt.Sprintf(
"https://itunes.apple.com/search?callback=jQuery20309382196007363668_1726162953706&entity=song,album&media=music&entity=song&term=%s&country=nz&limit=1",
Expand All @@ -72,7 +77,8 @@ func Search(query string) (*SearchResponse, error) {
start := strings.Index(bodyStr, "(")
end := strings.LastIndex(bodyStr, ")")
if start == -1 || end == -1 || start >= end {
return nil, fmt.Errorf("jsonp parse error: %w", err)
fmt.Println(bodyStr)
return nil, ErrInvalidJsonp
}
jsonData := bodyStr[start+1 : end]

Expand Down

0 comments on commit 47bc224

Please sign in to comment.