diff options
Diffstat (limited to 'crawler/database.go')
| -rw-r--r-- | crawler/database.go | 29 |
1 files changed, 21 insertions, 8 deletions
diff --git a/crawler/database.go b/crawler/database.go index 6e08b78..45f7fda 100644 --- a/crawler/database.go +++ b/crawler/database.go @@ -35,8 +35,8 @@ func (app *App) createTables() error { base_price INT NOT NULL, image_url TEXT, spirit_type TEXT NOT NULL, - website TEXT, - website_raw TEXT NOT NULL, + website LONGTEXT, + website_raw LONGTEXT NOT NULL, valid_until INT NOT NULL DEFAULT 0, created_at INT, FOREIGN KEY(shop) REFERENCES shop(id), @@ -76,7 +76,7 @@ func (app *App) createTables() error { } } - view_query := `CREATE OR REPLACE VIEW _intern_view AS SELECT angebot.id, angebot.name, angebot.abv, angebot.volume, angebot.url as long_url, angebot.short_url as url, spirit_type, original_price, discounted_price, base_price, shop.name as shop, shop.id as shop_id, shop.short_url as shop_url, created_at, valid_until + view_query := `CREATE OR REPLACE VIEW _intern_view AS SELECT angebot.id, angebot.name, angebot.abv, angebot.volume, angebot.url as long_url, angebot.short_url as url, spirit_type, original_price, discounted_price, base_price, image_url, shop.name as shop, shop.id as shop_id, shop.short_url as shop_url, website_raw, created_at, valid_until FROM angebot JOIN shop ON angebot.shop = shop.id WHERE (valid_until = 0 OR valid_until > (SELECT UNIX_TIMESTAMP()))` @@ -131,11 +131,24 @@ func (app *App) save_offer(W []Angebot) error { o.error_msg = err.Error() o.error_ctx = fmt.Sprintf(strings.Replace(detect_duplicate_query, "?", `"%s"`, 1), o.Name) WarnOffer(o, "database.go: Duplicate query failed") - } /* else { - o.error_msg = "database.go: Duplicate detected" - o.error_ctx = fmt.Sprintf(strings.Replace(detect_duplicate_query, "?", `"%s"`, 1), o.Name) - DebugOffer(o, "database.go: Duplicate detected") - }*/ + } else { + /* + * If everything went right we update the image url to reflect new changes. + */ + update_img_query := fmt.Sprintf(`UPDATE _intern_view SET image_url = ?, website_raw = ? WHERE name = ? AND shop_id = %d AND volume = %4.2f AND abv = %4.2f AND original_price = %d AND discounted_price = %d AND valid_until = %d`, o.Shop, o.Volume, o.Abv, o.Original_price, o.Discounted_price, o.Valid_until) + update_img_stmt, err := app.DB.Prepare(update_img_query) + if err != nil { + o.error_msg = err.Error() + o.error_ctx = fmt.Sprintf(`UPDATE _intern_view SET image_url = %s, website_raw = %s WHERE name = %s AND shop_id = %d AND volume = %4.2f AND abv = %4.2f AND original_price = %d AND discounted_price = %d AND valid_until = %d`, o.Image_url, "redacted", o.Name, o.Shop, o.Volume, o.Abv, o.Original_price, o.Discounted_price, o.Valid_until) + WarnOffer(o, "database.go: Preparing update_img_query failed") + } + _, err = update_img_stmt.Exec(o.Image_url, o.Website, o.Name) + if err != nil { + o.error_msg = err.Error() + o.error_ctx = fmt.Sprintf(`UPDATE _intern_view SET image_url = %s WHERE name = %s AND shop_id = %d AND volume = %4.2f AND abv = %4.2f AND original_price = %d AND discounted_price = %d AND valid_until = %d`, o.Image_url, o.Name, o.Shop, o.Volume, o.Abv, o.Original_price, o.Discounted_price, o.Valid_until) + WarnOffer(o, "database.go: Executing update_img_query failed") + } + } } return nil |
