Skip to content

Commit ce9ec22

Browse files
author
Francesc Campoy
committed
last improvements
1 parent abd40f0 commit ce9ec22

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

main.go

+17-12
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,12 @@ func main() {
3030
)
3131
flag.Parse()
3232

33-
// Create all tasks and send them to the channel.
33+
columns := []string{*name, *address, *phone, *email}
34+
headers := []string{"name", "address", "phone", "email"}
35+
// url and id are added as the first two rows.
36+
headers = append([]string{"url", "id"}, headers...)
37+
38+
// create all tasks and send them to the channel.
3439
type task struct {
3540
url string
3641
id int
@@ -43,7 +48,7 @@ func main() {
4348
close(tasks)
4449
}()
4550

46-
// Create workers and schedule closing results when all work is done.
51+
// create workers and schedule closing results when all work is done.
4752
results := make(chan []string)
4853
var wg sync.WaitGroup
4954
wg.Add(*concurrency)
@@ -56,7 +61,7 @@ func main() {
5661
go func() {
5762
defer wg.Done()
5863
for t := range tasks {
59-
r, err := fetch(t.url, t.id, *name, *address, *phone, *email)
64+
r, err := fetch(t.url, t.id, columns)
6065
if err != nil {
6166
log.Printf("could not fetch %v: %v", t.url, err)
6267
continue
@@ -66,12 +71,12 @@ func main() {
6671
}()
6772
}
6873

69-
if err := dumpCSV(*outfile, results); err != nil {
74+
if err := dumpCSV(*outfile, headers, results); err != nil {
7075
log.Printf("could not write to %s: %v", *outfile, err)
7176
}
7277
}
7378

74-
func fetch(url string, id int, queries ...string) ([]string, error) {
79+
func fetch(url string, id int, queries []string) ([]string, error) {
7580
res, err := http.Get(url)
7681
if err != nil {
7782
return nil, fmt.Errorf("could not get %s: %v", url, err)
@@ -86,21 +91,21 @@ func fetch(url string, id int, queries ...string) ([]string, error) {
8691
return nil, fmt.Errorf("bad response from server: %s", res.Status)
8792
}
8893

89-
// Load response into GoQuery
94+
// parse body with goquery.
9095
doc, err := goquery.NewDocumentFromReader(res.Body)
9196
if err != nil {
9297
return nil, fmt.Errorf("could not parse page: %v", err)
9398
}
9499

95-
// Extract info we want
100+
// extract info we want.
96101
r := []string{url, strconv.Itoa(id)}
97102
for _, q := range queries {
98103
r = append(r, strings.TrimSpace(doc.Find(q).Text()))
99104
}
100105
return r, nil
101106
}
102107

103-
func dumpCSV(path string, records <-chan []string) error {
108+
func dumpCSV(path string, headers []string, records <-chan []string) error {
104109
f, err := os.Create(path)
105110
if err != nil {
106111
return fmt.Errorf("unable to create file %s: %v", path, err)
@@ -110,19 +115,19 @@ func dumpCSV(path string, records <-chan []string) error {
110115
w := csv.NewWriter(f)
111116
defer w.Flush()
112117

113-
// Write headers to file
114-
if err := w.Write([]string{"id", "name", "url", "address", "phone", "email"}); err != nil {
118+
// write headers to file.
119+
if err := w.Write(headers); err != nil {
115120
log.Fatalf("error writing record to csv: %v", err)
116121
}
117122

118-
// Write all records
123+
// write all records.
119124
for r := range records {
120125
if err := w.Write(r); err != nil {
121126
log.Fatalf("could not write record to csv: %v", err)
122127
}
123128
}
124129

125-
// Check for extra errors
130+
// check for extra errors.
126131
if err := w.Error(); err != nil {
127132
return fmt.Errorf("writer failed: %v", err)
128133
}

0 commit comments

Comments
 (0)