/
directions.go
145 lines (129 loc) · 3.5 KB
/
directions.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package meanrecipe
import (
"errors"
"fmt"
"net/http"
"net/url"
"strings"
"time"
"github.com/PuerkitoBio/goquery"
log "github.com/cihub/seelog"
"golang.org/x/net/proxy"
)
var UseTor bool
func GetDirections(recipe string, include []string, exclude []string) (directions []string, err error) {
recipeURL, err := getRecipeURL(recipe, include, exclude)
if err != nil {
return
}
directions, err = getDirections(recipeURL)
return
}
func getDirections(allRecipesURL string) (directions []string, err error) {
log.Debugf("getting recipe directions for url %s", allRecipesURL)
req, err := http.NewRequest("GET", allRecipesURL, nil)
if err != nil {
return
}
req.Header.Set("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36")
timeout := time.Duration(30 * time.Second)
client := http.Client{
Timeout: timeout,
}
if UseTor {
tbProxyURL, err := url.Parse("socks5://127.0.0.1:9050")
if err != nil {
return []string{}, err
}
tbDialer, err := proxy.FromURL(tbProxyURL, proxy.Direct)
if err != nil {
return []string{}, err
}
tbTransport := &http.Transport{Dial: tbDialer.Dial}
client.Transport = tbTransport
}
resp, err := client.Do(req)
if err != nil {
return
}
defer resp.Body.Close()
doc, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
return
}
// Find the urls
directions = []string{}
doc.Find("span.recipe-directions__list--item").Each(func(i int, s *goquery.Selection) {
direction := strings.TrimSpace(s.Text())
if len(direction) < 3 {
return
}
directions = append(directions, direction)
})
if len(directions) == 0 {
err = errors.New("no directions")
return
}
return
}
func getRecipeURL(recipe string, include []string, exclude []string) (recipeURL string, err error) {
recipe = url.QueryEscape(strings.Replace(recipe, "_", " ", -1))
for i := range include {
include[i] = url.QueryEscape(include[i])
}
for i := range exclude {
exclude[i] = url.QueryEscape(exclude[i])
}
urlToGet := fmt.Sprintf("https://www.allrecipes.com/search/results/?wt=%s&ingIncl=%s&ingExcl=%s&sort=re", url.QueryEscape(recipe), strings.Join(include, ","), strings.Join(exclude, ","))
log.Debugf("getting recipe url for %s +%+v -%+v (%s)", recipe, include, exclude, urlToGet)
req, err := http.NewRequest("GET", urlToGet, nil)
if err != nil {
return
}
req.Header.Set("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36")
timeout := time.Duration(30 * time.Second)
client := http.Client{
Timeout: timeout,
}
if UseTor {
tbProxyURL, err := url.Parse("socks5://127.0.0.1:9050")
if err != nil {
return "", err
}
tbDialer, err := proxy.FromURL(tbProxyURL, proxy.Direct)
if err != nil {
return "", err
}
tbTransport := &http.Transport{Dial: tbDialer.Dial}
client.Transport = tbTransport
}
resp, err := client.Do(req)
if err != nil {
return
}
defer resp.Body.Close()
doc, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
return
}
// Find the urls
urls := []string{}
doc.Find("div.fixed-recipe-card__info > a").Each(func(i int, s *goquery.Selection) {
href, ok := s.Attr("href")
if !ok {
return
}
href, _ = url.QueryUnescape(href)
if strings.Contains(href, "=http") {
href = "http" + strings.Split(href, "=http")[1]
}
urls = append(urls, href)
// fmt.Printf("%d) %s\n", i, href)
})
if len(urls) == 0 {
err = errors.New("no urls")
return
}
recipeURL = urls[0]
return
}