-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfeedly_search.go
122 lines (100 loc) · 2.34 KB
/
feedly_search.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
// Copyright 2016 Egor Smolyakov. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package feedly_search
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"strings"
"github.com/olekukonko/tablewriter"
)
const SEARCH_API_URL = "https://cloud.feedly.com/v3/search/feeds"
type feed struct {
FeedID string `json:"feedId"`
Subscribers int `json:"subscribers"`
Title string `json:"title"`
Website string `json:"website"`
}
type response struct {
Results []feed `json:"results"`
Hint string `json:"hint,omitempty"`
Related []string `json:"related,omitempty"`
}
func Process(query, locale string, number int) {
resp, err := http.Get(constructURL(query, locale, number))
if err != nil {
resp.Body.Close()
log.Fatalln(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalln(err)
}
var r response
err = json.Unmarshal(body, &r)
if err != nil {
log.Fatalln(err)
}
if r.Hint != "" {
fmt.Println("Feedly hint:", r.Hint)
}
if len(r.Related) != 0 {
fmt.Println("Feedly related queries:", strings.Join(r.Related, ", "))
fmt.Println()
}
if len(r.Results) == 0 {
fmt.Println("No results.")
return
}
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Title", "Subscribers", "RSS"})
table.SetRowLine(true)
// Generate feeds list
for _, feed := range r.Results {
data := []string{
title(feed.Title, feed.Website),
subscribers(feed.Subscribers),
rssUrl(feed.FeedID),
}
table.Append(data)
}
table.Render()
}
func title(feedTitle, feedWebsite string) string {
r := ""
feedTitle = strings.TrimSpace(feedTitle)
if len(feedTitle) > 0 {
r = r + feedTitle
}
if len(feedWebsite) > 0 {
r = r + ", " + feedWebsite
}
return r
}
func rssUrl(feedID string) string {
return strings.Replace(feedID, "feed/", "", 1)
}
func subscribers(count int) string {
if s := strconv.Itoa(count); s != "" {
return s
}
return "-"
}
func constructURL(query, locale string, number int) string {
req, err := http.NewRequest("GET", SEARCH_API_URL, nil)
if err != nil {
log.Fatalln(err)
}
q := req.URL.Query()
q.Add("query", query)
q.Add("locale", locale)
q.Add("count", strconv.Itoa(number))
req.URL.RawQuery = q.Encode()
return req.URL.String()
}