-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.go
80 lines (67 loc) · 1.5 KB
/
utils.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
package main
import (
"log"
"net/http"
"net/http/httputil"
"strings"
"time"
"github.com/fatih/color"
)
var bold = color.New(color.Bold).SprintFunc()
func fetchPttFeed(url string) (Atom1, error) {
response, err := http.Get(url)
if err != nil {
log.Println("Error while fetching feed:", url, "\n", err)
return Atom1{}, err
}
defer response.Body.Close()
dump, err := httputil.DumpResponse(response, true)
if err != nil {
log.Println("Error while dumping feed:", url, "\n", err)
return Atom1{}, err
}
feed, err := parseAtom(dump)
if err != nil {
log.Println("Error while parsing feed")
}
return feed, nil
}
func parsePttTime(timeStr string) (time.Time, error) {
loc, _ := time.LoadLocation("Asia/Taipei")
var layout = "2006-01-02T15:04:05-07:00"
t, err := time.ParseInLocation(layout, strings.TrimSuffix(timeStr, "Z"), loc)
if err != nil {
log.Println("Can't parse updated time", err)
return time.Time{}, err
}
return t, nil
}
func filteredAny(str string, filters []Filter) bool {
if len(filters) == 0 {
return true
}
for _, filter := range filters {
// Default to true (for filter == "")
matched := true
keywords := strings.Fields(string(filter))
for _, kw := range keywords {
match := strings.Contains(strings.ToLower(str), strings.ToLower(kw))
if !match {
matched = false
break
}
}
if matched {
return true
}
}
return false
}
func contains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}