summaryrefslogtreecommitdiff
path: root/crawler/main.go
blob: 811f4d014771767d9506a446e455af06f259a4ef (plain)
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
package main

import (
	"encoding/json"
	"fmt"
	"time"

	_ "database/sql"
	_ "github.com/go-sql-driver/mysql"
	//_ "github.com/mattn/go-sqlite3"

	log "github.com/Sirupsen/logrus"
	"github.com/jmoiron/sqlx"
)

type App struct {
	Offers []Angebot
	Shops  []Shop
	Config *Config
	DB     *sqlx.DB
	Now    int64
	Debug  bool
}

type Angebot struct {
	Id               int
	Name             string
	Abv              float32
	Volume           float32
	Age              int
	Shop             int
	Url              string
	Short_url        string
	Original_price   int
	Discounted_price int
	Base_price       int
	Image_url        string
	Spirit_type      string
	Website          string
	Valid_until      int

	error_msg string
	error_ctx string
}

type Shop struct {
	Id             int
	Name           string
	Url            string
	Short_url      string
	Logo_url       string
	Shipping_costs int
	Free_shipping  string
}

func main() {

	var err error

	// copy global config to avoid woring with globals
	_own_config := _conf
	app := App{Config: &_own_config}
	// overwrite the global
	_conf = Config{}

	app.Now = time.Now().Unix()

	if "sqlite3" == app.Config.DBDriver {
		app.DB, err = sqlx.Connect(app.Config.DBDriver, app.Config.DBPath)
	} else {

		log.Debug(fmt.Sprintf(`Connecting to "%s" database "%s" as user "%s" on host "%s:%s" with extra options "%s".`, app.Config.DBDriver, app.Config.DBDBName, app.Config.DBUser, app.Config.DBHost, app.Config.DBPort, app.Config.DBOptions))

		app.DB, err = sqlx.Connect(app.Config.DBDriver, app.Config.DBUser+":"+app.Config.DBPassword+"@tcp("+app.Config.DBHost+":"+app.Config.DBPort+")/"+app.Config.DBDBName+app.Config.DBOptions)
	}
	if err != nil {
		Fatal(err, "Cannot connect to database")
	}

	if err = app.DB.Ping(); err != nil {
		Fatal(err, "No connection to database")
	}
	defer app.DB.Close()

	err = app.createTables()
	if err != nil {
		Fatal(err, "Creating table failed")
	}

	err = app.insertShops()
	if err != nil {
		Fatal(err, "Inserting shops failed")
	}

	shops, err := app.getShops()
	if err != nil {
		Fatal(err, "Getting shops failed")
	}

	app.ScrapeHTML(shops)

	// short url
	err = app.post_process()
	if err != nil {
		Fatal(err, "Post processing failed")
	}
}