This repository has been archived by the owner on Apr 28, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
amazon.go
76 lines (67 loc) · 1.78 KB
/
amazon.go
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
package extract
import (
"fmt"
"strings"
"github.com/PuerkitoBio/goquery"
"github.com/asaskevich/govalidator"
"github.com/koffeinsource/notreddit/data"
"golang.org/x/net/html"
)
func amazon(i *data.Item, sourceURL string, doc *goquery.Document) {
if !strings.Contains(sourceURL, "www.amazon.") {
return
}
fmt.Println("Running Amazon plugin.")
// find picture
{
selection := doc.Find("#landingImage")
if len(selection.Nodes) == 0 {
fmt.Println("Amazon plugin found no #landingImage. " + sourceURL)
} else {
if len(selection.Nodes) > 1 {
fmt.Println("Amazon plugin found >1 #landingImage. " + sourceURL)
}
for _, e := range selection.Nodes {
if e.Type == html.ElementNode && e.Data == "img" {
m := htmlAttributeToMap(e.Attr)
if govalidator.IsRequestURL(m["data-old-hires"]) {
i.ImageURL = m["data-old-hires"]
} else {
fmt.Println("Amazon plugin imgURL invalid. " + m["data-old-hires"])
}
}
}
}
}
// update url to contain tag
{
// This is our tag. We should make it configurable
urlExtension := "tag=" + "gschaftshuonl-21"
start := strings.Index(i.URL, "tag=")
if start != -1 {
end := strings.Index(i.URL[start+1:], "&") + start + 1
i.URL = i.URL[:start] + i.URL[end:]
}
if strings.Index(i.URL, "?") == -1 {
i.URL += "?" + urlExtension
} else {
i.URL += "&" + urlExtension
}
}
// update title
{
selection := doc.Find("#productTitle")
if len(selection.Nodes) == 0 {
fmt.Println("Amazon plugin found no #productTitle. " + sourceURL)
} else {
if len(selection.Nodes) > 1 {
fmt.Println("Amazon plugin found >1 #productTitle. " + sourceURL)
}
for _, e := range selection.Nodes {
if e.Type == html.ElementNode && e.Data == "span" {
i.Caption = e.FirstChild.Data
}
}
}
}
}