-
Notifications
You must be signed in to change notification settings - Fork 2
/
starter.go
99 lines (80 loc) · 2.65 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
package main
import (
"fmt"
"log"
"net/http"
"strings"
"encoding/json"
"os"
"strconv"
"github.com/gorilla/mux"
)
const (
errorRecover = "Could not retrieve app reviews"
errorParameterLimit = "Given parameter \"limit\" is not valid, it should be an integer"
)
func main() {
log.SetOutput(os.Stdout)
router := mux.NewRouter()
router.HandleFunc("/hitec/crawl/app-reviews/google-play/{package_name}/limit/{limit}", getAppReviews).Methods("GET")
router.HandleFunc("/hitec/crawl/app-reviews/google-play/static/", getAppReviewsStatic).Methods("GET")
log.Fatal(http.ListenAndServe(":9621", router))
}
// error handling
func recoverAPICall(w http.ResponseWriter, appReviewResponse AppReviewResponse) {
if r := recover(); r != nil {
log.Println("recovered from ", r)
appReviewResponse.Status = http.StatusInternalServerError
appReviewResponse.Error = errorRecover
}
}
// parsing of google play page to get reviews
func getAppReviews(writer http.ResponseWriter, request *http.Request) {
appReviewResponse := AppReviewResponse{}
appReviewResponse.Status = http.StatusOK
fmt.Println("Get Reviews")
defer recoverAPICall(writer, appReviewResponse)
// get request param
params := mux.Vars(request)
packageName := params["package_name"]
limit, limitError := strconv.Atoi(params["limit"])
if limitError == nil {
appReviewResponse.Reviews = Crawl(packageName, limit)
} else {
appReviewResponse.Status = http.StatusBadRequest
appReviewResponse.Error = errorParameterLimit
}
serveResponse(writer, appReviewResponse)
}
// parsing of given URL to get reviews
func getAppReviewsStatic(writer http.ResponseWriter, request *http.Request) {
appReviewResponse := AppReviewResponse{}
appReviewResponse.Status = http.StatusOK
fmt.Println("Get Reviews from given website")
defer recoverAPICall(writer, appReviewResponse)
queryParameter := request.URL.Query()
for parameter, value := range queryParameter {
if parameter == "target_url" {
var crawlError error = nil
url := strings.Join(value, "")
appReviewResponse.Reviews, crawlError = CrawlHtml(url)
if crawlError != nil {
appReviewResponse.Status = http.StatusBadRequest
appReviewResponse.Error = crawlError.Error()
}
break
}
}
serveResponse(writer, appReviewResponse)
}
// serves the generated content
func serveResponse(writer http.ResponseWriter, appReviewResponse AppReviewResponse) {
writer.Header().Set("Content-Type", "application/json")
writer.WriteHeader(appReviewResponse.Status)
encoder := json.NewEncoder(writer)
encoder.SetEscapeHTML(false)
errorEncoding := encoder.Encode(appReviewResponse)
if errorEncoding != nil {
appReviewResponse.Error = errorEncoding.Error()
}
}