-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
22d0502
commit c3bcd40
Showing
8 changed files
with
151 additions
and
10 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,12 @@ | ||
package krapo | ||
|
||
import _ "github.com/fluffy-melli/krapo/RDR_CMP" | ||
import ( | ||
"time" | ||
|
||
_ "github.com/fluffy-melli/krapo/RDR_CMP" // https://www.data.go.kr/tcs/dss/selectApiDataDetailView.do?publicDataPk=15056924 | ||
) | ||
|
||
func Time() string { | ||
currentTime := time.Now() | ||
return currentTime.Format("20060102") | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package render | ||
|
||
import ( | ||
"fmt" | ||
"image" | ||
_ "image/gif" | ||
_ "image/jpeg" | ||
_ "image/png" | ||
"net/http" | ||
"strings" | ||
"sync" | ||
) | ||
|
||
var FileCache = make(map[string]image.Image) | ||
var mu sync.RWMutex | ||
|
||
func LoadImageFromURL(ImageURL string) (image.Image, error) { | ||
ImageURL = strings.TrimSpace(ImageURL) | ||
mu.RLock() | ||
img, exists := FileCache[ImageURL] | ||
mu.RUnlock() | ||
if exists { | ||
return img, nil | ||
} | ||
resp, err := http.Get(ImageURL) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to fetch image from URL %s: %v", ImageURL, err) | ||
} | ||
defer resp.Body.Close() | ||
img, _, err = image.Decode(resp.Body) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to decode image from URL %s: %v", ImageURL, err) | ||
} | ||
mu.Lock() | ||
FileCache[ImageURL] = img | ||
mu.Unlock() | ||
return img, nil | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package render | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"os" | ||
) | ||
|
||
func Write(filename string, buf *bytes.Buffer) error { | ||
if buf == nil { | ||
return fmt.Errorf("buffer cannot be nil") | ||
} | ||
file, err := os.Create(filename) | ||
if err != nil { | ||
return fmt.Errorf("failed to create file: %v", err) | ||
} | ||
defer file.Close() | ||
_, err = file.Write(buf.Bytes()) | ||
if err != nil { | ||
return fmt.Errorf("failed to write data to file: %v", err) | ||
} | ||
return nil | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package render | ||
|
||
import ( | ||
"bytes" | ||
"image" | ||
"image/color" | ||
"image/gif" | ||
) | ||
|
||
func GIF(urls []string, delay int) (*bytes.Buffer, error) { | ||
var images []*image.Paletted | ||
var delays []int | ||
for _, url := range urls { | ||
img, err := LoadImageFromURL(url) | ||
if err != nil { | ||
return nil, err | ||
} | ||
palette := Palette(img) | ||
palettedImage := image.NewPaletted(img.Bounds(), palette) | ||
for y := 0; y < palettedImage.Bounds().Dy(); y++ { | ||
for x := 0; x < palettedImage.Bounds().Dx(); x++ { | ||
palettedImage.Set(x, y, img.At(x, y)) | ||
} | ||
} | ||
images = append(images, palettedImage) | ||
delays = append(delays, delay) | ||
} | ||
var buf bytes.Buffer | ||
err := gif.EncodeAll(&buf, &gif.GIF{ | ||
Image: images, | ||
Delay: delays, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &buf, nil | ||
} | ||
|
||
func Palette(frame image.Image) color.Palette { | ||
colorSet := make(map[color.Color]struct{}) | ||
for y := 0; y < frame.Bounds().Dy(); y++ { | ||
for x := 0; x < frame.Bounds().Dx(); x++ { | ||
colorSet[frame.At(x, y)] = struct{}{} | ||
} | ||
} | ||
var colors []color.Color | ||
for c := range colorSet { | ||
colors = append(colors, c) | ||
} | ||
if len(colors) > 256 { | ||
colors = colors[:256] | ||
} | ||
return colors | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"testing" | ||
|
||
"github.com/fluffy-melli/krapo" | ||
"github.com/fluffy-melli/krapo/RDR_CMP" | ||
"github.com/fluffy-melli/krapo/render" | ||
) | ||
|
||
func Test(t *testing.T) { | ||
urls, err := RDR_CMP.GetImagesURL("", krapo.Time()) | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
gif, err := render.GIF(urls, 5) | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
err = render.Write("./test.gif", gif) | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.