-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
135 lines (126 loc) · 3.6 KB
/
main.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
129
130
131
132
133
134
135
package main
import (
"flag"
"fmt"
"log"
"net/http"
"strings"
"time"
)
var (
timeToExpire time.Duration
)
func main() {
// Parse port from command-line parameters
port := flag.String("port", "8080", "HTTP Port to listen on")
flag.Parse()
// declare variables
timeToExpire = 10 * time.Minute
// Start our Server
log.Println("Starting Server on", *port)
http.HandleFunc("/", index)
http.HandleFunc("/news", news)
log.Fatal(http.ListenAndServe(":"+*port, nil))
}
func index(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Slacker News, a slack integration to provide current news. Check it out at www.github.com/munrocape/slacker-news"))
}
func news(w http.ResponseWriter, r *http.Request) {
news_source := r.URL.Query().Get("text")
log.Println(news_source)
tokens := strings.Split(strings.ToLower(news_source), " ")
var source, argument string
if len(tokens) == 2 {
source, argument = tokens[0], tokens[1]
} else {
source, argument = tokens[0], ""
}
switch {
case source == "hn":
stories, err := getHnTop10()
if err == nil {
w.Write([]byte(stories))
} else {
w.Write([]byte("Server Error - Firebase could not be reached"))
}
return
case source == "ph":
posts, err := GetPhTop10()
if err == nil {
w.Write([]byte(posts))
} else {
w.Write([]byte("Server Error - Product Hunt could not be reached"))
}
return
case source == "vice":
articles, err := GetViceTop10()
if err == nil {
w.Write([]byte(articles))
} else {
w.Write([]byte("Server Error - Vice News could not be reached"))
}
return
case source == "bbc":
articles, err := GetBbcTop10(argument)
if err == nil {
w.Write([]byte(articles))
} else {
if strings.Contains(err.Error(), "Invalid feed selection") {
response := fmt.Sprintf("That is an invalid BBC category: %s\nTry `/news help` to view all sources.", argument)
w.Write([]byte(response))
} else {
w.Write([]byte("Server Error - the BBC could not be reached"))
}
}
return
case source == "538":
articles, err := GetFteTop10(argument)
if err == nil {
w.Write([]byte(articles))
} else {
if strings.Contains(err.Error(), "Invalid feed selection") {
response := fmt.Sprintf("That is an invalid FiveThirtyEight category: %s\nTry `/news help` to view all sources.", argument)
w.Write([]byte(response))
} else {
w.Write([]byte("Server Error - FiveThirtyEight could not be reached"))
}
}
return
case source == "dn":
stories, err := GetDnArgument(argument)
if err == nil {
w.Write([]byte(stories))
} else {
if strings.Contains(err.Error(), "Invalid argument"){
w.Write([]byte("Invalid argument - try `/news help` to view valid selections."))
} else {
w.Write([]byte("Server Error - Designer News could not be reached"))
}
}
return
case source == "help":
w.Write([]byte(GetSources()))
return
case source == "":
w.Write([]byte(GetSources()))
return
}
user_argument := fmt.Sprintf("%s %s", source, argument)
w.Write([]byte("Hmm.. I can't figure out what news you are looking for :( I received \"" + strings.TrimSpace(user_argument) + "\"\nTry `/news help` to view all sources."))
}
func GetSources() string {
hn := "Hacker News: hn\n"
ph := "Product Hunt: ph\n"
vice := "Vice News: vice\n"
designer := "Designer News: dn [stories, motd]\n"
fte := "FiveThirtyEight: 538 [" + GetFteSources() + "]\n"
bbc := "BBC: bbc [" + GetBbcSources() + "]\n"
return fmt.Sprintf("%s%s%s%s%s%s", hn, ph, vice, designer, fte, bbc)
}
func ExpiredResponse(timestamp time.Time) bool {
timeSinceLast := time.Since(timestamp)
if timeSinceLast > timeToExpire {
return true
}
return false
}