-
Notifications
You must be signed in to change notification settings - Fork 2
/
starter.go
168 lines (141 loc) · 4.93 KB
/
starter.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"strconv"
"github.com/ChimeraCoder/anaconda"
"github.com/gorilla/mux"
)
func main() {
log.SetOutput(os.Stdout)
log.Fatal(http.ListenAndServe(":9624", makeRouter()))
}
func makeRouter() *mux.Router {
router := mux.NewRouter()
router.HandleFunc("/hitec/crawl/tweets/{account_name}/exists", getAccountNameExists).Methods("GET")
router.HandleFunc("/hitec/crawl/tweets/mention/{account_name}/history-in-days/{days}/lang/{lang}", getTweetsFromAccountByDays).Methods("GET")
router.HandleFunc("/hitec/crawl/tweets/mention/{account_name}/from/{date}/lang/{lang}", getTweetsFromDate).Methods("GET")
router.HandleFunc("/hitec/crawl/tweets/mention/{account_name}/lang/{lang}", getTweetsInLang).Methods("GET")
router.HandleFunc("/hitec/crawl/tweets/mention/{account_name}/lang/{lang}/fast", getTweetsInLangFast).Methods("GET")
router.HandleFunc("/hitec/crawl/tweets/hashtag/{hashtag}/lang/{lang}", getTweetsWithHashtagInLang).Methods("GET")
return router
}
func getAccountNameExists(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
accountName := params["account_name"]
accountNameExists := AccountNameExists(accountName)
w.Header().Set("Content-Type", "application/json")
response := ResponseMessage{}
response = response.Create(accountNameExists, accountName)
fmt.Println("response", response)
err := json.NewEncoder(w).Encode(response)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
} else {
w.WriteHeader(http.StatusOK)
}
}
func getTweetsFromAccountByDays(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
accountName := params["account_name"]
lang := params["lang"]
days, err := strconv.Atoi(params["days"])
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
var anacondaTweets []anaconda.Tweet
timeFrame := TimeFrameFromDays(days)
log.Printf("getTweetsFromAccountByDays for timeframe%v, and account %v\n", timeFrame, accountName)
if timeFrame.IsValid() {
anacondaTweets = Crawl(lang, timeFrame, accountName, false)
}
tweets := TweetsFromAnacondaCrawler(anacondaTweets)
w.Header().Set("Content-Type", "application/json")
err = json.NewEncoder(w).Encode(tweets)
if err != nil {
log.Printf("ERR cannot encode the following tweets %v\n", err)
log.Println(tweets)
w.WriteHeader(http.StatusInternalServerError)
} else {
w.WriteHeader(http.StatusOK)
}
}
func getTweetsFromDate(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
accountName := params["account_name"]
since := params["date"]
lang := params["lang"]
var anacondaTweets []anaconda.Tweet
timeFrame := TimeFrameFromSince(since)
if timeFrame.IsValid() {
anacondaTweets = Crawl(lang, timeFrame, accountName, false)
}
tweets := TweetsFromAnacondaCrawler(anacondaTweets)
w.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(tweets)
if err != nil {
log.Printf("ERR cannot encode the following tweets %v\n", err)
log.Println(tweets)
w.WriteHeader(http.StatusInternalServerError)
} else {
w.WriteHeader(http.StatusOK)
}
}
func getTweetsInLang(w http.ResponseWriter, r *http.Request) {
fmt.Println("getTweetsInLang called")
params := mux.Vars(r)
accountName := params["account_name"]
lang := params["lang"]
var anacondaTweets []anaconda.Tweet
timeFrame := TimeFrame{}
anacondaTweets = Crawl(lang, timeFrame, accountName, true)
tweets := TweetsFromAnacondaCrawler(anacondaTweets)
w.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(tweets)
if err != nil {
log.Printf("ERR cannot encode the following tweets %v\n", err)
log.Println(tweets)
w.WriteHeader(http.StatusInternalServerError)
} else {
w.WriteHeader(http.StatusOK)
}
}
func getTweetsInLangFast(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
accountName := params["account_name"]
lang := params["lang"]
log.Printf("getTweetsInLangFast called, for account %v\n", accountName)
var anacondaTweets []anaconda.Tweet
timeFrame := TimeFrame{}
anacondaTweets = Crawl(lang, timeFrame, accountName, false)
tweets := TweetsFromAnacondaCrawler(anacondaTweets)
w.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(tweets)
if err != nil {
log.Printf("ERR cannot encode the following tweets %v\n", err)
log.Println(tweets)
w.WriteHeader(http.StatusInternalServerError)
} else {
w.WriteHeader(http.StatusOK)
}
}
func getTweetsWithHashtagInLang(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
hashtag := params["hashtag"]
lang := params["lang"]
anacondaTweets := CrawlForHashtags(lang, []string{hashtag})
tweets := TweetsFromAnacondaCrawler(anacondaTweets)
w.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(tweets)
if err != nil {
log.Printf("ERR cannot encode the following tweets %v\n", err)
log.Println(tweets)
w.WriteHeader(http.StatusInternalServerError)
} else {
w.WriteHeader(http.StatusOK)
}
}