Skip to content

Commit

Permalink
大阪対応した
Browse files Browse the repository at this point in the history
Fix #9
  • Loading branch information
otiai10 committed Feb 28, 2023
1 parent 8a83043 commit 3f6a89a
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 12 deletions.
6 changes: 3 additions & 3 deletions cli/rainnow.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import (
"fmt"
"os"

"github.com/otiai10/amesh/lib/rainnow"
"github.com/otiai10/amesh/lib/tenki"
"github.com/otiai10/gat/render"
)

// Rainnow ...
func Rainnow(r render.Renderer, location rainnow.Location) error {
// Tenki
func Tenki(r render.Renderer, location tenki.Location) error {
entry := location.GetEntry()
img, err := entry.Image()
if err != nil {
Expand Down
8 changes: 3 additions & 5 deletions lib/rainnow/japan.go → lib/tenki/japan.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package rainnow
package tenki

import (
"image"
Expand All @@ -8,10 +8,8 @@ import (
)

const (
// TenkiStaticURL ...
TenkiStaticURL = "https://static.tenki.jp/static-images"
// JapanEntryPath ...
JapanEntryPath = "/radar/2006/01/02/15/04/00/japan-detail-large.jpg"
JapanEntryPath = "/japan-detail-large.jpg"
)

// Japan ...
Expand All @@ -31,7 +29,7 @@ func (japan Japan) GetEntry() Entry {
}
now := truncateTime(time.Now().In(loc), 5*time.Minute)
return JapanEntry{
URL: TenkiStaticURL + now.Format(JapanEntryPath),
URL: TenkiStaticURL + now.Format(TenkiDynamicTimestampPath) + JapanEntryPath,
}
}

Expand Down
53 changes: 53 additions & 0 deletions lib/tenki/osaka.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package tenki

import (
"image"
"log"
"net/http"
"time"
)

const (
// TenkiStaticURL ...
OsakaEntryPath = "/pref-30-large.jpg"
)

// Osaka ...
type Osaka struct{}

// OsakaEntry ...
type OsakaEntry struct {
URL string
}

// GetEntry ...
func (osaka Osaka) GetEntry() Entry {
area := "Asia/Tokyo"
loc, err := time.LoadLocation(area)
if err != nil {
log.Fatalf("Failed to load location `%s`", area)
}
now := truncateTime(time.Now().In(loc), 5*time.Minute)
return OsakaEntry{
URL: TenkiStaticURL + now.Format(TenkiDynamicTimestampPath) + OsakaEntryPath,
}
}

// Image ...
func (osaka OsakaEntry) Image(client ...*http.Client) (image.Image, error) {
if len(client) == 0 {
client = append(client, http.DefaultClient)
}
res, err := client[0].Get(osaka.URL)
if err != nil {
return nil, err
}
defer res.Body.Close()
img, _, err := image.Decode(res.Body)
return img, err
}

// ReferenceURL ...
func (osaka OsakaEntry) ReferenceURL() string {
return "https://tenki.jp/"
}
10 changes: 9 additions & 1 deletion lib/rainnow/rainow.go → lib/tenki/tenki.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package rainnow
package tenki

import (
"image"
"net/http"
"time"
)

const (
// TenkiStaticURL ...
TenkiStaticURL = "https://static.tenki.jp/static-images"
// TenkiDynamicTimestampPath ...
TenkiDynamicTimestampPath = "/radar/2006/01/02/15/04/00"
)

// Location ...
type Location interface {
GetEntry() Entry
Expand All @@ -19,6 +26,7 @@ type Entry interface {

var supported = map[string]Location{
"japan": Japan{},
"osaka": Osaka{},
}

// GetLocation ...
Expand Down
6 changes: 3 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"os"

"github.com/otiai10/amesh/cli"
"github.com/otiai10/amesh/lib/rainnow"
"github.com/otiai10/amesh/lib/tenki"
"github.com/otiai10/gat/render"
)

Expand Down Expand Up @@ -53,9 +53,9 @@ func main() {
}
renderer.SetScale(scale)
subcommand := flag.Arg(0)
switch loc := rainnow.GetLocation(subcommand); {
switch loc := tenki.GetLocation(subcommand); {
case loc != nil:
onerror(cli.Rainnow(renderer, loc))
onerror(cli.Tenki(renderer, loc))
case subcommand == "typhoon":
onerror(cli.Typhoon(renderer))
case lapse:
Expand Down

0 comments on commit 3f6a89a

Please sign in to comment.