Skip to content

Instantly share code, notes, and snippets.

@brnstz
Last active April 19, 2016 13:10
Show Gist options
  • Save brnstz/c5566f68c22e1b93a152ea98917d5263 to your computer and use it in GitHub Desktop.
Save brnstz/c5566f68c22e1b93a152ea98917d5263 to your computer and use it in GitHub Desktop.
package main
import (
"flag"
"fmt"
"log"
"github.com/brnstz/routine/wikimg"
)
var (
// Print a blank line with the given 256 ANSI color
fmtSpec = "\x1b[30;48;5;%dm%-80s\x1b[0m\n"
)
func main() {
var max int
flag.IntVar(&max, "max", 100, "maximum number of images to retrieve")
flag.Parse()
// Create a new image puller with our max
p := wikimg.NewPuller(max)
// Loop to retrieve more images
for {
imgURL, err := p.Next()
if err == wikimg.EndOfResults {
// Break from loop when end of results is reached
break
} else if err != nil {
// Log error and continue getting URLs
log.Println(err)
continue
}
// Everybody gets a goroutine!
go func() {
// Get the first color in this image
color, _, err := p.FirstColor(imgURL)
if err != nil {
log.Println(err)
return
}
// Print color to the terminal
fmt.Printf(fmtSpec, color, "")
}()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment