/
main.go
124 lines (95 loc) · 3.01 KB
/
main.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
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
package main
import (
"net/http"
"regexp"
"strconv"
"strings"
"golang.org/x/net/html"
"golang.org/x/net/html/atom"
"github.com/gin-gonic/gin"
"github.com/yhat/scrape"
)
type Movie struct {
Title string `json:"title"`
Poster string `json:"poster"`
ReleaseYear int `json:"release_year"`
Actors []string `json:"actors"`
SimilarIDs []string `json:"similar_ids"`
}
func validateAndFormatAmazonID(ama string) (string, bool) {
const (
AmazonIDLength = len("B011J35W5O")
Alphanumeric = "^[a-zA-Z0-9]+$" // simple but yet robbed from https://github.com/asaskevich/govalidator
)
var rxAlphanumeric = regexp.MustCompile(Alphanumeric)
parsedID := strings.ToUpper(ama)
invalid := len(parsedID) != AmazonIDLength || parsedID[0] != 'B' || !rxAlphanumeric.MatchString(parsedID)
return parsedID, !invalid //return ID, and boolean for valid IDS
}
func main() {
router := gin.Default()
router.GET("/movie/amazon/:amazon_id", func(c *gin.Context) {
id, valid := validateAndFormatAmazonID(c.Param("amazon_id"))
if !valid {
c.JSON(http.StatusInternalServerError, gin.H{
"error": "invalid amazon id",
"id": id,
})
return
}
resp, err := http.Get("http://www.amazon.de/gp/product/" + id)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"error": err,
})
return
}
//item does not exist in amazon.de
if resp.StatusCode == http.StatusNotFound {
c.JSON(http.StatusNotFound, gin.H{
"error": "product not available",
})
return
}
root, err := html.Parse(resp.Body)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"error": err,
})
return
}
actorsMatcher := func(n *html.Node) bool {
if n.DataAtom == atom.Dd && n.Parent != nil &&
n.PrevSibling != nil && n.PrevSibling.PrevSibling != nil {
return scrape.Attr(n.Parent, "class") == "dv-meta-info size-small" &&
scrape.Text(n.PrevSibling.PrevSibling) == "Darsteller:"
}
return false
}
posterMatcher := func(n *html.Node) bool {
if n.DataAtom == atom.Img && n.Parent != nil {
return scrape.Attr(n.Parent, "class") == "dp-meta-icon-container"
}
return false
}
//NOTE: Since this is a demo, I assume matchers will always hit a result
movie := &Movie{}
titleNode, _ := scrape.Find(root, scrape.ById("aiv-content-title"))
movie.Title = scrape.Text(titleNode.FirstChild)
releaseYearNode, _ := scrape.Find(root, scrape.ByClass("release-year"))
year, _ := strconv.Atoi(scrape.Text(releaseYearNode))
movie.ReleaseYear = year
actorsNode, _ := scrape.Find(root, actorsMatcher)
movie.Actors = strings.Split(scrape.Text(actorsNode), ",")
posterNode, _ := scrape.Find(root, posterMatcher)
movie.Poster = scrape.Attr(posterNode, "src")
movieNodes := scrape.FindAll(root, scrape.ByClass("downloadable_movie"))
ids := make([]string, len(movieNodes))
for i, movieNode := range movieNodes {
ids[i] = scrape.Attr(movieNode, "data-asin")
}
movie.SimilarIDs = ids
c.JSON(http.StatusOK, movie)
})
router.Run(":8080")
}