forked from gaulliath/operative-framework
/
cron.go
141 lines (121 loc) · 3.43 KB
/
cron.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// AUTHOR : TRISTAN GRANIER
// RESUME : This service get domain name registered with email address
// TIME : Every 48 hours
package email_to_domain_cron
import (
"bytes"
"encoding/json"
"log"
"net/http"
"strings"
"time"
"github.com/graniet/operative-framework/session"
"github.com/joho/godotenv"
"github.com/pkg/errors"
)
type Service struct {
session.CronJob
session *session.Session
}
func GetNewService(sess *session.Session) *Service {
return &Service{
session: sess,
}
}
// Service as been started every 48 hours
func (service *Service) GetHibernate() time.Duration {
return 48 * time.Hour
}
// Service name as been set here
func (service *Service) Name() string {
return "email_to_domain.cron"
}
// Define if service need configuration file
func (service *Service) HasConfiguration() bool {
return true
}
// Get configuration
func (service *Service) GetConfiguration() map[string]string {
configuration := make(map[string]string)
configuration["EMAIL"] = "example@email.com,example2@email.com"
configuration["TO_SERVER"] = "false"
configuration["SERVER_URI"] = "http://example.com/api/v1.0/insert"
configuration["VERBOSE"] = "true"
return configuration
}
// Get required fields in configuration file
func (service *Service) GetRequired() []string {
return []string{
"EMAIL",
"TO_SERVER",
"SERVER_URI",
"VERBOSE",
}
}
// Fetching username tweets
func (service *Service) Fetch(configuration map[string]string, email string) (bool, error) {
module, err := service.session.SearchModule("email_to_domain")
if err != nil {
return false, err
}
targetId, err := service.session.AddTarget("email", email)
if err != nil {
if !strings.Contains(err.Error(), "already exist") || targetId == "" {
return false, err
}
}
_, err = module.SetParameter("TARGET", targetId)
if err != nil {
return false, err
}
module.Start()
target, err := service.session.GetTarget(targetId)
if err != nil {
return false, err
}
results, err := target.GetFormatedResults("email_to_domain")
js, err := json.Marshal(&results)
if err != nil {
return false, err
}
if strings.ToLower(configuration["TO_SERVER"]) == "true" {
if strings.ToLower(configuration["VERBOSE"]) == "true" {
log.Println("Prepare request '" + email + "' at '" + configuration["SERVER_URI"] + "'")
}
req, err := http.NewRequest("POST", configuration["SERVER_URI"], bytes.NewBuffer(js))
if err != nil {
return false, errors.New("Can't make request to '" + configuration["SERVER_URI"] + "'")
}
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return false, err
}
defer resp.Body.Close()
if strings.ToLower(configuration["VERBOSE"]) == "true" {
log.Println("Request '" + email + "' as been sent at '" + configuration["SERVER_URI"] + "'")
}
}
return true, nil
}
// Service Execution with opf routine
func (service *Service) Run() (bool, error) {
configuration, _ := godotenv.Read(service.session.Config.Common.ConfigurationJobs + service.Name() + "/cron.conf")
service.session.Stream.Verbose = false
if strings.Contains(configuration["EMAIL"], ",") {
emails := strings.Split(configuration["EMAIL"], ",")
for _, email := range emails {
ret, err := service.Fetch(configuration, email)
if err != nil {
return ret, err
}
}
} else {
ret, err := service.Fetch(configuration, configuration["EMAIL"])
if err != nil {
return ret, err
}
}
return true, nil
}