This commit is contained in:
Jeffrey Paul 2024-05-20 06:36:41 -07:00
parent 56961bda66
commit 355b90f584

90
main.go
View File

@ -194,6 +194,18 @@ func main() {
logErrorAndExit(fmt.Errorf("failed to create images directory: %w", err))
}
// Create foils and promos directories inside alpha and beta directories
for _, setType := range []string{"alpha", "beta"} {
err = os.MkdirAll(filepath.Join(imageDir, setType, "foils"), os.ModePerm)
if err != nil {
logErrorAndExit(fmt.Errorf("failed to create foils directory: %w", err))
}
err = os.MkdirAll(filepath.Join(imageDir, setType, "promos"), os.ModePerm)
if err != nil {
logErrorAndExit(fmt.Errorf("failed to create promos directory: %w", err))
}
}
// Fetch all cards for both alpha and beta sets
alphaCards, err := fetchAllCards(limit, "alp")
if err != nil {
@ -207,13 +219,14 @@ func main() {
// Save alpha and beta cards separately and write combined cards.json
allCards := append(alphaCards, betaCards...)
saveCards(alphaCards, "alpha", dataDir, imageDir)
saveCards(betaCards, "beta", dataDir, imageDir)
saveCards(allCards, "all", dataDir, imageDir)
saveCardsAsJSON(alphaCards, "alpha", dataDir)
saveCardsAsJSON(betaCards, "beta", dataDir)
saveCardsAsJSON(allCards, "all", dataDir)
fetchImagesForCards(allCards, imageDir)
}
// saveCards saves all cards for the given set type and writes to files and images.
func saveCards(cards []*Card, filePrefix, dataDir, imageDir string) {
// saveCardsAsJSON saves all cards for the given set type to JSON files.
func saveCardsAsJSON(cards []*Card, filePrefix, dataDir string) {
// Remove duplicates
uniqueCards := removeDuplicateCards(cards)
@ -221,24 +234,6 @@ func saveCards(cards []*Card, filePrefix, dataDir, imageDir string) {
filename := fmt.Sprintf("%s/%s_cards.json", dataDir, filePrefix)
writeCardsToFile(filename, uniqueCards)
// Download images for each variant and save to the appropriate directory
for _, card := range uniqueCards {
for _, variant := range card.Variants {
imagePath := generateImageFilename(variant)
err := os.MkdirAll(filepath.Dir(imagePath), os.ModePerm)
if err != nil {
logErrorAndExit(fmt.Errorf("failed to create image directory: %w", err))
}
err = downloadImage(variant.Src, imagePath)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to download image %s: %v\n", imagePath, err)
continue
}
}
}
// Group cards by element and write to files
groupedByElement := groupCardsByElement(uniqueCards)
for element, elementCards := range groupedByElement {
@ -256,6 +251,53 @@ func saveCards(cards []*Card, filePrefix, dataDir, imageDir string) {
fmt.Printf("All cards have been written to %s_cards.json. Total cards: %d\n", filePrefix, len(uniqueCards))
}
// fetchImagesForCards downloads images for each card variant and saves them to the appropriate directory.
func fetchImagesForCards(cards []*Card, imageDir string) {
for _, card := range cards {
for _, variant := range card.Variants {
imagePath := generateImageFilename(variant)
err := os.MkdirAll(filepath.Dir(imagePath), os.ModePerm)
if err != nil {
logErrorAndExit(fmt.Errorf("failed to create image directory: %w", err))
}
// Download the image
err = downloadImage(variant.Src, imagePath)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to download image %s: %v\n", imagePath, err)
continue
}
// Hardlink to foils directory if the variant is a foil
if variant.Finish == "Foil" {
linkPath := filepath.Join(imageDir, filepath.Dir(imagePath), "foils", filepath.Base(imagePath))
err := createHardlink(imagePath, linkPath)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to create hardlink %s: %v\n", linkPath, err)
}
}
// Hardlink to promos directory if the variant is a promo
if variant.Product == "Promo" {
linkPath := filepath.Join(imageDir, filepath.Dir(imagePath), "promos", filepath.Base(imagePath))
err := createHardlink(imagePath, linkPath)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to create hardlink %s: %v\n", linkPath, err)
}
}
}
}
}
// createHardlink creates a hardlink from src to dst, if dst does not already exist.
func createHardlink(src, dst string) error {
if _, err := os.Stat(dst); err == nil {
return nil // Hardlink already exists
}
return os.Link(src, dst)
}
// removeDuplicateCards removes duplicate cards by checking the 'id' field.
func removeDuplicateCards(cards []*Card) []*Card {
cardMap := make(map[string]*Card)
@ -310,7 +352,7 @@ func groupCardsByElement(cards []*Card) map[string][]*Card {
grouped := make(map[string][]*Card)
for _, card := range cards {
for _, element := range card.Elements {
grouped[element.Name] = append(grouped[element.Name], card)
grouped[strings.ToLower(element.Name)] = append(grouped[strings.ToLower(element.Name)], card)
}
}
return grouped