Skip to content

Commit

Permalink
chore(kemono): Add custom type for time parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Jun 30, 2024
1 parent 149ba26 commit c0efd03
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 17 deletions.
4 changes: 2 additions & 2 deletions internal/kemono/creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ type Creator struct {
ID string `json:"id"`
Name string `json:"name"`
Service string `json:"service"`
Indexed string `json:"indexed"`
Updated string `json:"updated"`
Indexed Time `json:"indexed"`
Updated Time `json:"updated"`
}

func (c *Creator) ImageURL() *url.URL {
Expand Down
24 changes: 9 additions & 15 deletions internal/kemono/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ type Post struct {
Title string `json:"title"`
Content string `json:"content"`
Embed Embed `json:"embed"`
Added string `json:"added"`
Published string `json:"published"`
Edited string `json:"edited"`
Added Time `json:"added"`
Published Time `json:"published"`
Edited Time `json:"edited"`
Tags Tags `json:"tags"`
File *Attachment `json:"file"`
Attachments []*Attachment `json:"attachments"`
Expand All @@ -42,15 +42,11 @@ func (p *Post) URL() *url.URL {

func (p *Post) FeedItem() *feeds.Item {
item := &feeds.Item{
Id: p.ID,
Link: &feeds.Link{Href: p.URL().String()},
Title: p.Title,
}
if parsed, err := time.Parse("2006-01-02T15:04:05", p.Published); err == nil {
item.Created = parsed
}
if parsed, err := time.Parse("2006-01-02T15:04:05", p.Edited); err == nil {
item.Updated = parsed
Id: p.ID,
Link: &feeds.Link{Href: p.URL().String()},
Title: p.Title,
Created: time.Time(p.Published),
Updated: time.Time(p.Edited),
}

var buf strings.Builder
Expand Down Expand Up @@ -93,12 +89,10 @@ func (p *Post) PodcastItem(ctx context.Context) (*podcast.Item, *Attachment, err
TypeFormatted: audioInfo.MIMEType,
},
}
item.AddPubDate((*time.Time)(&p.Published))
if image != nil {
item.AddImage(image.ThumbURL().String())
}
if parsed, err := time.Parse("2006-01-02T15:04:05", p.Published); err == nil {
item.AddPubDate(&parsed)
}
return item, image, nil
}

Expand Down
42 changes: 42 additions & 0 deletions internal/kemono/util.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package kemono

import (
"bytes"
"encoding/json"
"errors"
"strings"
"time"

"golang.org/x/text/cases"
"golang.org/x/text/language"
Expand All @@ -19,3 +23,41 @@ func formatServiceName(name string) string {
name = serviceNameReplacer.Replace(name)
return name
}

var ErrInvalidTimeType = errors.New("invalid time type")

type Time time.Time

func (d *Time) UnmarshalJSON(data []byte) error {
decoder := json.NewDecoder(bytes.NewReader(data))
decoder.UseNumber()

t, err := decoder.Token()
if err != nil {
return err
}

switch t := t.(type) {
case string:
parsed, err := time.Parse("2006-01-02T15:04:05", t)
if err != nil {
return err
}

*d = Time(parsed.UTC())
return nil
case json.Number:
val, err := t.Int64()
if err != nil {
return err
}

parsed := time.Unix(val, 0).UTC()
*d = Time(parsed)
return nil
case nil:
*d = Time(time.Time{}.UTC())
return nil
}
return ErrInvalidTimeType
}
31 changes: 31 additions & 0 deletions internal/kemono/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package kemono

import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestTime_UnmarshalJSON(t *testing.T) {
type args struct {
bytes []byte
}
tests := []struct {
name string
d Time
args args
want time.Time
wantErr require.ErrorAssertionFunc
}{
{"string", Time{}, args{[]byte(`"2024-05-02T14:21:02.807702"`)}, time.Date(2024, time.May, 2, 14, 21, 2, 807702000, time.UTC), require.NoError},
{"unix", Time{}, args{[]byte(`1714659663`)}, time.Date(2024, time.May, 2, 14, 21, 3, 0, time.UTC), require.NoError},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.wantErr(t, tt.d.UnmarshalJSON(tt.args.bytes))
assert.Equal(t, tt.want, time.Time(tt.d))
})
}
}

0 comments on commit c0efd03

Please sign in to comment.