diff --git a/.travis.yml b/.travis.yml index 56bc01a..aea415d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -30,6 +30,10 @@ before-install: script: + - curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh| sh -s -- -b $(go env GOPATH)/bin v1.18.0 + - echo "checking linting in golang ..." + - golangci-lint run + - echo "perfect linting standards! moving ahead ..." - make build - ./bin/service & - export ENV=test @@ -39,7 +43,7 @@ script: - diff -u <(echo -n) <(gofmt -d ./src ./tsdb) - echo "perfect go format. moving ahead ..." - cd view/web/ && npm start && cd ../.. - - go test service/controllers/controllers_test.go + - go test -v ./... - cd tests/ - npm install - xvfb-run -s "-screen 0 1920x1080x16" -a mocha test.js \ No newline at end of file diff --git a/service/bin/jarvis b/service/bin/jarvis deleted file mode 100755 index 5bcf434..0000000 Binary files a/service/bin/jarvis and /dev/null differ diff --git a/service/config/index.go b/service/config/index.go index dd43b8e..01ec8ac 100644 --- a/service/config/index.go +++ b/service/config/index.go @@ -20,10 +20,9 @@ var config Configuration // init reads and stores data from config.json func init() { ENV := os.Getenv("ENV") - fmt.Println("env is ", ENV) prefix := "" if ENV == "test" { - prefix = "../" + prefix = "../../" } jsonFile, err := os.Open(fmt.Sprintf("%sstatic/config.json", prefix)) @@ -35,7 +34,10 @@ func init() { if ioError != nil { logger.Error(ioError) } - json.Unmarshal(byteValue, &config) + if err := json.Unmarshal(byteValue, &config); err != nil { + panic(err) + } + fmt.Println("loaded configuration") defer jsonFile.Close() } diff --git a/service/controllers/ast.go b/service/controllers/ast.go index 1efb57a..7956859 100644 --- a/service/controllers/ast.go +++ b/service/controllers/ast.go @@ -75,7 +75,10 @@ func AST(routeObject response, w http.ResponseWriter) { } jData, _ := json.Marshal(responseJSON) - w.Write(jData) + if _, err := w.Write(jData); err != nil { + panic(err) + } + TextToSpeech(responseJSON.Message, 0) } else if matchPars == "yahoo" { @@ -96,7 +99,10 @@ func AST(routeObject response, w http.ResponseWriter) { } jData, _ := json.Marshal(responseJSON) - w.Write(jData) + if _, err := w.Write(jData); err != nil { + panic(err) + } + TextToSpeech(responseJSON.Message, 0) } else if matchPars == "bing" { @@ -118,7 +124,10 @@ func AST(routeObject response, w http.ResponseWriter) { } jData, _ := json.Marshal(responseJSON) - w.Write(jData) + if _, err := w.Write(jData); err != nil { + panic(err) + } + TextToSpeech(responseJSON.Message, 0) } else if matchPars == "youtube" || matchPars == "videos" || matchPars == "watch" { @@ -139,7 +148,10 @@ func AST(routeObject response, w http.ResponseWriter) { } jData, _ := json.Marshal(responseJSON) - w.Write(jData) + if _, err := w.Write(jData); err != nil { + panic(err) + } + TextToSpeech(responseJSON.Message, 0) } else if matchPars == "images" || matchPars == "image" { @@ -159,13 +171,18 @@ func AST(routeObject response, w http.ResponseWriter) { } jData, _ := json.Marshal(responseJSON) - w.Write(jData) + if _, err := w.Write(jData); err != nil { + panic(err) + } + TextToSpeech(responseJSON.Message, 0) } else if matchPars == "weather" { if len(messageArr) < 3 { - w.Write([]byte(`{"status": "success", "message": "ENTER: weather ", "result": ""}`)) + if _, err := w.Write([]byte(`{"status": "success", "message": "ENTER: weather ", "result": ""}`)); err != nil { + panic(err) + } } else { city, state := messageArr[len(messageArr)-2], messageArr[len(messageArr)-1] @@ -176,13 +193,18 @@ func AST(routeObject response, w http.ResponseWriter) { } jData, _ := json.Marshal(response) - w.Write(jData) + if _, err := w.Write(jData); err != nil { + panic(err) + } + TextToSpeech(response.Message+city+" "+state, 0) } } else if matchPars == "meaning" { if len(messageArr) == 1 { - w.Write([]byte(`{"status": "success", "message": "ENTER: meaning ", "result": ""}`)) + if _, err := w.Write([]byte(`{"status": "success", "message": "ENTER: meaning ", "result": ""}`)); err != nil { + panic(err) + } } else { wordStr := remainingString @@ -197,7 +219,10 @@ func AST(routeObject response, w http.ResponseWriter) { } jData, _ := json.Marshal(responseJSON) - w.Write(jData) + if _, err := w.Write(jData); err != nil { + panic(err) + } + TextToSpeech(responseJSON.Message+" "+filterForSpeech(wordStr), 0) } else { @@ -212,13 +237,18 @@ func AST(routeObject response, w http.ResponseWriter) { } jData, _ := json.Marshal(responseJSON) - w.Write(jData) + if _, err := w.Write(jData); err != nil { + panic(err) + } + TextToSpeech(responseJSON.Message+" "+filterForSpeech(wordStr), 0) } } } else if matchPars == "medicine" { if len(messageArr) <= 1 { - w.Write([]byte(`{"status": "success", "message": "ENTER: medicine ", "result": ""}`)) + if _, err := w.Write([]byte(`{"status": "success", "message": "ENTER: medicine ", "result": ""}`)); err != nil { + panic(err) + } } else { med := messageArr[len(messageArr)-1] result := messages.HealthMedController(med, w) @@ -228,16 +258,20 @@ func AST(routeObject response, w http.ResponseWriter) { // add support for multiple symptoms at once and use ML to determine the best medicine suited if len(messageArr) < 2 { - w.Write([]byte(`{"status": "success", "message": "ENTER: symptoms ", "result": ""}`)) + if _, err := w.Write([]byte(`{"status": "success", "message": "ENTER: symptoms ", "result": ""}`)); err != nil { + panic(err) + } + } else { - fmt.Println("inside") - symp := strings.Join(messageArr[1:len(messageArr)], " ") + symp := strings.Join(messageArr[1:], " ") result := messages.HealthSympController(symp, w) TextToSpeech(result, 0) } } else if strings.HasPrefix(strings.ToLower(message), "set reminder") { - w.Write([]byte(`{"status": "success", "message": "Enter Reminder details : ", "result": ""}`)) + if _, err := w.Write([]byte(`{"status": "success", "message": "Enter Reminder details : ", "result": ""}`)); err != nil { + panic(err) + } } else if strings.HasPrefix(strings.ToLower(message), "show reminder") { @@ -248,7 +282,10 @@ func AST(routeObject response, w http.ResponseWriter) { } jData, _ := json.Marshal(responseJSON) - w.Write(jData) + if _, err := w.Write(jData); err != nil { + panic(err) + } + TextToSpeech("Here are your reminders.", 0) } else if strings.HasPrefix(strings.ToLower(message), "deploy") { @@ -259,7 +296,9 @@ func AST(routeObject response, w http.ResponseWriter) { } else if strings.HasPrefix(strings.ToLower(message), "send mail") { - w.Write([]byte(`{"status": "success", "message": "Enter Mail details : ", "result": ""}`)) + if _, err := w.Write([]byte(`{"status": "success", "message": "Enter Mail details : ", "result": ""}`)); err != nil { + panic(err) + } } else { // general conversation @@ -275,12 +314,16 @@ func AST(routeObject response, w http.ResponseWriter) { matchPars == "image" || matchPars == "weather" || matchPars == "medicine" || matchPars == "symptoms" || strings.HasPrefix(message, "send mail") { - w.Write([]byte(`{"status": "success", "message": "Services unavailable at the moment ! Check your Internet Connection and try again.", "result": ""}`)) + if _, err := w.Write([]byte(`{"status": "success", "message": "Services unavailable at the moment ! Check your Internet Connection and try again.", "result": ""}`)); err != nil { + panic(err) + } TextToSpeech("Services unavailable at the moment!", 0) } else if strings.HasPrefix(message, "set reminder") { - w.Write([]byte(`{"status": "success", "message": "Enter Reminder details : ", "result": ""}`)) + if _, err := w.Write([]byte(`{"status": "success", "message": "Enter Reminder details : ", "result": ""}`)); err != nil { + panic(err) + } } else if strings.HasPrefix(message, "show reminder") { @@ -291,7 +334,10 @@ func AST(routeObject response, w http.ResponseWriter) { } jData, _ := json.Marshal(responseJSON) - w.Write(jData) + if _, err := w.Write(jData); err != nil { + panic(err) + } + TextToSpeech("Here are your reminders.", 0) } else { // Conversation. diff --git a/service/controllers/controllers.go b/service/controllers/controllers.go index b565c31..23e1ba2 100644 --- a/service/controllers/controllers.go +++ b/service/controllers/controllers.go @@ -24,7 +24,9 @@ func MessagesController(w http.ResponseWriter, r *http.Request) { w.Header().Set("Access-Control-Allow-Origin", "*") w.Header().Set("Access-Control-Allow-Headers", "Content-Type") - r.ParseForm() + if err := r.ParseForm(); err != nil { + panic(err) + } request := response{ username: r.FormValue("username"), @@ -49,39 +51,11 @@ func filterForSpeech(s string) string { } -// gives the difference of two string arrays as an array of the differed element -func stringDifference(slice1 []string, slice2 []string) []string { - var diff []string - - // Loop two times, first to find slice1 strings not in slice2, - // second loop to find slice2 strings not in slice1 - for i := 0; i < 2; i++ { - for _, s1 := range slice1 { - found := false - for _, s2 := range slice2 { - if s1 == s2 { - found = true - break - } - } - // String not found. We add it to return slice - if !found { - diff = append(diff, s1) - } - } - // Swap the slices, only if it was the first loop - if i == 0 { - slice1, slice2 = slice2, slice1 - } - } - - return diff -} - func buildGoogleUrls(searchTerm, countryCode, languageCode string, pages, count int) ([]string, error) { toScrape := []string{} searchTerm = strings.Trim(searchTerm, " ") searchTerm = strings.Replace(searchTerm, " ", "+", -1) + if googleBase, found := googleDomains[countryCode]; found { for i := 0; i < pages; i++ { start := i * count @@ -92,17 +66,20 @@ func buildGoogleUrls(searchTerm, countryCode, languageCode string, pages, count err := fmt.Errorf("country (%s) is currently not supported", countryCode) return nil, err } + return toScrape, nil } func googleResultParsing(response *http.Response, rank int) ([]messageQueryBody, error) { - doc, err := goquery.NewDocumentFromResponse(response) + doc, err := goquery.NewDocumentFromReader(response.Body) if err != nil { return nil, err } + results := []messageQueryBody{} sel := doc.Find("div.g") rank++ + for i := range sel.Nodes { item := sel.Eq(i) linkTag := item.Find("a") @@ -110,9 +87,11 @@ func googleResultParsing(response *http.Response, rank int) ([]messageQueryBody, descTag := item.Find("span.st") desc := descTag.Text() link = strings.Trim(link, " ") + if desc == "" { desc = link } + if link != "" && link != "#" && !strings.HasPrefix(link, "/") { result := messageQueryBody{ Head: desc, @@ -122,6 +101,7 @@ func googleResultParsing(response *http.Response, rank int) ([]messageQueryBody, rank++ } } + return results, err } @@ -129,6 +109,7 @@ func buildBingUrls(searchTerm, country string, pages, count int) ([]string, erro toScrape := []string{} searchTerm = strings.Trim(searchTerm, " ") searchTerm = strings.Replace(searchTerm, " ", "+", -1) + if countryCode, found := bingDomains[country]; found { for i := 0; i < pages; i++ { first := firstParameter(i, count) @@ -139,6 +120,7 @@ func buildBingUrls(searchTerm, country string, pages, count int) ([]string, erro err := fmt.Errorf("country (%s) is currently not supported", country) return nil, err } + return toScrape, nil } @@ -150,13 +132,15 @@ func firstParameter(number, count int) int { } func bingResultParser(response *http.Response, rank int) ([]messageQueryBody, error) { - doc, err := goquery.NewDocumentFromResponse(response) + doc, err := goquery.NewDocumentFromReader(response.Body) if err != nil { return nil, err } + results := []messageQueryBody{} sel := doc.Find("li.b_algo") rank++ + for i := range sel.Nodes { item := sel.Eq(i) linkTag := item.Find("a") @@ -164,9 +148,11 @@ func bingResultParser(response *http.Response, rank int) ([]messageQueryBody, er descTag := item.Find("div.b_caption p") desc := descTag.Text() link = strings.Trim(link, " ") + if desc == "" { desc = link } + if link != "" && link != "#" && !strings.HasPrefix(link, "/") { result := messageQueryBody{ Head: desc, @@ -181,28 +167,35 @@ func bingResultParser(response *http.Response, rank int) ([]messageQueryBody, er func meanings(word string) []meaningStr { url := "https://en.oxforddictionaries.com/definition/" + word + res, err := scrapper.ScrapeClientRequest(url, nil) if err != nil { logger.Error(err) } - doc, err := goquery.NewDocumentFromResponse(res) + + doc, err := goquery.NewDocumentFromReader(res.Body) if err != nil { logger.Error(err) } + var resultObj []meaningStr sel := doc.Find("section.gramb") + for sectionIndex := range sel.Nodes { - fmt.Println(sectionIndex) section := sel.Eq(sectionIndex) typArray := section.Find("span.pos") + for typIndex := range typArray.Nodes { fmt.Println(typArray.Eq(typIndex).Text()) } + trgArray := section.Find("div.trg") + for trgIndex := range trgArray.Nodes { var meaning meaningStr trg := trgArray.Eq(trgIndex) paraArray := trg.Find("p") + for paraIndex := range paraArray.Nodes { para := paraArray.Eq(paraIndex) meaningArray := para.Find("span.ind") @@ -212,6 +205,7 @@ func meanings(word string) []meaningStr { } exampleArray := trg.Find("li.ex") + if len(exampleArray.Nodes) > 0 { meaning.Example = exampleArray.Eq(0).Text() } else { @@ -223,10 +217,12 @@ func meanings(word string) []meaningStr { subMeaningArray := trg.Find("li.subSense") var extractedSubMean []submeanStr + for subMeaningIndex := range subMeaningArray.Nodes { var subMeaningObj submeanStr subMeaningBlock := subMeaningArray.Eq(subMeaningIndex) meaningArray := subMeaningBlock.Find("span.ind") + if len(meaningArray.Nodes) > 0 { subMeaningObj.Smean = meaningArray.Eq(0).Text() } @@ -259,23 +255,28 @@ func weather(city string, state string) weatherStr { country := "india" url := "https://www.msn.com/en-in/weather/today/" + city + "," + state + "," + country + "/we-city?weadegreetype=C" + res, err := scrapper.ScrapeClientRequest(url, nil) if err != nil { logger.Error(err) } - doc, err := goquery.NewDocumentFromResponse(res) + + doc, err := goquery.NewDocumentFromReader(res.Body) if err != nil { logger.Error(err) } + currentSectionArray := doc.Find("section.curcond") if len(currentSectionArray.Nodes) <= 0 { logger.Error(err) } + currentSection := currentSectionArray.Eq(0) tempSpanArray := currentSection.Find("span.current") if len(tempSpanArray.Nodes) <= 0 { logger.Error(err) } + tempSpan := tempSpanArray.Eq(0) resultObj.Temperature = tempSpan.Text() + "°C" @@ -285,14 +286,17 @@ func weather(city string, state string) weatherStr { } conditionsArray := currentSection.Find("div.weather-info>ul>li") + for conditionIndex := range conditionsArray.Nodes { conditionSection := conditionsArray.Eq(conditionIndex) conditionChildrens := conditionSection.Contents() + if len(conditionChildrens.Nodes) <= 0 { continue } conditionName := strings.TrimSpace(conditionChildrens.First().Text()) conditionVal := strings.TrimSpace(conditionChildrens.Last().Text()) + switch strings.ToLower(conditionName) { case "feels like": resultObj.FeelsLike = conditionVal + "C" @@ -310,36 +314,43 @@ func weather(city string, state string) weatherStr { func scrapeImage(query string) []messageQueryBody { url := "https://www.google.co.in/search?q=" + query + "&source=lnms&tbm=isch&gbv=1" res, err := scrapper.ScrapeClientRequest(url, nil) + if err != nil { logger.Error(err) } - doc, err := goquery.NewDocumentFromResponse(res) + + doc, err := goquery.NewDocumentFromReader(res.Body) if err != nil { logger.Error(err) } + var resultObj []messageQueryBody tableArray := doc.Find("table") if len(tableArray.Nodes) <= 5 { logger.Error(errors.New("Unable to find image Table.")) } + table := tableArray.Eq(4) rowsArray := table.Find("td") count := 0 + for rowIndex := range rowsArray.Nodes { var resultElement messageQueryBody row := rowsArray.Eq(rowIndex) textArray := row.Find("font") text := "" + if len(textArray.Nodes) > 0 { text = textArray.Eq(0).Text() } + imgArray := row.Find("img") if len(imgArray.Nodes) <= 0 { continue } + imgTag := imgArray.Eq(0) imgLink, doLinkExist := imgTag.Attr("src") - if !doLinkExist { continue } @@ -347,6 +358,7 @@ func scrapeImage(query string) []messageQueryBody { resultElement.Link = imgLink resultElement.Head = text resultObj = append(resultObj, resultElement) + count = count + 1 if count >= 10 { break diff --git a/service/controllers/controllers_test.go b/service/controllers/controllers_test.go index d5e24bd..fa0faa2 100644 --- a/service/controllers/controllers_test.go +++ b/service/controllers/controllers_test.go @@ -33,28 +33,28 @@ func TestGoogleSearch(t *testing.T) { } -func disableTestYahooSearch(t *testing.T) { +// func disableTestYahooSearch(t *testing.T) { - form := url.Values{} - form.Add("username", "default") - form.Add("message", "yahoo CET bhubaneswar") - resp, err := http.PostForm("http://localhost:3000/message", form) - if err != nil { - panic(err) - } - body, _ := ioutil.ReadAll(resp.Body) - fmt.Println(string(body)) - // success check - if !(strings.Contains(string(body), "{\"status\":\"success\"") || strings.Contains(string(body), "{\"status\":true")) { - t.Errorf("Yahoo Response didnt match as required") - } +// form := url.Values{} +// form.Add("username", "default") +// form.Add("message", "yahoo CET bhubaneswar") +// resp, err := http.PostForm("http://localhost:3000/message", form) +// if err != nil { +// panic(err) +// } +// body, _ := ioutil.ReadAll(resp.Body) +// fmt.Println(string(body)) +// // success check +// if !(strings.Contains(string(body), "{\"status\":\"success\"") || strings.Contains(string(body), "{\"status\":true")) { +// t.Errorf("Yahoo Response didnt match as required") +// } - // incoming data checks - if !strings.Contains(string(body), "here are the top search results") { - t.Errorf("Yahoo functionality response didn't match as required!") - } +// // incoming data checks +// if !strings.Contains(string(body), "here are the top search results") { +// t.Errorf("Yahoo functionality response didn't match as required!") +// } -} +// } func TestBingSearch(t *testing.T) { os.Setenv("ENV", "test") @@ -81,34 +81,57 @@ func TestBingSearch(t *testing.T) { // TODO -func disableTestImagesSearch(t *testing.T) { +// func disableTestImagesSearch(t *testing.T) { - form := url.Values{} - form.Add("username", "default") - form.Add("message", "image CET bhubaneswar") - resp, err := http.PostForm("http://localhost:3000/message", form) - if err != nil { - panic(err) - } - body, _ := ioutil.ReadAll(resp.Body) - fmt.Println(string(body)) - // success check - if !(strings.Contains(string(body), "{\"status\":\"success\"") || strings.Contains(string(body), "{\"status\":true")) { - t.Errorf("Google Image Response didnt match as required") - } +// form := url.Values{} +// form.Add("username", "default") +// form.Add("message", "image CET bhubaneswar") +// resp, err := http.PostForm("http://localhost:3000/message", form) +// if err != nil { +// panic(err) +// } +// body, _ := ioutil.ReadAll(resp.Body) +// fmt.Println(string(body)) +// // success check +// if !(strings.Contains(string(body), "{\"status\":\"success\"") || strings.Contains(string(body), "{\"status\":true")) { +// t.Errorf("Google Image Response didnt match as required") +// } - // incoming data checks - if !strings.Contains(string(body), "here are the searched images") { - t.Errorf("Google image functionality response didn't match as required!") - } +// // incoming data checks +// if !strings.Contains(string(body), "here are the searched images") { +// t.Errorf("Google image functionality response didn't match as required!") +// } -} +// } + +// func disableTestYoutube(t *testing.T) { + +// form := url.Values{} +// form.Add("username", "default") +// form.Add("message", "youtube palazzo") +// resp, err := http.PostForm("http://localhost:3000/message", form) +// if err != nil { +// panic(err) +// } +// body, _ := ioutil.ReadAll(resp.Body) +// fmt.Println(string(body)) +// // success check +// if !(strings.Contains(string(body), "{\"status\":\"success\"") || strings.Contains(string(body), "{\"status\":true")) { +// t.Errorf("Youtube Response didnt match as required") +// } -func disableTestYoutube(t *testing.T) { +// // incoming data checks +// if !strings.Contains(string(body), "here are the top search videos") { +// t.Errorf("Youtube functionality response didn't match as required!") +// } + +// } +func TestWeather_1(t *testing.T) { + os.Setenv("ENV", "test") form := url.Values{} form.Add("username", "default") - form.Add("message", "youtube palazzo") + form.Add("message", "weather bhubaneswar odisha") resp, err := http.PostForm("http://localhost:3000/message", form) if err != nil { panic(err) @@ -117,21 +140,21 @@ func disableTestYoutube(t *testing.T) { fmt.Println(string(body)) // success check if !(strings.Contains(string(body), "{\"status\":\"success\"") || strings.Contains(string(body), "{\"status\":true")) { - t.Errorf("Youtube Response didnt match as required") + t.Errorf("Weather Response 1 didnt match as required") } // incoming data checks - if !strings.Contains(string(body), "here are the top search videos") { - t.Errorf("Youtube functionality response didn't match as required!") + if !strings.Contains(string(body), "here are the current weather conditions") { + t.Errorf("Weather functionality 1 response didn't match as required!") } } -func disableTestWeather_1(t *testing.T) { - +func TestWeather_2(t *testing.T) { + os.Setenv("ENV", "test") form := url.Values{} form.Add("username", "default") - form.Add("message", "weather bhubaneswar odisha") + form.Add("message", "weather in bhubaneswar odisha") resp, err := http.PostForm("http://localhost:3000/message", form) if err != nil { panic(err) @@ -140,35 +163,12 @@ func disableTestWeather_1(t *testing.T) { fmt.Println(string(body)) // success check if !(strings.Contains(string(body), "{\"status\":\"success\"") || strings.Contains(string(body), "{\"status\":true")) { - t.Errorf("Weather Response 1 didnt match as required") + t.Errorf("Weather Response 2 didnt match as required") } // incoming data checks if !strings.Contains(string(body), "here are the current weather conditions") { - t.Errorf("Weather functionality 1 response didn't match as required!") + t.Errorf("Weather functionality 2 response didn't match as required!") } } - -// func TestWeather_2(t *testing.T) { - -// form := url.Values{} -// form.Add("username", "default") -// form.Add("message", "weather in bhubaneswar odisha") -// resp, err := http.PostForm("http://localhost:3000/message", form) -// if err != nil { -// panic(err) -// } -// body, _ := ioutil.ReadAll(resp.Body) -// fmt.Println(string(body)) -// // success check -// if !(strings.Contains(string(body), "{\"status\":\"success\"") || strings.Contains(string(body), "{\"status\":true")) { -// t.Errorf("Weather Response 2 didnt match as required") -// } - -// // incoming data checks -// if !strings.Contains(string(body), "here are the current weather conditions") { -// t.Errorf("Weather functionality 2 response didn't match as required!") -// } - -// } diff --git a/service/controllers/logs/index.log b/service/controllers/logs/index.log new file mode 100644 index 0000000..73dd03a --- /dev/null +++ b/service/controllers/logs/index.log @@ -0,0 +1,4 @@ +2020/03/20 20:29:37 open static/config.json: no such file or directory +2020/03/20 20:34:38 open ../static/config.json: no such file or directory +2020/03/20 20:34:54 open ../static/messages.json: no such file or directory +2020/03/20 20:35:38 open ../static/medicine_database.json: no such file or directory diff --git a/service/controllers/processing.go b/service/controllers/processing.go index d9f7bfe..881dacd 100644 --- a/service/controllers/processing.go +++ b/service/controllers/processing.go @@ -26,9 +26,7 @@ func processGoogleResponses(searchTerm, countryCode, languageCode string, proxyS return nil, err } resultCounter += len(data) - for _, result := range data { - results = append(results, result) - } + results = append(results, data...) time.Sleep(time.Duration(backoff) * time.Second) } return results, nil @@ -214,9 +212,7 @@ func processBingResponses(searchTerm, country string, proxyString interface{}, p if err != nil { return nil, err } - for _, result := range data { - results = append(results, result) - } + results = append(results, data...) time.Sleep(time.Duration(backoff) * time.Second) } return results, nil diff --git a/service/controllers/searchVariants.go b/service/controllers/searchVariants.go index f2bd0af..2ed3d1f 100644 --- a/service/controllers/searchVariants.go +++ b/service/controllers/searchVariants.go @@ -201,15 +201,6 @@ var googleDomains = map[string]string{ "zw": "https://www.google.co.zw/search?q=", } -var yandexDomains = map[string]string{ - "com": "https://yandex.com/search/?text=", - "ru": "https://yandex.ru/search/?text=", - "ua": "https://yandex.ua/search/?text=", - "kz": "https://yandex.kz/search/?text=", - "by": "https://yandex.by/search/?text=", - "tr": "https://yandex.tr/search/?text=", -} - var bingDomains = map[string]string{ "com": "", "uk": "&cc=GB", diff --git a/service/controllers/send_mail.go b/service/controllers/send_mail.go index 36c2cdc..1cc22a3 100644 --- a/service/controllers/send_mail.go +++ b/service/controllers/send_mail.go @@ -35,7 +35,9 @@ func (s *SMTPServer) ServerName() string { func EmailController(w http.ResponseWriter, r *http.Request) { w.Header().Set("Access-Control-Allow-Origin", "*") w.Header().Set("Access-Control-Allow-Headers", "Content-Type") - r.ParseForm() + if err := r.ParseForm(); err != nil { + panic(err) + } to := r.FormValue("to") toArr := strings.Split(to, ";") @@ -136,9 +138,13 @@ func send(mailObject Mail, w http.ResponseWriter) { logger.Error(err) } - client.Quit() + if err = client.Quit(); err != nil { + panic(err) + } logger.Info("Mail sent successfully") - w.Write([]byte(`{"status":"success", "message": "Mail sent Successfully"}`)) + if _, err := w.Write([]byte(`{"status":"success", "message": "Mail sent Successfully"}`)); err != nil { + panic(err) + } } diff --git a/service/controllers/set_reminder.go b/service/controllers/set_reminder.go index 13dad04..e340a76 100644 --- a/service/controllers/set_reminder.go +++ b/service/controllers/set_reminder.go @@ -21,7 +21,9 @@ type reminder struct { func ReminderController(w http.ResponseWriter, r *http.Request) { w.Header().Set("Access-Control-Allow-Origin", "*") w.Header().Set("Access-Control-Allow-Headers", "Content-Type") - r.ParseForm() + if err := r.ParseForm(); err != nil { + panic(err) + } request := reminder{ Title: r.FormValue("title"), @@ -57,9 +59,13 @@ func addReminder(reminderObject reminder, w http.ResponseWriter) { _, err = stmt.Exec(reminderObject.Title, reminderObject.Description, reminderObject.Time, reminderObject.Cookie) checkErr(err) - tx.Commit() + if err = tx.Commit(); err != nil { + panic(err) + } - w.Write([]byte(`{"status": "success", "message": "Reminder has been set !"}`)) + if _, err := w.Write([]byte(`{"status": "success", "message": "Reminder has been set !"}`)); err != nil { + panic(err) + } } func ShowReminder() []reminder { diff --git a/service/controllers/types.go b/service/controllers/types.go index 939ca49..438ea21 100644 --- a/service/controllers/types.go +++ b/service/controllers/types.go @@ -5,10 +5,6 @@ type response struct { message string } -type statusCode struct { - status string -} - type messageQueryBody struct { Head string `json:"head"` Link string `json:"link"` diff --git a/service/messages/conversations.go b/service/messages/conversations.go index 13286c7..00020ca 100644 --- a/service/messages/conversations.go +++ b/service/messages/conversations.go @@ -12,11 +12,6 @@ import ( "time" ) -type response struct { - username string - message string -} - type jsonResponse struct { Status bool `json:"status"` Message string `json:"message"` @@ -49,13 +44,12 @@ var ( messagesRepliesParser Messagesreplies resp jsonResponse username, speak string - countMessage int16 ) func init() { prefix := "" if os.Getenv("ENV") == "test" { - prefix = "../" + prefix = "../../" } fmt.Println("Loading messages JSON parsers....") @@ -120,7 +114,9 @@ func GeneralConvHandler(req, name string, res http.ResponseWriter) string { resp = jsonResponse{true, temp, true, nil} speak = temp marshalled, _ := json.Marshal(resp) - res.Write(marshalled) + if _, err := res.Write(marshalled); err != nil { + panic(err) + } } } @@ -141,7 +137,9 @@ func GeneralConvHandler(req, name string, res http.ResponseWriter) string { resp = jsonResponse{true, temp, true, nil} speak = temp marshalled, _ := json.Marshal(resp) - res.Write(marshalled) + if _, err := res.Write(marshalled); err != nil { + panic(err) + } } } @@ -161,7 +159,9 @@ func GeneralConvHandler(req, name string, res http.ResponseWriter) string { resp = jsonResponse{true, temp, true, nil} speak = temp marshalled, _ := json.Marshal(resp) - res.Write(marshalled) + if _, err := res.Write(marshalled); err != nil { + panic(err) + } } } @@ -181,7 +181,9 @@ func GeneralConvHandler(req, name string, res http.ResponseWriter) string { resp = jsonResponse{true, temp, true, nil} speak = temp marshalled, _ := json.Marshal(resp) - res.Write(marshalled) + if _, err := res.Write(marshalled); err != nil { + panic(err) + } } } @@ -203,7 +205,9 @@ func GeneralConvHandler(req, name string, res http.ResponseWriter) string { resp = jsonResponse{true, temp, true, nil} speak = temp marshalled, _ := json.Marshal(resp) - res.Write(marshalled) + if _, err := res.Write(marshalled); err != nil { + panic(err) + } } } @@ -223,7 +227,9 @@ func GeneralConvHandler(req, name string, res http.ResponseWriter) string { resp = jsonResponse{true, temp, true, nil} speak = temp marshalled, _ := json.Marshal(resp) - res.Write(marshalled) + if _, err := res.Write(marshalled); err != nil { + panic(err) + } } } diff --git a/service/messages/health.go b/service/messages/health.go index 4313f70..153cfea 100644 --- a/service/messages/health.go +++ b/service/messages/health.go @@ -4,13 +4,14 @@ import ( "encoding/json" "errors" "fmt" - "github.com/Harkishen-Singh/Jarvis-personal-assistant/service/logger" - scrapper "github.com/Harkishen-Singh/Jarvis-personal-assistant/service/utils" - "github.com/PuerkitoBio/goquery" "io/ioutil" "net/http" "os" "strings" + + "github.com/Harkishen-Singh/Jarvis-personal-assistant/service/logger" + scrapper "github.com/Harkishen-Singh/Jarvis-personal-assistant/service/utils" + "github.com/PuerkitoBio/goquery" ) type medicineResponse struct { @@ -65,7 +66,7 @@ var ( func init() { prefix := "" if os.Getenv("ENV") == "test" { - prefix = "../" + prefix = "../../" } fmt.Println("Loading health-medicine JSON parsers....") medicineFile, err := os.Open(fmt.Sprintf("%sstatic/medicine_database.json", prefix)) @@ -271,7 +272,8 @@ func scrapMedicineLog(medicine *string) []string { if err != nil { logger.Error(err) } - doc, err := goquery.NewDocumentFromResponse(res) + doc, err := goquery.NewDocumentFromReader(res.Body) + if err != nil { logger.Error(err) } @@ -279,7 +281,7 @@ func scrapMedicineLog(medicine *string) []string { resultFould := false contentArray := doc.Find("table.search-results") if len(contentArray.Nodes) <= 0 { - logger.Error(errors.New("Search result table not found.")) + logger.Error(errors.New("RESULT_NOT_FOUND")) } content := contentArray.Eq(0) tableRowArray := content.Find("tr") @@ -303,22 +305,6 @@ func scrapMedicineLog(medicine *string) []string { return []string{result, *medicine} } -func processScrapLog(log *string) (data string) { - logStr := string(*log) - llogStr := len(logStr) - subl := "data ->" - lsubl := len(subl) - for i := 0; i < llogStr-lsubl; i++ { - if subl == logStr[i:i+lsubl] { - data = logStr[i+lsubl : llogStr] - break - } - } - fmt.Println("scrapped is -> ", data) - data = strings.Replace(data, "undefined", "", 1) - return -} - func handleResponse(ctr int, data []string, res http.ResponseWriter) string { var resp medicineResponse if ctr == 1 { @@ -336,7 +322,10 @@ func handleResponse(ctr int, data []string, res http.ResponseWriter) string { } send, _ := json.Marshal(resp) - res.Write(send) + if _, err := res.Write(send); err != nil { + panic(err) + } + return "generic " + data[1] } @@ -352,7 +341,7 @@ func scrapSymptomsLog(sypm *string) []string { if err != nil { logger.Error(err) } - doc, err := goquery.NewDocumentFromResponse(res) + doc, err := goquery.NewDocumentFromReader(res.Body) if err != nil { logger.Error(err) } diff --git a/service/services/herokuhost/docker_heroku.go b/service/services/herokuhost/docker_heroku.go index 634e1ad..78d4c44 100644 --- a/service/services/herokuhost/docker_heroku.go +++ b/service/services/herokuhost/docker_heroku.go @@ -6,101 +6,11 @@ package herokuhost import ( - "encoding/json" "fmt" "net/http" "os/exec" ) -const ( - // auto-deployment is disabled for now. - // shouldbe done in future versions if required. - herokuMailID string = "" -) - -func herokuLogin() bool { - return false -} - -type automateDeploymentContainer interface { - herokuLogin() bool - herokuContainerPush() bool - herokuContainerLogin() bool - herokuCreate() string - herokuContainerRelease() bool - herokuOpen() bool -} - -type herokuDetails struct { - emailID, password string -} - -func (dep herokuDetails) herokuLogin() bool { - - res, err := exec.Command("heroku login").Output() - if err != nil { - fmt.Println("heroku login error") - panic(err) - } - fmt.Printf("heroku login \n %s", res) - return true -} - -func herokuContainerPush() bool { - res, err := exec.Command("heroku container:push web").Output() - if err != nil { - fmt.Println("heroku container push error") - panic(err) - } - fmt.Printf("heroku container push \n %s", res) - return true -} - -func herokuContainerLogin() bool { - res, err := exec.Command("heroku container:login").Output() - if err != nil { - fmt.Println("heroku container login error") - panic(err) - } - fmt.Printf("heroku container login \n %s", res) - return true -} - -func herokuCreate(expectedName string) string { - res, err := exec.Command("heroku create", expectedName).Output() - if err != nil { - fmt.Println("heroku create error") - panic(err) - } - fmt.Printf("heroku create \n %s", res) - return string(res) -} - -func herokuContainerRelease() bool { - res, err := exec.Command("heroku container:release web").Output() - if err != nil { - fmt.Println("heroku container release error") - panic(err) - } - fmt.Printf("heroku container release \n %s", res) - return true -} - -func herokuOpen() bool { - res, err := exec.Command("heroku open").Output() - if err != nil { - fmt.Println("heroku container release error") - panic(err) - } - fmt.Printf("heroku container release \n %s", res) - return true -} - -type automateDeploymentGithub interface { - herokuGithubSubprocess() string - herokuGithubLogs() string -} - type herokuGithubCredentials struct { email, password, repoName, result string } @@ -161,7 +71,6 @@ func DeploymentFunction(repoName string, res http.ResponseWriter) string { resp.Status = true resp.Message = "Successfully deployed. Link " + response } - unmarshall, _ := json.Marshal(resp) - res.Write(unmarshall) + return resp.Message } diff --git a/service/utils/check_service.go b/service/utils/check_service.go index ec01051..df65750 100644 --- a/service/utils/check_service.go +++ b/service/utils/check_service.go @@ -8,8 +8,5 @@ import ( // returns the status as a boolean value func Connected() bool { _, err := http.Get("http://www.google.co.in") - if err != nil { - return false - } - return true + return err == nil } diff --git a/tests/test.go b/tests/test.go index 43c92d0..f967f40 100644 --- a/tests/test.go +++ b/tests/test.go @@ -5,12 +5,8 @@ import ( "os/exec" ) -const ( - herokuMailID string = "harkishensingh@hotmail.com" -) - func init() { - res, err := exec.Command("docker info").Output() + res, err := exec.Command("docker", "info").Output() if err != nil || len(string(res)) == 0 { fmt.Println("[JARVIS] seems like docker is not installed") } @@ -20,48 +16,13 @@ func init() { } } -func checkDockerInstallation() bool { - - res, err := exec.Command("docker info").Output() - if err != nil || len(string(res)) == 0 { - fmt.Println("[JARVIS] seems like docker is not installed") - return false - } - - return true -} - -func checkHerokuCLIInstallation() bool { - - res, err := exec.Command("heroku").Output() - if err != nil || len(string(res)) == 0 { - fmt.Println("[JARVIS] seems like heroku is not installed") - return false - } - - return true -} - -func herokuLogin() bool { - return false -} - -type automateDeployment interface { - herokuLogin() bool - herokuContainerPush() bool - herokuContainerLogin() bool - herokuCreate() string - herokuContainerRelease() bool - herokuOpen() bool -} - type herokuDetails struct { emailID, password string } func (dep herokuDetails) herokuLogin() bool { - res, err := exec.Command("heroku login").Output() + res, err := exec.Command("heroku", "login").Output() if err != nil { fmt.Println("heroku login error") panic(err) @@ -71,7 +32,7 @@ func (dep herokuDetails) herokuLogin() bool { } func herokuContainerPush() bool { - res, err := exec.Command("heroku container:push web").Output() + res, err := exec.Command("heroku", "container:push web").Output() if err != nil { fmt.Println("heroku container push error") panic(err) @@ -80,18 +41,8 @@ func herokuContainerPush() bool { return true } -func herokuContainerLogin() bool { - res, err := exec.Command("heroku container:login").Output() - if err != nil { - fmt.Println("heroku container login error") - panic(err) - } - fmt.Printf("heroku container login \n %s", res) - return true -} - func herokuCreate(expectedName string) string { - res, err := exec.Command("heroku create", expectedName).Output() + res, err := exec.Command("heroku", "create", expectedName).Output() if err != nil { fmt.Println("heroku create error") panic(err) @@ -101,7 +52,7 @@ func herokuCreate(expectedName string) string { } func herokuContainerRelease() bool { - res, err := exec.Command("heroku container:release web").Output() + res, err := exec.Command("heroku", "container:release web").Output() if err != nil { fmt.Println("heroku container release error") panic(err) @@ -111,7 +62,7 @@ func herokuContainerRelease() bool { } func herokuOpen() bool { - res, err := exec.Command("heroku open").Output() + res, err := exec.Command("heroku", "open").Output() if err != nil { fmt.Println("heroku container release error") panic(err)