This repository has been archived by the owner on Feb 24, 2020. It is now read-only.
generated from earthboundkid/go-cli
/
feed.go
198 lines (183 loc) · 4.9 KB
/
feed.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package feed
import (
"encoding/json"
"fmt"
"log"
"sort"
"strings"
"time"
"github.com/spotlightpa/arc-reader/internal/jsonschema"
)
type Feed struct {
Stories []*Story
}
func (f *Feed) UnmarshalJSON(data []byte) error {
var v jsonschema.API
err := json.Unmarshal(data, &v)
f.Stories = make([]*Story, 0, len(v.Contents))
for _, content := range v.Contents {
story := contentToStory(content)
if story != nil {
f.Stories = append(f.Stories, story)
}
}
sort.Slice(f.Stories, func(i, j int) bool {
return f.Stories[i].PubDate.Before(f.Stories[j].PubDate)
})
return err
}
type Story struct {
ID string `toml:"internal-id"`
ImageCredit string `toml:"image-credit"`
ImageCaption string `toml:"image-description"`
ImageURL string `toml:"image"`
Slug string `toml:"slug"`
PubDate time.Time `toml:"published"`
Budget string `toml:"internal-budget"`
Hed string `toml:"title"`
Subhead string `toml:"subtitle"`
Summary string `toml:"description"`
Blurb string `toml:"dek"`
Authors []string `toml:"authors"`
Body string `toml:"-"`
LinkTitle string `toml:"linktitle"`
}
func contentToStory(content jsonschema.Contents) *Story {
authors := make([]string, len(content.Credits.By))
for i := range content.Credits.By {
authors[i] = content.Credits.By[i].Name
}
var body strings.Builder
readContentElements(content.ContentElements, &body)
story := Story{
ID: content.Slug,
Slug: slugFromURL(content.CanonicalURL),
PubDate: content.Planning.Scheduling.PlannedPublishDate,
Budget: content.Planning.BudgetLine,
Hed: content.Headlines.Basic,
Subhead: content.Subheadlines.Basic,
Summary: content.Description.Basic,
Blurb: content.Description.Basic,
Authors: authors,
Body: body.String(),
LinkTitle: content.Headlines.Web,
}
if image := imageFrom(content.PromoItems); image != nil {
story.ImageURL = image.URL
story.ImageCaption = image.Caption
story.ImageCredit = image.Credit
}
return &story
}
func (story *Story) String() string {
if story == nil {
return "<nil story>"
}
return fmt.Sprintf("%#v", *story)
}
func slugFromURL(s string) string {
stop := strings.LastIndexByte(s, '-')
if stop == -1 {
return s
}
start := strings.LastIndexByte(s[:stop], '/')
if start == -1 {
return s
}
return s[start+1 : stop]
}
func readContentElements(rawels []*json.RawMessage, body *strings.Builder) {
for i, raw := range rawels {
var _type string
wrapper := jsonschema.ContentElementType{Type: &_type}
if err := json.Unmarshal(*raw, &wrapper); err != nil {
log.Printf("runtime error: %v", err)
}
var graf string
switch _type {
case "text", "raw_html":
wrapper := jsonschema.ContentElementText{Content: &graf}
if err := json.Unmarshal(*raw, &wrapper); err != nil {
log.Printf("runtime error: %v", err)
}
case "header":
var v jsonschema.ContentElementHeading
if err := json.Unmarshal(*raw, &v); err != nil {
log.Printf("runtime error: %v", err)
}
graf = strings.Repeat("#", v.Level) + " " + v.Content
case "oembed_response":
var v jsonschema.ContentElementOembed
if err := json.Unmarshal(*raw, &v); err != nil {
log.Printf("runtime error: %v", err)
}
graf = v.RawOembed.HTML
case "list":
var v jsonschema.ContentElementList
if err := json.Unmarshal(*raw, &v); err != nil {
log.Printf("runtime error: %v", err)
}
var identifier string
switch v.ListType {
case "unordered":
identifier = "- "
default:
log.Printf("warning: unknown list type - %q", v.ListType)
}
for j, item := range v.Items {
var li string
if j != 0 {
body.WriteString("\n\n")
}
switch item.Type {
case "text":
li = item.Content
default:
log.Printf("warning: unknown item type - %q", item.Type)
}
body.WriteString(identifier)
body.WriteString(li)
body.WriteString("\n\n")
}
case "image":
var v jsonschema.ContentElementImage
if err := json.Unmarshal(*raw, &v); err != nil {
log.Printf("runtime error: %v", err)
}
var credits []string
for _, c := range v.Credits.By {
credits = append(credits, c.Name)
}
graf = fmt.Sprintf("## Image:\n\n%s\n\n%s (%s)\n",
v.URL, v.Caption, strings.Join(credits, " "),
)
default:
log.Printf("warning: unknown element type - %q", _type)
}
if i != 0 {
body.WriteString("\n\n")
}
body.WriteString(graf)
}
}
type Image struct {
Credit, Caption, URL string
}
func imageFrom(p jsonschema.PromoItems) *Image {
var credits []string
for i, credit := range p.Basic.Credits.By {
name := credit.Byline
if name == "" {
name = credit.Name
}
credits = append(credits, name)
if len(p.Basic.Credits.Affiliation) > i {
credits = append(credits, p.Basic.Credits.Affiliation[i].Name)
}
}
return &Image{
strings.Join(credits, " / "),
p.Basic.Caption,
p.Basic.URL,
}
}