-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinshorts.go
97 lines (79 loc) · 2.13 KB
/
inshorts.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
package main
import (
"bytes"
"encoding/json"
"errors"
"net/http"
"text/template"
"time"
)
type NewsCategories string
const (
ALL_NEWS NewsCategories = "all_news"
TOP_NEWS NewsCategories = "top_stories"
TRENDING_NEWS NewsCategories = "trending"
)
const baseUrl = "https://inshorts.com/api/en/news"
var inshortsClient = http.Client{
Timeout: 10 * time.Second,
}
type InshortsNewsResponse struct {
Data InshortsNewsData
}
type InshortsNewsData struct {
MinNewsId string `json:"min_news_id"`
NewsList []InshortsNews `json:"news_list"`
}
type InshortsNews struct {
Type string
HashId string `json:"hash_id"`
NewsObject InshortsNewsObject `json:"news_obj"`
}
type InshortsNewsObject struct {
HashId string `json:"hash_id"`
Title string
Content string
Image string `json:"image_url"`
Author string `json:"author_name"`
URL string `json:"source_url"`
CreatedAt int64 `json:"created_at"`
}
func (r InshortsNewsResponse) GetLastNewsDate() string {
var date int64
if len(r.Data.NewsList) > 0 {
date = r.Data.NewsList[0].NewsObject.CreatedAt / 1000
} else {
date = time.Now().Unix()
}
return time.Unix(date, 0).Format(time.RFC822)
}
func (o InshortsNewsObject) GetCreatedAt() string {
return time.Unix(o.CreatedAt/1000, 0).Format(time.RFC822)
}
func (o InshortsNewsObject) GetMarkupContent() string {
t, _ := template.New("content").Parse(`
<img src="{{.Image}}" alt="{{.Title}}" style="max-width: 100%; height: auto;" />
<p>{{.Content}}</p>
`)
var b bytes.Buffer
t.Execute(&b, o)
return b.String()
}
func GetResults(category NewsCategories) (*InshortsNewsResponse, error) {
queryParams := "?include_card_data=true"
switch category {
case ALL_NEWS, TRENDING_NEWS, TOP_NEWS:
queryParams += "&category=" + string(category)
default:
queryParams += "&category=" + string(ALL_NEWS)
}
resp, err := inshortsClient.Get(baseUrl + queryParams)
if err != nil {
return nil, errors.New("Error getting result from inshorts")
}
defer resp.Body.Close()
var parsed InshortsNewsResponse
decoder := json.NewDecoder(resp.Body)
decoder.Decode(&parsed)
return &parsed, nil
}