Skip to content

Commit c7165a0

Browse files
committed
Add news
1 parent e45ef19 commit c7165a0

File tree

2 files changed

+123
-4
lines changed

2 files changed

+123
-4
lines changed

pkg/posts/hot.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ const (
2121
// API URL's
2222
var (
2323
hotPostsAPI = fmt.Sprintf("%s/posts/%s", rootAPIURL, "hot")
24-
newsAPI = fmt.Sprintf("%s/posts/%s", rootAPIURL, "news")
2524
postAPI = fmt.Sprintf("%s/post", rootAPIURL)
2625
)
2726

pkg/posts/news.go

Lines changed: 123 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,134 @@ import (
44
"encoding/json"
55
"fmt"
66
"log"
7+
"os"
8+
"strconv"
9+
"time"
10+
11+
"github.com/rivo/tview"
12+
)
13+
14+
var (
15+
newsAPI = fmt.Sprintf("%s/posts/%s", rootAPIURL, "news")
716
)
817

918
func GetNews() {
1019
b, err := makeRequest(newsAPI)
20+
if err != nil {
21+
log.Printf("Oops, some network error: %v\n", err)
22+
os.Exit(0)
23+
}
24+
var posts TopNews
25+
err = json.Unmarshal(b, &posts)
1126
if err != nil {
1227
log.Println(err)
1328
}
14-
m := make(map[string]interface{})
15-
json.Unmarshal(b, &m)
16-
fmt.Println(m)
29+
30+
list := tview.NewList()
31+
app := tview.NewApplication()
32+
33+
for ind, post := range posts.Posts {
34+
35+
list = list.AddItem(post.Title, post.Brief, rune(strconv.Itoa(ind)[0]), nil)
36+
}
37+
38+
list.AddItem("Quit", "Press to exit", 'q', func() {
39+
app.Stop()
40+
os.Exit(0)
41+
})
42+
43+
list.SetSelectedFunc(func(runeindex int, title string, desc string, r rune) {
44+
if r != 'q' {
45+
n, err := strconv.Atoi(string(r))
46+
if err != nil {
47+
app.Stop()
48+
panic(err)
49+
}
50+
51+
openPost(app, posts.Posts[n].Cuid, list)
52+
53+
}
54+
})
55+
if err := app.SetRoot(list, true).SetFocus(list).Run(); err != nil {
56+
app.Stop()
57+
panic(err)
58+
}
59+
60+
}
61+
62+
type TopNews struct {
63+
Posts []struct {
64+
ID string `json:"_id"`
65+
IsFollowing bool `json:"isFollowing"`
66+
FollowersCount int `json:"followersCount"`
67+
Cuid string `json:"cuid"`
68+
Slug string `json:"slug"`
69+
Author struct {
70+
ID string `json:"_id"`
71+
Username string `json:"username"`
72+
Name string `json:"name"`
73+
Photo string `json:"photo"`
74+
Tagline string `json:"tagline"`
75+
IsEvangelist bool `json:"isEvangelist"`
76+
BadgesAwarded []interface{} `json:"badgesAwarded"`
77+
TotalUpvotesReceived int `json:"totalUpvotesReceived"`
78+
Appreciations []struct {
79+
Badge string `json:"badge"`
80+
ID string `json:"_id"`
81+
Count int `json:"count"`
82+
} `json:"appreciations"`
83+
DateJoined time.Time `json:"dateJoined"`
84+
SocialMedia struct {
85+
Twitter string `json:"twitter"`
86+
Github string `json:"github"`
87+
Stackoverflow string `json:"stackoverflow"`
88+
Linkedin string `json:"linkedin"`
89+
Google string `json:"google"`
90+
Website string `json:"website"`
91+
} `json:"socialMedia"`
92+
StoriesCreated []interface{} `json:"storiesCreated"`
93+
NumFollowing int `json:"numFollowing"`
94+
NumFollowers int `json:"numFollowers"`
95+
IsDeactivated bool `json:"isDeactivated"`
96+
Location string `json:"location"`
97+
NumReactions int `json:"numReactions"`
98+
} `json:"author"`
99+
Title string `json:"title"`
100+
URL string `json:"url"`
101+
Type string `json:"type"`
102+
Host string `json:"host"`
103+
ReactionsByCurrentUser []interface{} `json:"reactionsByCurrentUser"`
104+
TotalReactions int `json:"totalReactions"`
105+
Reactions []struct {
106+
ID string `json:"_id"`
107+
Image string `json:"image"`
108+
Name string `json:"name"`
109+
} `json:"reactions"`
110+
BookmarkedIn []interface{} `json:"bookmarkedIn"`
111+
HasReward bool `json:"hasReward"`
112+
IsPublication bool `json:"isPublication"`
113+
Contributors []interface{} `json:"contributors"`
114+
IsActive bool `json:"isActive"`
115+
ResponseCount int `json:"responseCount"`
116+
DateAdded time.Time `json:"dateAdded"`
117+
Tags []struct {
118+
ID string `json:"_id"`
119+
Name string `json:"name"`
120+
Slug string `json:"slug"`
121+
IsApproved bool `json:"isApproved"`
122+
IsActive bool `json:"isActive"`
123+
MergedWith interface{} `json:"mergedWith,omitempty"`
124+
} `json:"tags"`
125+
Downvotes int `json:"downvotes"`
126+
Upvotes int `json:"upvotes"`
127+
TotalPollVotes int `json:"totalPollVotes"`
128+
PollOptions []interface{} `json:"pollOptions"`
129+
HasPolls bool `json:"hasPolls"`
130+
Brief string `json:"brief"`
131+
CoverImage string `json:"coverImage"`
132+
Views int `json:"views"`
133+
IsAnonymous bool `json:"isAnonymous"`
134+
IndexVotedByCurrentUser int `json:"indexVotedByCurrentUser"`
135+
OriginalURL string `json:"originalUrl"`
136+
} `json:"posts"`
17137
}

0 commit comments

Comments
 (0)