gosplash/internal/unsplash/parser.go

43 lines
923 B
Go
Raw Normal View History

2023-11-23 14:24:28 +01:00
package unsplash
import (
"strconv"
"strings"
"time"
"git.velvettear.de/velvettear/loggo"
)
// parse a slice of json map objects to images
func parseData(data []map[string]interface{}) []Image {
timestamp := time.Now().UnixMilli()
var images []Image
for _, value := range data {
images = append(images, parseSingleData(value))
}
loggo.DebugTimed("parsed "+strconv.Itoa(len(images))+" json object(s)", timestamp)
return images
}
// parse a single json map object to an image
func parseSingleData(data map[string]interface{}) Image {
var img Image
for key, value := range data {
switch strings.ToLower(key) {
case "id":
img.Id = value.(string)
case "slug":
img.Name = value.(string)
case "urls":
for subkey, subvalue := range value.(map[string]interface{}) {
if strings.ToLower(subkey) != "full" {
continue
}
img.Url = subvalue.(string)
break
}
}
}
return img
}