-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
227 lines (186 loc) · 5.7 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package main
import (
"context"
"fmt"
"log"
"regexp"
"strings"
"sync"
"github.com/joshuamcginnis/biz-hunt/gui"
"github.com/chromedp/cdproto/cdp"
"github.com/chromedp/chromedp"
"github.com/chromedp/chromedp/runner"
)
const depth int = 2
const baseURL string = "https://www.bizbuysell.com"
const startURL string = "https://www.bizbuysell.com/california/los-angeles-county-businesses-for-sale"
// Listing represents the details from an individual listing
type Listing struct {
// URL string
Title string
Location string
AskingPrice string
CashFlow string
// GrossRevenue uint
// Ebitda uint
// FFE uint
// InventoryValue uint
// Rent string
// Established uint
Description string
// Inventory string
// RealEstate string
// BuildingSF uint
// LeaseExpiration string
// Facilities string
// SupportAndTraining string
// ReasonForSelling string
}
func main() {
gui.render()
var err error
ctxt, cancel := context.WithCancel(context.Background())
defer cancel()
pool, err := chromedp.NewPool()
defer pool.Shutdown()
c, err := pool.Allocate(ctxt)
if err != nil {
log.Fatalln("Error starting CDP instance.", err)
}
u := "https://www.bizbuysell.com/Business-Opportunity/Public-Works-Construction-7MM-Multi-Year-Contracts-Key-Employees/1577140/"
err = c.Run(ctxt, loadListing(u))
if err != nil {
log.Fatalln("Error navigating to url", err)
}
var title string
if err = c.Run(ctxt, chromedp.Text(".bfsTitle", &title, chromedp.BySearch)); err != nil {
log.Fatalln("Could not get title.", err)
}
var description string
if err = c.Run(ctxt, chromedp.Text(".businessDescription", &description, chromedp.BySearch)); err != nil {
log.Fatalln("Could not get description.", err)
}
var askingPrice string
if err = c.Run(ctxt, chromedp.Text(".price b", &askingPrice, chromedp.BySearch)); err != nil {
log.Fatalln("Could not get askingPrice.", err)
}
var location string
if err = c.Run(ctxt, chromedp.Text(".row-fluid h2.gray", &location, chromedp.BySearch)); err != nil {
log.Fatalln("Could not get location.", err)
}
var cashFlow string
if err = c.Run(ctxt, chromedp.Text("div.row-fluid.lgFinancials > div:nth-child(2) > p > b", &cashFlow, chromedp.BySearch)); err != nil {
log.Fatalln("Could not get location.", err)
}
l := Listing{
sanitizeInput(title),
sanitizeInput(location),
sanitizeInput(askingPrice),
sanitizeInput(cashFlow),
sanitizeInput(description),
}
fmt.Println(l)
defer c.Release()
// //poolLog := chromedp.PoolLog(log.Printf, log.Printf, log.Printf)
// pool, err := chromedp.NewPool() //(poolLog)
// defer pool.Shutdown()
// var urlToVisit strings.Builder
// var listingUrls []string
// for i := 1; i < depth; i++ {
// urlToVisit.Reset()
// urlToVisit.WriteString(startURL)
// if i > 1 {
// urlToVisit.WriteString(fmt.Sprintf("/%v", i))
// }
// url := urlToVisit.String()
// var listingNodes []*cdp.Node
// err = c.Run(ctxt, getListingUrls(url, &listingNodes))
// if err != nil {
// log.Fatalln("Error running getListingUrls.", err)
// }
// for n := 0; n < len(listingNodes); n++ {
// href := listingNodes[n].AttributeValue("href")
// urlToVisit.Reset()
// urlToVisit.WriteString(baseURL)
// urlToVisit.WriteString(href)
// formattedURL := strings.Split(urlToVisit.String(), "?")[0]
// fmt.Println(formattedURL)
// listingUrls = append(listingUrls, formattedURL)
// }
// }
// fmt.Println(listingUrls)
// chrome.Release()
// var wg sync.WaitGroup
// var listings []Listing
// for _, urlStr := range listingUrls {
// wg.Add(1)
// go getListingDetails(ctxt, pool, &wg, urlStr, &listings)
// }
// wg.Wait()
// err = pool.Shutdown()
// fmt.Println("shutting down")
// if err != nil {
// log.Fatal(err)
// }
}
func sanitizeInput(input string) string {
pattern := regexp.MustCompile(`[\n\s•]+`)
input = pattern.ReplaceAllString(input, " ")
input = strings.ReplaceAll(input, "’", "'")
input = strings.TrimSpace(input)
return input
}
func getListingDetails(ctxt context.Context, pool *chromedp.Pool, wg *sync.WaitGroup, urlStr string, listings *[]Listing) {
defer wg.Done()
chrome, err := pool.Allocate(ctxt,
runner.Flag("headless", true),
runner.Flag("disable-gpu", true),
runner.Flag("no-first-run", true),
runner.Flag("no-sandbox", true),
runner.Flag("no-default-browser-check", true),
//3runner.Flag("remote-debugging-port", 9222),
)
if err != nil {
log.Fatal(err)
}
defer chrome.Release()
var description string
err = chrome.Run(ctxt, getDescription(urlStr, &description))
if err != nil {
log.Fatal(err)
}
fmt.Println(urlStr, "hi")
fmt.Println(description, "hello")
}
func getDescription(url string, description *string) chromedp.Tasks {
fmt.Println("running")
return chromedp.Tasks{
chromedp.Navigate(url),
chromedp.Text(".businessDescription", description, chromedp.BySearch),
}
}
func getListingUrls(url string, listingNodes *[]*cdp.Node) chromedp.Tasks {
fmt.Println("heyoo", url)
return chromedp.Tasks{
chromedp.Navigate(url),
chromedp.WaitReady(".pagination", chromedp.BySearch),
chromedp.Nodes(".listingResult", listingNodes, chromedp.BySearch),
}
}
func getListingInfo(url string, details *Listing, bodyNodes *[]*cdp.Node) chromedp.Action {
return chromedp.Tasks{
chromedp.Navigate(url),
chromedp.WaitReady("#footer", chromedp.BySearch),
chromedp.Nodes(".pageContent", bodyNodes, chromedp.BySearch),
chromedp.ActionFunc(func(ctxt context.Context, h cdp.Executor) error {
fmt.Println(bodyNodes)
return nil
}),
}
}
func loadListing(url string) chromedp.Action {
return chromedp.Tasks{
chromedp.Navigate(url),
chromedp.WaitReady("#footer"),
}
}