Skip to content

Commit

Permalink
First version
Browse files Browse the repository at this point in the history
  • Loading branch information
j-vizcaino committed Oct 7, 2016
1 parent ea43966 commit 093670d
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ _testmain.go

# Vendor libs
vendor/

.vscode/
auvieux-scraper
debug
16 changes: 16 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/make -f

all: auvieux-scraper


.PHONY: all deps clean

deps:
glide install

auvieux-scraper: auvieux-scraper.go
go build -o $@ $^

clean:
rm -rf auvieux-scraper

71 changes: 71 additions & 0 deletions auvieux-scraper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package main

import (
"fmt"
"os"
"regexp"
"strconv"

"github.com/PuerkitoBio/goquery"
)

const searchURLBase = "http://www.auvieuxcampeur.fr/catalogsearch/result/?q="

func searchURL(id string) string {
return fmt.Sprintf("%s%s", searchURLBase, id)
}

type product struct {
ID string
Name string
Price float32
}

func convertPrice(pstr string) (float64, error) {
// Remove trailing ' €'
re := regexp.MustCompile(`^(\d+),(\d+).*$`)
s := re.ReplaceAllString(pstr, "$1.$2")
return strconv.ParseFloat(s, 32)
}

func scrapeProduct(id string) (*product, error) {
url := searchURL(id)
doc, err := goquery.NewDocument(url)
if err != nil {
return nil, err
}

name := doc.Find(".product-name > a").First().Text()
priceStr := doc.Find(".regular-price .price").First().Text()

price, err := convertPrice(priceStr)
if err != nil {
return nil, err
}

return &product{
ID: id,
Name: name,
Price: float32(price),
}, nil
}

func main() {
products := os.Args[1:]

if len(products) == 0 {
fmt.Printf("Usage: auvieux-scraper product_id [product_id ...]")
os.Exit(1)
}

fmt.Printf("Référence;Désignation;Prix TTC\n")
for _, productID := range products {
p, err := scrapeProduct(productID)
if err != nil {
fmt.Printf("Error getting product %s (%s)\n", productID, err)
continue
}
fmt.Printf("%s;%s;%.2f\n", p.ID, p.Name, p.Price)
}

}

0 comments on commit 093670d

Please sign in to comment.