forked from pathao-eng/empleo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
93 lines (79 loc) · 1.6 KB
/
main.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
package main
import (
"bufio"
"fmt"
"github.com/pathao-eng/empleo/core"
"github.com/spf13/viper"
"html/template"
"log"
"os"
"plugin"
"strings"
"time"
)
func main() {
if len(os.Args) > 1 {
viper.AddConfigPath(os.Args[1])
}
viper.AddConfigPath(".")
viper.SetConfigType("json")
viper.SetConfigName("config")
if err := viper.ReadInConfig(); err != nil {
panic(err)
}
var sources []core.EmpleoSource
srcCfg := viper.GetStringMapString("sources")
for k, v := range srcCfg {
plug, err := plugin.Open(v)
if err != nil {
log.Println(err)
continue
}
sym, err := plug.Lookup(strings.ToUpper(k))
if err != nil {
log.Println(err)
continue
}
a, ok := sym.(core.EmpleoSource)
if !ok {
log.Println("Symbol is not type of EmpleoSource")
continue
}
sources = append(sources, a)
}
var empleos []core.Empleo
for _, s := range sources {
if err := s.Init(); err != nil {
log.Println(err)
continue
}
data := fetchAll(s, []core.Empleo{})
empleos = append(empleos, data...)
}
timeNow := time.Now().Format("2006-01-02")
f, err := os.Create(fmt.Sprintf("./results/%s.md", timeNow))
if err != nil {
panic(err)
}
w := bufio.NewWriter(f)
t := template.New("EmpleoTemplate")
t, err = t.ParseGlob("templates/*.tmpl")
if err != nil {
panic(err)
}
err = t.Execute(w, map[string]interface{}{
"parsed_date": timeNow,
"empleos": empleos,
})
if err != nil {
panic(err)
}
}
func fetchAll(s core.EmpleoSource, init []core.Empleo) []core.Empleo {
data, ok := s.Fetch()
init = append(init, data...)
if ok {
return fetchAll(s, init)
}
return init
}