-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.go
102 lines (89 loc) · 2.47 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
94
95
96
97
98
99
100
101
102
package main
import "os"
import "fmt"
// Those values are initialized by a LD Flag
var (
// Version contains the current git tag
Version string
// Build is the build date
Build string
)
func main() {
// parse arguments
args := os.Args
analyzeMode := len(args) == 3 && args[2] == "--analyze"
migrateDbMode := len(args) == 3 && args[2] == "--migratedb"
updateDbMode := len(args) == 2
if !analyzeMode && !updateDbMode && !migrateDbMode {
printUsage(args[0])
return
}
configFilePath := args[1]
// read configuration
config, err := ReadConfigFile(configFilePath)
if err != nil {
fmt.Print(err.Error())
return
}
// retrieve data about previous launches
dbAdData, err := LoadOrCreate(config.DatabaseFilepath, config.Searches)
if err != nil {
fmt.Print(err.Error())
return
}
// run in the good mode.
if analyzeMode {
analyzeDb(config, dbAdData)
} else if migrateDbMode {
migrateDb(config, dbAdData)
} else if updateDbMode {
updateDb(config, dbAdData)
}
fmt.Println("DONE")
}
func printUsage(progName string) {
fmt.Println(progName + " is a scraper of a well knwon classified ads website.")
fmt.Println()
fmt.Println("Usage:")
fmt.Println()
fmt.Println(" " + progName + " configFile.json [--analyze|--migratedb]")
fmt.Println()
fmt.Println("Version : " + Version)
fmt.Println("Build : " + Build)
fmt.Println()
}
func analyzeDb(config Configuration, dbAdData *DbAdData) {
fmt.Println("analyzing Database")
for _, search := range config.Searches {
fmt.Println("Search : ", search.Name)
adDatas, _ := dbAdData.GetAllAds(search)
Analyze(adDatas)
}
}
func migrateDb(config Configuration, dbAdData *DbAdData) {
fmt.Println("migrating Database")
err := dbAdData.Migrate()
if err != nil {
fmt.Print(err.Error())
return
}
}
func updateDb(config Configuration, dbAdData *DbAdData) {
// Scrap new data
collector := CallbackCollectorFactory(*dbAdData)
timer := CallbackTimerFactory(config.TimeBetweenRequestsInSeconds)
printer := CallbackPrinterFactory()
// it could be possible to have a callback that compute a distance to a
// point, and enrichies the AdData. on the basis of this webservice :
// $ curl -s "http://api-adresse.data.gouv.fr/search/?type=city&q=Carcassonne" |jq
if err := Scraper(config.Searches, collector, timer, printer); err != nil {
fmt.Print(err.Error())
return
}
// build & send a mail
fmt.Println("Sending mail")
if err := SendAdsByMail(config, collector.newAdsBySearch); err != nil {
fmt.Print(err.Error())
return
}
}