forked from edgi-govdata-archiving/epa-search-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
139 lines (114 loc) · 2.92 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
package main
import (
"bytes"
"encoding/csv"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"time"
"github.com/PuerkitoBio/goquery"
)
func main() {
queryData, err := ioutil.ReadFile("queries.csv")
if err != nil {
panic(err)
}
csvr := csv.NewReader(bytes.NewBuffer(queryData))
for {
query, err := csvr.Read()
if err != nil {
if err.Error() == "EOF" {
break
}
panic(err)
}
err = CrawlEntry(query[0])
if err != nil {
panic(err)
}
}
if err := ConcatResults(); err != nil {
panic(err)
}
}
func SearchPageUrl(query string, page, resultCount int) string {
start := ((page - 1) * resultCount) + 1
return fmt.Sprintf("https://nlquery.epa.gov/epasearch/epasearch?querytext=%s&start=%d&results_per_page=%d&cluster=no&filter=&fld=&url_directory=&federated=no&max_results=1000&result_template=2col.ftl&areaname=&areapagehead=epafiles_pagehead&areapagefoot=epafiles_pagefoot&areasidebar=search_sidebar&stylesheet=&sort=term_relevancy&faq=true&cluster=both&sessionid=2950002FD05190D4090549F0E6EB934A&doctype=all&typeofsearch=epa&site=epa_default", query, start, resultCount)
}
func CrawlEntry(query string) error {
page := 1
resultCount := 500
for {
url := SearchPageUrl(query, page, resultCount)
fmt.Println(url)
doc, err := goquery.NewDocument(url)
if err != nil {
return err
}
kontinue, err := CrawlPage(url, doc, query, page, resultCount)
if err != nil {
return err
}
if !kontinue {
break
}
time.Sleep(time.Second)
page++
}
return nil
}
func CrawlPage(url string, doc *goquery.Document, query string, page, resultCount int) (bool, error) {
kontinue := true
sel := doc.Find("#main-content dl").Find("a")
buf := &bytes.Buffer{}
csvw := csv.NewWriter(buf)
now := time.Now().Unix()
fmt.Println("num entries:", len(sel.Nodes))
for i := range sel.Nodes {
node := sel.Eq(i)
href, _ := node.Attr("href")
// fmt.Println(i+1, ",", href)
csvw.Write([]string{strconv.FormatInt(now, 10), href})
}
if len(sel.Nodes) < resultCount {
kontinue = false
}
csvw.Flush()
if err := ioutil.WriteFile(fmt.Sprintf("results/%s-%d-%d.csv", query, page, resultCount), buf.Bytes(), os.ModePerm); err != nil {
return kontinue, err
}
return kontinue, nil
}
func ConcatResults() error {
buf := &bytes.Buffer{}
wr := csv.NewWriter(buf)
err := filepath.Walk("results/", func(path string, fi os.FileInfo, err error) error {
if err != nil {
return err
}
if !fi.IsDir() && filepath.Ext(fi.Name()) == ".csv" {
fmt.Println(filepath.Join("results", fi.Name()))
data, err := ioutil.ReadFile(filepath.Join("results", fi.Name()))
if err != nil {
return err
}
r := csv.NewReader(bytes.NewBuffer(data))
rows, err := r.ReadAll()
if err != nil {
return err
}
return wr.WriteAll(rows)
}
return nil
})
if err != nil {
return err
}
wr.Flush()
if err := ioutil.WriteFile("results.csv", buf.Bytes(), os.ModePerm); err != nil {
return err
}
return nil
}