-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
75 lines (60 loc) · 1.61 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
package main
import (
"fmt"
"log"
"os"
"time"
"github.com/joho/godotenv"
"github.com/riemerio/pellets-price-scraper/db"
"github.com/riemerio/pellets-price-scraper/pellets"
"github.com/riemerio/pellets-price-scraper/prices"
"github.com/robfig/cron/v3"
"go.mongodb.org/mongo-driver/bson/primitive"
)
func init() {
connection := db.Connect("localhost", 27017)
fmt.Println(connection.NumberSessionsInProgress())
}
func main() {
err := godotenv.Load(".env")
if err != nil {
log.Println("No local .env file found")
}
fmt.Println("post code:", os.Getenv("SCRAPING_POSTCODE"))
fmt.Println("main started")
c := cron.New()
c.AddFunc(os.Getenv("SCRAPING_CRON"), doRequests)
c.Start()
//sig := make(chan os.Signal)
//signal.Notify(sig, os.Interrupt, os.Kill)
//<-sig
select {}
}
func doRequests() {
go func() {
result := pellets.FetchPriceForLoosePellets(6000)
entry, err := db.Collection.InsertOne(db.CTX, &prices.Price{
ID: primitive.NewObjectID(),
Price: result.NettoEinzelpreis,
Timestamp: time.Now(),
RecordId: pellets.Loose,
})
if err != nil {
fmt.Println(err.Error())
}
fmt.Println("loose pellets: %s with id %s", result.NettoEinzelpreis, entry.InsertedID)
}()
go func() {
result := pellets.FetchPriceForSackedPellets(6)
entry, err := db.Collection.InsertOne(db.CTX, &prices.Price{
ID: primitive.NewObjectID(),
Price: result.NettoEinzelpreis,
Timestamp: time.Now(),
RecordId: pellets.Sacked,
})
if err != nil {
fmt.Println(err.Error())
}
fmt.Println("sacked pellets: %s with id %s", result.NettoEinzelpreis, entry.InsertedID)
}()
}