-
Notifications
You must be signed in to change notification settings - Fork 0
/
parsers.go
59 lines (53 loc) · 1.49 KB
/
parsers.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
package utils
import (
"encoding/base64"
"log"
"regexp"
"strings"
"google.golang.org/api/gmail/v1"
)
/**
* Parses url with the given regular expression and returns the
* group values defined in the expression.
*
*/
func GetParams(regEx, url string) (paramsMap map[string]string) {
var compRegEx = regexp.MustCompile(regEx)
match := compRegEx.FindStringSubmatch(url)
paramsMap = make(map[string]string)
for i, name := range compRegEx.SubexpNames() {
if i > 0 && i <= len(match) {
paramsMap[name] = match[i]
}
}
if paramsMap["Seg"] != "" && paramsMap["Min"] == "" {
paramsMap["Min"] = "0"
}
return paramsMap
}
func GetMsgBody(msg *gmail.Message) (string, error) {
// Obtener el cuerpo del mensaje
var body string
if msg.Payload.Body.Data != "" {
bodyBytes, err := base64.StdEncoding.DecodeString(msg.Payload.Body.Data)
if err != nil {
log.Fatalf("Error al decodificar el cuerpo del mensaje: %v", err)
return "", err
}
body = strings.ReplaceAll(string(bodyBytes), "\r", "")
} else {
// Si el cuerpo está en multipart, iterar por las partes para encontrar el cuerpo deseado
for _, part := range msg.Payload.Parts {
if part.MimeType == "text/plain" || part.MimeType == "text/html" {
bodyBytes, err := base64.StdEncoding.DecodeString(part.Body.Data)
if err != nil {
log.Fatalf("Error al decodificar el cuerpo del mensaje: %v", err)
return "", err
}
body = strings.ReplaceAll(string(bodyBytes), "\r", "")
break
}
}
}
return body, nil
}