-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ea43966
commit 093670d
Showing
3 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,3 +25,7 @@ _testmain.go | |
|
||
# Vendor libs | ||
vendor/ | ||
|
||
.vscode/ | ||
auvieux-scraper | ||
debug |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
|
||
} |