Skip to content

Commit 65ebcf0

Browse files
authored
Merge pull request #38 from circa10a/fix-news-year-parsing
Fix news year parsing
2 parents f252861 + 4e98ac7 commit 65ebcf0

File tree

8 files changed

+1862
-36
lines changed

8 files changed

+1862
-36
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ GOCMD=go
22
BINARY=awsnews
33
BUILD_FLAGS=-ldflags="-s -w"
44
PROJECT=circa10a/go-aws-news
5-
VERSION=0.3.2
5+
VERSION=0.4.0
66

77
# First target for travis ci
88
test:

README.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ Fetch what's new from AWS and send out notifications on social sites.
2020
- [Get Today's news](#get-todays-news)
2121
- [Get Yesterday's news](#get-yesterdays-news)
2222
- [Get all news for the month](#get-all-news-for-the-month)
23-
- [Gets from a previous month](#gets-from-a-previous-month)
23+
- [Get from a previous month](#get-from-a-previous-month)
24+
- [Get from a previous year](#get-from-a-previous-year)
2425
- [Print out announcements](#print-out-announcements)
2526
- [Loop over news data](#loop-over-news-data)
27+
- [Limit news results count](#limit-news-results-count)
2628
- [Get news as JSON](#get-news-as-json)
2729
- [Get news as HTML](#get-news-as-html)
2830
- [Get news about a specific product](#get-news-about-a-specific-product)
@@ -150,13 +152,20 @@ news, _ := awsnews.Yesterday()
150152
news, _ := awsnews.ThisMonth()
151153
```
152154

153-
#### Gets from a previous month
155+
#### Get from a previous month
154156

155157
```go
156158
// Custom timeframe(June 2019)
157159
news, err := awsnews.Fetch(2019, 06)
158160
```
159161

162+
#### Get from a previous year
163+
164+
```go
165+
// Custom timeframe(2017)
166+
news, err := awsnews.FetchYear(2017)
167+
```
168+
160169
#### Print out announcements
161170

162171
```go
@@ -188,6 +197,14 @@ for _, v := range news {
188197
}
189198
```
190199

200+
#### Limit news results count
201+
202+
```go
203+
news, _ := awsnews.ThisMonth()
204+
// Last 10 news items of the month
205+
news.Last(10).Print()
206+
```
207+
191208
#### Get news as JSON
192209

193210
```go

news/announcements.go

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,42 +39,44 @@ func (n newsDoc) GetAnnouncements() (Announcements, error) {
3939
return announcements, nil
4040
}
4141

42-
// Fetch Fetches all of the announcements for the specified year/month that was input.
42+
// Fetch gets all of the announcements for the specified year/month that was input.
4343
func Fetch(year int, month int) (Announcements, error) {
44-
doc, err := getNewsDoc(year, month)
44+
doc, err := getNewsDocMonth(year, month)
4545
if err != nil {
4646
return Announcements{}, err
4747
}
4848
return newsDoc{doc}.GetAnnouncements()
4949
}
5050

51-
// Extract date from amazon's format
52-
// Input: Posted on: Jan 7, 2020
53-
// Output: Jan 7, 2020
54-
// parseDate Extracts a standarized date format from the AWS html document.
55-
func parseDate(postDate string) string {
56-
r, _ := regexp.Compile("[A-Z][a-z]{2}\\s[0-9]{1,2},\\s[0-9]{4}")
57-
// AWS sometimes doesn't have a post date
58-
if len(r.FindStringSubmatch(postDate)) > 0 {
59-
return r.FindStringSubmatch(postDate)[0]
51+
// FetchYear gets all of the announcements for the specified year that was input.
52+
func FetchYear(year int) (Announcements, error) {
53+
doc, err := getNewsDocYear(year)
54+
if err != nil {
55+
return Announcements{}, err
6056
}
61-
return "No posted date"
57+
return newsDoc{doc}.GetAnnouncements()
6258
}
6359

6460
// ThisMonth gets the current month's AWS announcements.
6561
func ThisMonth() (Announcements, error) {
62+
var thisMonthsAnnouncements Announcements
6663
currentTime := time.Now()
67-
news, err := Fetch(currentTime.Year(), int(currentTime.Month()))
64+
news, err := FetchYear(currentTime.Year())
65+
for _, announcement := range news {
66+
if announcement.PostDate[:3] == currentTime.Month().String()[:3] {
67+
thisMonthsAnnouncements = append(thisMonthsAnnouncements, announcement)
68+
}
69+
}
6870
if err != nil {
6971
return news, err
7072
}
71-
return news, nil
73+
return thisMonthsAnnouncements, nil
7274
}
7375

7476
// Today gets today's AWS announcements.
7577
func Today() (Announcements, error) {
7678
var todaysAnnouncements Announcements
77-
news, err := ThisMonth()
79+
news, err := FetchYear(time.Now().Year())
7880
if err != nil {
7981
return news, err
8082
}
@@ -90,7 +92,7 @@ func Today() (Announcements, error) {
9092
// Yesterday gets yesterday's AWS announcments.
9193
func Yesterday() (Announcements, error) {
9294
var yesterdaysAnnouncements Announcements
93-
news, err := ThisMonth()
95+
news, err := FetchYear(time.Now().Year())
9496
if err != nil {
9597
return news, err
9698
}
@@ -103,6 +105,19 @@ func Yesterday() (Announcements, error) {
103105
return yesterdaysAnnouncements, nil
104106
}
105107

108+
// Extract date from amazon's format
109+
// Input: Posted on: Jan 7, 2020
110+
// Output: Jan 7, 2020
111+
// parseDate Extracts a standarized date format from the AWS html document.
112+
func parseDate(postDate string) string {
113+
r, _ := regexp.Compile("[A-Z][a-z]{2}\\s[0-9]{1,2},\\s[0-9]{4}")
114+
// AWS sometimes doesn't have a post date
115+
if len(r.FindStringSubmatch(postDate)) > 0 {
116+
return r.FindStringSubmatch(postDate)[0]
117+
}
118+
return "No posted date"
119+
}
120+
106121
// Print Prints out an ASCII table of your selection of AWS announcements.
107122
func (a Announcements) Print() {
108123
data := [][]string{}
@@ -122,6 +137,14 @@ func (a Announcements) Print() {
122137
table.Render()
123138
}
124139

140+
// Last returns a set number of news items you specify
141+
func (a Announcements) Last(n int) Announcements {
142+
if len(a) > n {
143+
return a[:n]
144+
}
145+
return a
146+
}
147+
125148
// JSON Converts Announcements to JSON.
126149
func (a Announcements) JSON() ([]byte, error) {
127150
json, err := json.Marshal(a)

news/announcements_test.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ func TestFetch(t *testing.T) {
1515
assert.Equal(t, len(news), 100)
1616
}
1717

18+
// Integration test
19+
func TestFetchYear(t *testing.T) {
20+
news, err := FetchYear(2020)
21+
assert.NoError(t, err)
22+
assert.Greater(t, len(news), 0)
23+
}
24+
1825
func TestParseDate(t *testing.T) {
1926
assert.Equal(t, "Jan 1, 2020", parseDate("Posted on: Jan 1, 2020"))
2027
}
@@ -53,22 +60,28 @@ func TestYesterday(t *testing.T) {
5360
}
5461
}
5562

63+
func TestLast(t *testing.T) {
64+
news, err := newsDoc{monthTestDoc}.GetAnnouncements()
65+
news = news.Last(5)
66+
assert.NoError(t, err)
67+
assert.Equal(t, len(news), 5)
68+
}
5669
func TestJSON(t *testing.T) {
57-
news, err := newsDoc{testDoc}.GetAnnouncements()
70+
news, err := newsDoc{monthTestDoc}.GetAnnouncements()
5871
assert.NoError(t, err)
5972
_, jsonErr := news.JSON()
6073
assert.NoError(t, jsonErr)
6174
}
6275

6376
func TestHTML(t *testing.T) {
64-
news, err := newsDoc{testDoc}.GetAnnouncements()
77+
news, err := newsDoc{monthTestDoc}.GetAnnouncements()
6578
assert.NoError(t, err)
6679
data := []byte(news.HTML())
6780
assert.True(t, xml.Unmarshal(data, new(interface{})) == nil)
6881
}
6982

7083
func TestFilter(t *testing.T) {
71-
news, _ := newsDoc{testDoc}.GetAnnouncements()
84+
news, _ := newsDoc{monthTestDoc}.GetAnnouncements()
7285
filteredNews := news.Filter([]string{"EKS", "ECS"})
7386
assert.Equal(t, len(filteredNews), 6)
7487
}

news/examples_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ func ExampleFetch() {
1010
news.Print()
1111
}
1212

13+
func ExampleFetchYear() {
14+
news, err := FetchYear(2020)
15+
if err != nil {
16+
// Handle error
17+
}
18+
news.Print()
19+
}
20+
1321
func ExampleThisMonth() {
1422
news, err := ThisMonth()
1523
if err != nil {
@@ -36,6 +44,15 @@ func ExampleYesterday() {
3644
fmt.Println(news)
3745
}
3846

47+
func (a Announcements) ExampleLast() {
48+
news, err := ThisMonth()
49+
if err != nil {
50+
// Handle error
51+
}
52+
// Show last 10 news items of the month
53+
news.Last(10).Print()
54+
}
55+
3956
func (a Announcements) ExampleJSON() {
4057
news, err := ThisMonth()
4158
if err != nil {

news/newsdoc.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,18 @@ import (
66
"github.com/PuerkitoBio/goquery"
77
)
88

9-
// getNewsDoc Fetches html from AWS depending on the year/month specified.
10-
func getNewsDoc(year int, month int) (*goquery.Document, error) {
9+
// getNewsDocYear Fetches html from AWS depending on the year/month specified.
10+
func getNewsDocYear(year int) (*goquery.Document, error) {
11+
url := fmt.Sprintf("https://aws.amazon.com/about-aws/whats-new/%d", year)
12+
doc, err := goquery.NewDocument(url)
13+
if err != nil {
14+
return doc, err
15+
}
16+
return doc, nil
17+
}
18+
19+
// getNewsDocMonth Fetches html from AWS depending on the year/month specified.
20+
func getNewsDocMonth(year int, month int) (*goquery.Document, error) {
1121
url := fmt.Sprintf("https://aws.amazon.com/about-aws/whats-new/%v/%02d/", year, month)
1222
doc, err := goquery.NewDocument(url)
1323
if err != nil {

news/newsdoc_test.go

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,45 +9,66 @@ import (
99
"github.com/stretchr/testify/assert"
1010
)
1111

12-
// Reusable html document
13-
var testDoc *goquery.Document
12+
// Reusable html documents
13+
var monthTestDoc *goquery.Document
14+
var yearTestDoc *goquery.Document
1415

1516
func init() {
1617
// create from a file
17-
f, err := os.Open("./resources/aws-news-2019-12.html")
18+
// Monthly Testing
19+
monthFile, err := os.Open("./resources/aws-news-2019-12.html")
1820
if err != nil {
1921
log.Fatal(err)
2022
}
21-
defer f.Close()
22-
doc, err := goquery.NewDocumentFromReader(f)
23+
defer monthFile.Close()
24+
monthDoc, err := goquery.NewDocumentFromReader(monthFile)
2325
if err != nil {
2426
log.Fatal(err)
2527
}
26-
testDoc = doc
28+
monthTestDoc = monthDoc
29+
30+
// create from a file
31+
// yearly Testing
32+
yearFile, err := os.Open("./resources/aws-news-2020.html")
33+
if err != nil {
34+
log.Fatal(err)
35+
}
36+
defer yearFile.Close()
37+
yearDoc, err := goquery.NewDocumentFromReader(yearFile)
38+
if err != nil {
39+
log.Fatal(err)
40+
}
41+
yearTestDoc = yearDoc
2742
}
2843

2944
// Integration test
30-
func TestGetNewsDoc(t *testing.T) {
31-
_, err := getNewsDoc(2019, 12)
45+
func TestGetNewsDocMonth(t *testing.T) {
46+
_, err := getNewsDocMonth(2019, 12)
47+
assert.NoError(t, err)
48+
}
49+
50+
// Integration
51+
func TestGetNewsDocYear(t *testing.T) {
52+
_, err := getNewsDocYear(2020)
3253
assert.NoError(t, err)
3354
}
3455

3556
func TestGetSelectionItems(t *testing.T) {
36-
news := newsDoc{testDoc}
57+
news := newsDoc{monthTestDoc}
3758
assert.Equal(t, news.getSelectionItems().Length(), 100)
3859
}
3960

4061
func TestGetSelectionTitle(t *testing.T) {
41-
news := newsDoc{testDoc}
62+
news := newsDoc{monthTestDoc}
4263
assert.Equal(t, news.getSelectionTitle(news.getSelectionItems().First()), "Amazon EKS Announces Beta Release of Amazon FSx for Lustre CSI Driver")
4364
}
4465

4566
func TestGetSelectionLink(t *testing.T) {
46-
news := newsDoc{testDoc}
67+
news := newsDoc{monthTestDoc}
4768
assert.Equal(t, news.getSelectionItemLink(news.getSelectionItems().First()), "https://aws.amazon.com/about-aws/whats-new/2019/12/amazon-eks-announces-beta-release-amazon-fsx-lustre-csi-driver/")
4869
}
4970

5071
func TestGetSelectionDate(t *testing.T) {
51-
news := newsDoc{testDoc}
72+
news := newsDoc{monthTestDoc}
5273
assert.Equal(t, news.getSelectionItemDate(news.getSelectionItems().First()), "\n Posted On: Dec 23, 2019 \n ")
5374
}

0 commit comments

Comments
 (0)