This repository has been archived by the owner on Jun 10, 2022. It is now read-only.
/
firstft.go
136 lines (108 loc) · 3.42 KB
/
firstft.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
package firstft
import (
"fmt"
"os"
// "regexp"
// "strings"
"time"
// "encoding/json"
"github.com/joho/godotenv"
. "github.com/gorilla/feeds"
"regexp"
"github.com/railsagainstignorance/alignment/content"
)
func getEnvParam(key string, defaultValue string) string {
godotenv.Load()
value := os.Getenv(key)
if value == "" {
value = defaultValue
}
return value
}
func getFirstFTArticles(maxArticles int) *[]*content.Article {
sRequest := &content.SearchRequest{
QueryType: "brand",
QueryText: "FirstFT",
MaxArticles: maxArticles,
MaxDurationMillis: 1000,
SearchOnly: false, // i.e. return full article details too from CAPI
}
fmt.Println("getFirstFTArticles: sRequest=", sRequest, ", maxArticles=", maxArticles)
sapiResult := content.Search(sRequest)
articles := []*content.Article {}
hrefRegexp := regexp.MustCompile(`href="https:\/\/www\.ft\.com\/content\/([0-9a-f\-]+)"`)
for i, article := range *(sapiResult.Articles) {
fmt.Println("getFirstFTArticles: ", i, ") ", article.Title)
articles = append( articles, article )
// rewrite the firstFT article
// - attributions, e.g. (FT, Miami Herald)</p> --> (by the FT and Miami Herald)
// - item title: span class="ft-bold">Indian phone fraud?</span> --> append ":"
// scan the firstFT article for FT article links, get the uuid
// - e.g. <a href="https://www.ft.com/content/1f9974ea-f9e7-11e6-9516-2d969e0d3b65">German business leader</a>s -> 1f9974ea-f9e7-11e6-9516-2d969e0d3b65
// - lookup in CAPI, append to list
matches := hrefRegexp.FindAllStringSubmatch(article.Body, -1)
if matches != nil {
fmt.Println("getFirstFTArticles: found ", len(matches), " references to FT articles) ")
for _,m := range matches {
uuid := m[1]
a := content.GetArticle(uuid)
articles = append( articles, a )
}
}
}
return &articles
}
// type (content.)Article struct {
// SiteUrl string
// Uuid string
// Title string
// Author string
// Excerpt string
// Body string
// PubDateString string
// PubDate *time.Time
// ImageUrl string
// ImageWidth int
// ImageHeight int
// NonPromoImageUrl string
// NonPromoImageWidth int
// NonPromoImageHeight int
// PromoImageUrl string
// PromoImageWidth int
// PromoImageHeight int
// PullQuoteAssets *[]PullQuoteAsset
// }
func articlesToRss(articles *[]*content.Article) *string {
const siteUrl = "http://www.ft.com/"
now := time.Now()
feed := &Feed{
Title: "Articles for narration from the Financial Times: FT Labs experiment",
Link: &Link{Href: siteUrl},
Description: "Exploring the text to speech possibilities.",
Author: &Author{Name: "Chris Gathercole", Email: "chris.gathercole@ft.com"},
Created: now,
Copyright: "This work is copyright © Financial Times",
}
feed.Items = []*Item{}
for _, article := range *articles {
created, _ := time.Parse("2006-01-02", article.PubDateString)
feed.Items = append(feed.Items, &Item{
Title: article.Title,
Link: &Link{Href: article.SiteUrl},
Description: article.Body,
Created: created,
Id: article.SiteUrl,
})
}
rss, _ := feed.ToRss()
return &rss
}
func GenerateRss(maxArticles int) *string {
articles := getFirstFTArticles( maxArticles )
rssString := articlesToRss( articles )
return rssString
}
func main() {
godotenv.Load()
// ...
}