-
Notifications
You must be signed in to change notification settings - Fork 6
/
newsapi.go
128 lines (110 loc) · 2.85 KB
/
newsapi.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
package main
import (
"github.com/biezhi/moe"
"encoding/json"
"log"
"github.com/olekukonko/tablewriter"
"os"
"reflect"
"sync"
"net/http"
"io/ioutil"
)
var (
wg sync.WaitGroup
lock sync.Mutex
)
// BaseArticle article
type BaseArticle struct {
Title string `json:"title,omitempty"`
URL string `json:"url,omitempty"`
Content string `json:"content,omitempty"`
}
// ShowNews show news
func ShowNews(q string) {
tip := "loading all news..."
url := "http://reader.one/api/all/hn,reddit,ph,dn,github,medium,lifehacker?limit=20"
if q != "" {
tip = "loading " + q + " ..."
url = "http://reader.one/api/news/" + q + "?limit=20"
}
moe := moe.New(tip).Color(moe.Green).Start()
body := GetRequestBody(url)
var resp []BaseArticle
if err := json.Unmarshal(body, &resp); err != nil {
log.Fatalln("load news fail")
return
}
items := mapToString(resp, "URL")
shortUrls := shortURLItems(items)
moe.Stop()
render(resp, shortUrls)
}
// render articles to console
func render(articles []BaseArticle, shortUrls map[string]*string) {
table := tablewriter.NewWriter(os.Stdout)
table.SetColWidth(70)
table.SetHeader([]string{"Title", "URL"})
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetBorder(false)
table.SetHeaderColor(tablewriter.Colors{tablewriter.Bold, tablewriter.BgBlackColor},
tablewriter.Colors{tablewriter.Bold, tablewriter.BgBlackColor})
table.SetColumnColor(tablewriter.Colors{tablewriter.Bold, tablewriter.FgRedColor},
tablewriter.Colors{tablewriter.Bold, tablewriter.FgBlueColor})
for _, article := range articles {
URL := shortUrls[article.URL]
Title := article.Title
row := []string{Title, *URL}
table.Append(row)
}
table.Render()
}
// GetShortURL get sina short url
func GetShortURL(url string) string {
reqURL := "http://api.t.sina.com.cn/short_url/shorten.json?source=3271760578&url_long=" + url
if url == "" {
return ""
}
var urlArr []map[string]interface{}
body := GetRequestBody(reqURL)
if err := json.Unmarshal(body, &urlArr); err != nil {
return ""
}
return urlArr[0]["url_short"].(string)
}
// GetRequestBody http get body
func GetRequestBody(reqURL string) []byte {
resp, err := http.Get(reqURL)
if err != nil {
return nil
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
return body
}
func mapToString(struts []BaseArticle, fieldName string) []string {
result := make([]string, len(struts))
for _, item := range struts {
v := reflect.ValueOf(item)
f := v.FieldByName(fieldName)
result = append(result, f.String())
}
return result
}
func shortURLItems(longUrls []string) map[string]*string {
shortUrls := make(map[string]*string, len(longUrls))
for _, url := range longUrls {
wg.Add(1)
go func(url string) {
lock.Lock()
defer func() {
lock.Unlock()
wg.Done()
}()
URL := GetShortURL(url)
shortUrls[url] = &URL
}(url)
}
wg.Wait()
return shortUrls
}