Completed
Push — master ( 6c31ef...b0b258 )
by Serhii
02:18
created

telegram/helpers.go   A

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Test Coverage

Coverage 11.9%

Importance

Changes 0
Metric Value
cc 15
eloc 59
dl 0
loc 99
ccs 5
cts 42
cp 0.119
crap 168.8545
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A telegram.getErrorsIfExist 0 12 3
A telegram.GetMessageIfPingIsNotSuccessful 0 14 3
A telegram.GetMessagesWithNewReports 0 14 3
A telegram.todayIsReportCheckDay 0 3 1
A telegram.GetMessagesWithNewErrors 0 14 3
A telegram.getChatID 0 4 1
A telegram.getReportsIfExist 0 7 1
1
package telegram
2
3
import (
4
	"fmt"
5
	"os"
6
	"os/exec"
7
	"strconv"
8
	"strings"
9
	"time"
10
11
	"github.com/SerhiiCho/shoshka-go/models"
12
	"github.com/SerhiiCho/shoshka-go/utils"
13
)
14
15
func getChatID() int64 {
16 1
	chatID, err := strconv.ParseInt(os.Getenv("BOT_CHAT_ID"), 10, 64)
17 1
	utils.HandleError(err, "Cannot convert chat id to integer")
18 1
	return chatID
19
}
20
21
// todayIsReportCheckDay returns true if current day is one of allowed for checking reports
22
func todayIsReportCheckDay(today string) bool {
23 1
	allowedDays := strings.Split(os.Getenv("DAYS_FOR_REPORT_CHECK"), ",")
24 1
	return utils.Contains(allowedDays, today)
25
}
26
27
// GetMessagesWithNewReports puts messages into a chanel
28
func GetMessagesWithNewReports() []string {
29
	var messages []string
30
	today := time.Now().Weekday().String()
31
32
	if !todayIsReportCheckDay(today) {
33
		fmt.Printf("Today is %s, but messages will be send only in %s days of the week\n", today, os.Getenv("DAYS_FOR_REPORT_CHECK"))
34
		return messages
35
	}
36
37
	for _, report := range getReportsIfExist() {
38
		messages = append(messages, fmt.Sprintf("New Photo Report!\n%s\n%s", report.Title, report.URL))
39
	}
40
41
	return messages
42
}
43
44
// GetMessageIfPingIsNotSuccessful returns error message if ping is not successful
45
func GetMessageIfPingIsNotSuccessful() []string {
46
	var messages []string
47
	host := os.Getenv("SITE_ADDRESS")
48
	out, err := exec.Command("ping", host, "-c2").Output()
49
50
	canPing := !strings.Contains(string(out), "Destination Host Unreachable")
51
52
	if canPing && err == nil {
53
		fmt.Printf("Host %s is reachable\n", host)
54
		return messages
55
	}
56
57
	fmt.Printf("Host %s is reachable\n", host)
58
	return append(messages, fmt.Sprintf("Host %s is not reachable", host))
59
}
60
61
// GetMessagesWithNewErrors puts messages into a chanel
62
func GetMessagesWithNewErrors() []string {
63
	var messages []string
64
65
	for _, errorMessage := range getErrorsIfExist() {
66
		messages = append(messages, fmt.Sprintf("Error has occurred on Shobar site https://shobar.com.ua\n\n%s", errorMessage))
67
	}
68
69
	if len(messages) == 0 {
70
		fmt.Printf("There are no new errors in log file: %s\n", os.Getenv("ERROR_LOG_PATH"))
71
		return messages
72
	}
73
74
	fmt.Printf("Found new errors in log file: %s\n", os.Getenv("ERROR_LOG_PATH"))
75
	return messages
76
}
77
78
func getErrorsIfExist() []string {
79
	errorsContext := utils.FileGetContents(os.Getenv("ERROR_LOG_PATH"))
80
81
	newErrors := utils.ParseErrors(errorsContext)
82
	oldErrors := utils.GetCached("errors")
83
	uniqueErrors := utils.GetUniqueItem(oldErrors, newErrors)
84
85
	if newErrors != nil && len(uniqueErrors) > 0 {
86
		utils.PutIntoCache(newErrors, "errors")
87
	}
88
89
	return uniqueErrors
90
}
91
92
// getReportsIfExist makes request gets data and searches for new Photo Reports
93
func getReportsIfExist() []models.PhotoReport {
94
	html := utils.GetHTMLFromTargetURL(os.Getenv("BOT_TARGET_URL"))
95
	posts := utils.GetPostsFromHTML(&html)
96
	photoReports := utils.GetAllInformation(&posts)
97
	titles := utils.GetTitlesFromPhotoReports(photoReports)
98
99
	return utils.GenerateMapOfNewData(titles, photoReports)
100
}
101