-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.go
121 lines (92 loc) · 2.42 KB
/
helper.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
package main
import (
"encoding/json"
"io/ioutil"
"log"
"net/url"
"os"
"os/user"
"sort"
"strings"
)
// filterSources add listSources ids to query string.
// The ids will be added to "s" param.
// If listSources is nil, mainFeedURL url will be returned
func filterSources(inputURL string, listSources []string) *url.URL {
currentURL, err := url.Parse(inputURL)
if err != nil {
return nil
}
if len(listSources) == 0 {
return currentURL
}
params := currentURL.Query()
params.Add("s", strings.Join(listSources, ","))
currentURL.RawQuery = params.Encode()
return currentURL
}
func feedByCategory(feeds []*feed) map[string][]*feed {
orderedFeed := make(map[string][]*feed)
for _, currentFeed := range feeds {
if _, ok := orderedFeed[currentFeed.Category]; !ok {
orderedFeed[currentFeed.Category] = make([]*feed, 0)
}
orderedFeed[currentFeed.Category] = append(orderedFeed[currentFeed.Category], currentFeed)
}
return orderedFeed
}
func orderFeedByName(feeds map[string][]*feed) map[string][]*feed {
sortedFeeds := make(map[string][]*feed, len(feeds))
for index, value := range feeds {
sort.Slice(value, func(i, j int) bool {
return value[i].Title < value[j].Title
})
sortedFeeds[index] = value
}
return sortedFeeds
}
func loadSettings(filename string) *preference {
p := &preference{}
tmpUser, err := user.Current()
if err != nil {
log.Println("Unable to determinate the home directory", err)
return p
}
filename = strings.Join([]string{tmpUser.HomeDir, filename}, string(os.PathSeparator))
file, err := os.Open(filename)
if err != nil {
log.Println("Unable to open setting file", filename)
return p
}
defer file.Close()
data, err := ioutil.ReadAll(file)
if err != nil {
log.Println("Unable to read setting file", err)
return p
}
json.Unmarshal(data, p)
if err != nil {
log.Println("Unable to parse the json", err)
return p
}
return p
}
func saveSettings(filename string, p *preference) bool {
tmpUser, err := user.Current()
if err != nil {
log.Println("Unable to determinate the home directory", err)
return false
}
filename = strings.Join([]string{tmpUser.HomeDir, filename}, string(os.PathSeparator))
data, err := json.Marshal(p)
if err != nil {
log.Println("Unable to marshal settings", err)
return false
}
err = ioutil.WriteFile(filename, data, 0600)
if err != nil {
log.Println("Unable to save the settings", err)
return false
}
return true
}