Created
May 4, 2015 06:37
-
-
Save ksnabb/9a22a9f67a11a410d00e to your computer and use it in GitHub Desktop.
Retrieve images of products according to ean code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"encoding/json" | |
"image" | |
"image/color" | |
"image/draw" | |
"image/jpeg" | |
"io/ioutil" | |
"log" | |
"net/http" | |
"os" | |
"strconv" | |
"text/template" | |
"github.com/nfnt/resize" | |
) | |
const SIDELENGTH int = 300 | |
func handleImageFromLink(link string, ean string) int { | |
if link == "" { | |
return 400 | |
} | |
resp, err := http.Get(link) | |
if err != nil { | |
log.Print(err) | |
return 400 | |
} | |
log.Print(link + " HTTP GET status code: " + strconv.Itoa(resp.StatusCode)) | |
if resp.StatusCode != 200 { | |
return 400 | |
} | |
var img image.Image | |
img, err = jpeg.Decode(resp.Body) | |
if err != nil { | |
log.Print(err) | |
return 400 | |
} | |
file, err := os.Create("./img/" + ean + ".jpg") | |
if err != nil { | |
log.Print(err) | |
return 400 | |
} | |
bounds := img.Bounds() | |
// resize img to the right size | |
if bounds.Max.X < bounds.Max.Y { | |
img = resize.Resize(0, uint(SIDELENGTH), img, resize.Bicubic) | |
} else { | |
img = resize.Resize(uint(SIDELENGTH), 0, img, resize.Bicubic) | |
} | |
// add white so that img becomes square | |
bounds = img.Bounds() | |
xd := (SIDELENGTH - bounds.Max.X) / 2 | |
yd := (SIDELENGTH - bounds.Max.Y) / 2 | |
// create image with sie 187 187 | |
timg := image.NewRGBA(image.Rect(0, 0, SIDELENGTH, SIDELENGTH)) | |
draw.Draw(timg, timg.Bounds(), &image.Uniform{color.White}, image.ZP, draw.Src) | |
draw.Draw(timg, image.Rect(xd, yd, SIDELENGTH, SIDELENGTH), img, image.ZP, draw.Src) | |
jpeg.Encode(file, timg, nil) | |
return 200 | |
} | |
type product struct { | |
Ean string | |
Nimike string | |
} | |
func main() { | |
products, err := ioutil.ReadFile("./products.json") | |
if err != nil { | |
log.Print(err) | |
} | |
var prods []product | |
err = json.Unmarshal(products, &prods) | |
if err != nil { | |
log.Print(err) | |
} | |
marketImageURL, err := template.New("marketImage").Parse("https://www.k-ruokakauppa.fi/Image/products/{{.Ean}}.jpg") | |
if err != nil { | |
log.Print(err) | |
} | |
kesproImageURL, err := template.New("kesproImage").Parse("https://www.kespro.com/Image/products/{{.Ean}}.jpg") | |
if err != nil { | |
log.Print(err) | |
} | |
image.RegisterFormat("jpeg", "jpeg", jpeg.Decode, jpeg.DecodeConfig) | |
var tempURL bytes.Buffer | |
for _, prod := range prods { | |
marketImageURL.Execute(&tempURL, prod) | |
link := tempURL.String() | |
code := handleImageFromLink(link, prod.Ean) | |
tempURL.Reset() | |
if code != 200 { | |
kesproImageURL.Execute(&tempURL, prod) | |
link := tempURL.String() | |
code = handleImageFromLink(link, prod.Ean) | |
tempURL.Reset() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment