telegram.getChatID   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 4
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nop 0
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
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
	var emptyStrings []string
81
82
	if errorsContext == "" {
83
		utils.PutIntoCache(emptyStrings, "errors")
84
		return emptyStrings
85
	}
86
87
	newErrors := utils.ParseErrors(errorsContext)
88
89
	if len(newErrors) == 0 {
90
		utils.PutIntoCache(emptyStrings, "errors")
91
		return emptyStrings
92
	}
93
94
	oldErrors := utils.GetCached("errors")
95
	uniqueErrors := utils.GetUniqueItem(oldErrors, newErrors)
96
97
	if newErrors != nil && len(uniqueErrors) > 0 {
98
		utils.PutIntoCache(newErrors, "errors")
99
	}
100
101
	return uniqueErrors
102
}
103
104
// getReportsIfExist makes request gets data and searches for new Photo Reports
105
func getReportsIfExist() []models.PhotoReport {
106
	html := utils.GetHTMLFromTargetURL(os.Getenv("BOT_TARGET_URL"))
107
	posts := utils.GetPostsFromHTML(&html)
108
	photoReports := utils.GetAllInformation(&posts)
109
	titles := utils.GetTitlesFromPhotoReports(photoReports)
110
111
	return utils.GenerateMapOfNewData(titles, photoReports)
112
}
113