summaryrefslogtreecommitdiff
path: root/crawler/main.go
blob: 5255e1ca5017bbc7a35d4aac0991029be69f3b37 (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
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
package main

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

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

	"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
	Shop             int
	Url              string
	Original_price   int
	Discounted_price int
	Image_url        string
	Spirit_type      string
	Valid_until      int
}

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

func main() {

	var err error

	app := App{Config: &Config{}}
	app.Config.parseConfig("")

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

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

		if app.Config.Debug {
			log.Println(app.Config.DBUser + ":" + app.Config.DBPassword + "@tcp(" + app.Config.DBHost + ":" + app.Config.DBPort + ")/" + app.Config.DBDBName + 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)
	}
	defer app.DB.Close()

	if err != nil {
		log.Fatal(err)
	}

	err = app.createTables()
	if err != nil {
		log.Fatal(err)
	}

	err = app.insertShops()
	if err != nil {
		log.Fatal(err)
	}

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

	W := ScrapeHTML(shops)

	err = app.save_offer(W)
	if err != nil {
		log.Fatal(err)
	}
	err = app.remove_expired(W)
	if err != nil {
		log.Fatal(err)
	}
}

func printName(W []Angebot, name string) {
	return
	fmt.Println("-------------------")
	fmt.Println("Sonderangebote von " + name)
	fmt.Println("-------------------")

	output, err := json.MarshalIndent(W, "", "	")
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(string(output))
}

func ScrapeHTML(shops []Shop) []Angebot {
	var W []Angebot

	for _, shop := range shops {

		switch shop.Name {
		case "Bottleworld":
			W = append(W, ScrapeBottleWord(shop)...)
		case "MC Whisky":
			W = append(W, ScrapeMCWhisky(shop)...)
		case "Rum & Co":
			W = append(W, ScrapeRumundCo(shop)...)
		case "Whic":
			W = append(W, ScrapeWhic(shop)...)
		case "Whisky.de":
			W = append(W, ScrapeWhiskyde(shop)...)
		case "Whiskysite.nl":
			W = append(W, ScrapeWhiskysitenl(shop)...)
		case "Whisky World":
			W = append(W, ScrapeWhiskyworld(shop)...)
		case "Whiskyzone":
			W = append(W, ScrapeWhiskyzone(shop)...)
		default:
			log.Println(shop.Name + ": No Crawler")
		}
	}

	return W
}